C Programming - Bitwise Operators - Discussion
Discussion Forum : Bitwise Operators - Find Output of Program (Q.No. 10)
10.
What will be the output of the program?
#include<stdio.h>
int main()
{
printf("%d %d\n", 32<<1, 32<<0);
printf("%d %d\n", 32<<-1, 32<<-0);
printf("%d %d\n", 32>>1, 32>>0);
printf("%d %d\n", 32>>-1, 32>>-0);
return 0;
}
Discussion:
42 comments Page 1 of 5.
Sundar said:
1 decade ago
Refer the explanation given following questions. You will understand it better way.
http://www.indiabix.com/c-programming/bitwise-operators/discussion-511
http://www.indiabix.com/c-programming/bitwise-operators/discussion-503
Hope this help you. Have a nice day!
http://www.indiabix.com/c-programming/bitwise-operators/discussion-511
http://www.indiabix.com/c-programming/bitwise-operators/discussion-503
Hope this help you. Have a nice day!
Ahmed said:
8 years ago
In case;
32<<1 means 0010 0000 are add +1 0100 0000 = 64.
32<<0 means 0010 0000 are add +0 0010 0000 = 32.
32<<-1 means 0010 0000 Zero -1 0000 0000 = 0.
32<<-0 means 0010 0000 L less -0 0010 0000 = 32.
32>>-1 means 0010 0000 Zero -1 0100 0000 = 64.
32>>-0 means 0010 0000 L less -0 0010 0000 = 0.
What the mean of the abbreviation l Less -0 and Zero -1.
32<<1 means 0010 0000 are add +1 0100 0000 = 64.
32<<0 means 0010 0000 are add +0 0010 0000 = 32.
32<<-1 means 0010 0000 Zero -1 0000 0000 = 0.
32<<-0 means 0010 0000 L less -0 0010 0000 = 32.
32>>-1 means 0010 0000 Zero -1 0100 0000 = 64.
32>>-0 means 0010 0000 L less -0 0010 0000 = 0.
What the mean of the abbreviation l Less -0 and Zero -1.
(1)
R K said:
1 decade ago
In case:
32<<1 means 0010 0000 are add +1 0100 0000 = 64.
32<<0 means 0010 0000 are add +0 0010 0000 = 32.
32<<-1 means 0010 0000 Zero -1 0000 0000 = 0.
32<<-0 means 0010 0000 L less -0 0010 0000 = 32.
32>>-1 means 0010 0000 Zero -1 0100 0000 = 64.
32>>-0 means 0010 0000 L less -0 0010 0000 = 0.
32<<1 means 0010 0000 are add +1 0100 0000 = 64.
32<<0 means 0010 0000 are add +0 0010 0000 = 32.
32<<-1 means 0010 0000 Zero -1 0000 0000 = 0.
32<<-0 means 0010 0000 L less -0 0010 0000 = 32.
32>>-1 means 0010 0000 Zero -1 0100 0000 = 64.
32>>-0 means 0010 0000 L less -0 0010 0000 = 0.
(1)
Vasani Anil said:
1 decade ago
printf("%d %d\n", 32<<1, 32<<0); // 64 32 same as operation.
printf("%d %d\n", 32<<-1, 32<<-0);
-1=11111111 11111111 it's value more than 16 so o/p 0 32.
printf("%d %d\n", 32>>1, 32>>0);
printf("%d %d\n", 32>>-1, 32>>-0);// same as above reason.
printf("%d %d\n", 32<<-1, 32<<-0);
-1=11111111 11111111 it's value more than 16 so o/p 0 32.
printf("%d %d\n", 32>>1, 32>>0);
printf("%d %d\n", 32>>-1, 32>>-0);// same as above reason.
Sindhu said:
1 decade ago
32<<1 means : Binary value of 32 is 00100000 << 1 gives 01000000 gives 64.
32<<0 has no change gives the same value.
32<<-1 means: As -1 is negative number should take its 2's compliment value i.e 15 .so << by 15bits gives result zero to binary value of 32.
32<<0 has no change gives the same value.
32<<-1 means: As -1 is negative number should take its 2's compliment value i.e 15 .so << by 15bits gives result zero to binary value of 32.
Raj Naik said:
1 decade ago
@Nikita
Negative numbers are treated with 2's complement method.
1's complement: Inverting the bits ( all 1s to 0s and all 0s to 1s)
2's complement: Adding 1 to the result of 1's complement
Hence
1111 1110 +
0000 0001
___________________
1111 1111 --> Which is equal to 256
Negative numbers are treated with 2's complement method.
1's complement: Inverting the bits ( all 1s to 0s and all 0s to 1s)
2's complement: Adding 1 to the result of 1's complement
Hence
1111 1110 +
0000 0001
___________________
1111 1111 --> Which is equal to 256
Vincent said:
1 decade ago
C reference manual says that result of a shift by negative value is undefined. So it really means probably that this value depends on a C compiler. Because of this I do not think that the question is appropriate for general C language test.
Divya said:
1 decade ago
@vineet:
You are right. We have to consider 16 bits then -1 is represented as 16 '1's in 2's compliment. So it equals to (2 power 16)-1 if you shift by that many times the value of the exprssion is 0.
You are right. We have to consider 16 bits then -1 is represented as 16 '1's in 2's compliment. So it equals to (2 power 16)-1 if you shift by that many times the value of the exprssion is 0.
Jason said:
8 years ago
The behavior is undefined according to "standard" C. You may interpret -1 as shift into opposite direction ( << -1 is actually >> 1) or you may interpret as (unsigned ) -1.
Shikha said:
1 decade ago
For an instance 32<<1
32 can be written as : 0011 0010
When you shift left by 1 bit, you get : 0110 0100
Which is 64. Similarly you can try for all options.
32 can be written as : 0011 0010
When you shift left by 1 bit, you get : 0110 0100
Which is 64. Similarly you can try for all options.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers