C Programming - Bitwise Operators - Discussion
Discussion Forum : Bitwise Operators - Point Out Correct Statements (Q.No. 1)
1.
Which of the following statements are correct about the program?
#include<stdio.h>
int main()
{
unsigned int num;
int i;
scanf("%u", &num);
for(i=0; i<16; i++)
{
printf("%d", (num<<i & 1<<15)?1:0);
}
return 0;
}
Answer: Option
Explanation:
If we give input 4, it will print 00000000 00000100 ;
If we give input 3, it will print 00000000 00000011 ;
If we give input 511, it will print 00000001 11111111 ;
Discussion:
19 comments Page 1 of 2.
Viraj said:
1 decade ago
1 <<15 = -32768 i.e. 1000 0000 0000 0000
In the loop the bits of 'num' are brought to the MSB one by one using left shift and ANDed with the 1000 0000 0000 0000
In the loop the bits of 'num' are brought to the MSB one by one using left shift and ANDed with the 1000 0000 0000 0000
Mohanty said:
1 decade ago
What is "i<16"?
Abhishek said:
1 decade ago
@Mohanty
num<<i
shifts one bit, so to shift 16 bits the condition is set as i<16
num<<i
shifts one bit, so to shift 16 bits the condition is set as i<16
Ankit said:
1 decade ago
Please explain it clearly.
Sadhna said:
1 decade ago
Hi, I have a doubt in printf statement it will print either 0 or 1 as we are printing the result of a conditional statement, can someone explain this to me ?
Prem said:
1 decade ago
If the condition is true it will print as 1 otherwise 0.
BHAKAR said:
1 decade ago
for(i=0; i<16; i++)
{
printf("%d", (num<<i & 1<<15)?1:0);
}
Lets assume num = 4.
Then num<<0 =0000 0000 0000 0100 & (1000 0000 0000 0000 this is 1<<15) = (all 0 so false condion so 0
PRINT 0
same repeat
Then num<<1 =0000 0000 0000 1000 & (1000 0000 0000 0000 this is 1<<15)= (all 0 so false condion so 0
PRINT 0
AT 14 LOOP POSITION
Then num<<14 =1000 0000 0000 0000 & (1000 0000 0000 0000 this is 1<<15)= (all 1000 0000 0000 0000 so TRUE condion so 1
PRINT 1
0000 0000 0000 0100
{
printf("%d", (num<<i & 1<<15)?1:0);
}
Lets assume num = 4.
Then num<<0 =0000 0000 0000 0100 & (1000 0000 0000 0000 this is 1<<15) = (all 0 so false condion so 0
PRINT 0
same repeat
Then num<<1 =0000 0000 0000 1000 & (1000 0000 0000 0000 this is 1<<15)= (all 0 so false condion so 0
PRINT 0
AT 14 LOOP POSITION
Then num<<14 =1000 0000 0000 0000 & (1000 0000 0000 0000 this is 1<<15)= (all 1000 0000 0000 0000 so TRUE condion so 1
PRINT 1
0000 0000 0000 0100
Haritha said:
1 decade ago
Please explain it more clearly.
Uma shnakr said:
1 decade ago
for(i=0; i<16; i++)
{
printf("%d", (num<<i & 1<<15)?1:0);
}
Lets assume num = 2.
Then num<<0 =0000 0000 0000 0100 & (1000 0000 0000 0000 this is 1<<15) = (all 0 so false condion so 0
PRINT 0
printed yet=0
same repeat
Then num<<1 =0000 0000 0000 1000 & (1000 0000 0000 0000 this is 1<<15)= (all 0 so false condion so 0
PRINT 0
printed yet=00
Then num<<2 =0000 0000 0000 1000 & (1000 0000 0000 0000 this is 1<<15)= (all 0 so false condion so 0
PRINT 0
printed yet=000
...............
AT 14 LOOP POSITION
Then num<<14 =1000 0000 0000 0000 & (1000 0000 0000 0000 this is 1<<15)= (all 1000 0000 0000 0000 so TRUE condion so 1
PRINT 1
printed yet=000000000000001
0000 0000 0000 0100
AT 15 LOOP POSITION
Then num<<15 =1000 0000 0000 0000 & (1000 0000 0000 0000 this is 1<<15)= (all 1000 0000 0000 0000 so TRUE condion so 1
PRINT 0
printed yet=0000000000000010
hence finaly we have 0000000000000010=2
{
printf("%d", (num<<i & 1<<15)?1:0);
}
Lets assume num = 2.
Then num<<0 =0000 0000 0000 0100 & (1000 0000 0000 0000 this is 1<<15) = (all 0 so false condion so 0
PRINT 0
printed yet=0
same repeat
Then num<<1 =0000 0000 0000 1000 & (1000 0000 0000 0000 this is 1<<15)= (all 0 so false condion so 0
PRINT 0
printed yet=00
Then num<<2 =0000 0000 0000 1000 & (1000 0000 0000 0000 this is 1<<15)= (all 0 so false condion so 0
PRINT 0
printed yet=000
...............
AT 14 LOOP POSITION
Then num<<14 =1000 0000 0000 0000 & (1000 0000 0000 0000 this is 1<<15)= (all 1000 0000 0000 0000 so TRUE condion so 1
PRINT 1
printed yet=000000000000001
0000 0000 0000 0100
AT 15 LOOP POSITION
Then num<<15 =1000 0000 0000 0000 & (1000 0000 0000 0000 this is 1<<15)= (all 1000 0000 0000 0000 so TRUE condion so 1
PRINT 0
printed yet=0000000000000010
hence finaly we have 0000000000000010=2
Mahendra said:
1 decade ago
correct one goes below
for(i=0; i<16; i++)
{
printf("%d", (num<<i & 1<<15)?1:0);
}
Lets assume num = 4.
Then num<<0 =0000 0000 0000 0100 & (1000 0000 0000 0000 this is 1<<15) = (all 0 so false condion so 0
PRINT 0
printed yet=0
same repeat
Then num<<1 =0000 0000 0000 1000 & (1000 0000 0000 0000 this is 1<<15)= (all 0 so false condion so 0
PRINT 00
printed yet=00
Then num<<2 =0000 0000 0000 1000 & (1000 0000 0000 0000 this is 1<<15)= (all 0 so false condion so 0
PRINT 0
printed yet=000
...............
AT 13 LOOP POSITION
Then num<<13 =1000 0000 0000 0000 & (1000 0000 0000 0000 this is 1<<15)= (all 1000 0000 0000 0000 so TRUE condion so 1
PRINT 1
printed yet=0000000000001
0000 0000 0000 0100
AT 15 LOOP POSITION
Then num<<15 =0000 0000 0000 0010 & (1000 0000 0000 0000 this is 1<<15)= (all 1000 0000 0000 0000 so false condion so 0
PRINT 0
printed yet=0000000000000100
hence finaly we have 000000000000100=4
for(i=0; i<16; i++)
{
printf("%d", (num<<i & 1<<15)?1:0);
}
Lets assume num = 4.
Then num<<0 =0000 0000 0000 0100 & (1000 0000 0000 0000 this is 1<<15) = (all 0 so false condion so 0
PRINT 0
printed yet=0
same repeat
Then num<<1 =0000 0000 0000 1000 & (1000 0000 0000 0000 this is 1<<15)= (all 0 so false condion so 0
PRINT 00
printed yet=00
Then num<<2 =0000 0000 0000 1000 & (1000 0000 0000 0000 this is 1<<15)= (all 0 so false condion so 0
PRINT 0
printed yet=000
...............
AT 13 LOOP POSITION
Then num<<13 =1000 0000 0000 0000 & (1000 0000 0000 0000 this is 1<<15)= (all 1000 0000 0000 0000 so TRUE condion so 1
PRINT 1
printed yet=0000000000001
0000 0000 0000 0100
AT 15 LOOP POSITION
Then num<<15 =0000 0000 0000 0010 & (1000 0000 0000 0000 this is 1<<15)= (all 1000 0000 0000 0000 so false condion so 0
PRINT 0
printed yet=0000000000000100
hence finaly we have 000000000000100=4
(2)
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers