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.
Sangharsh said:
2 decades ago
How is 32 << -1 is equal to 0 ?
Madhureddy said:
1 decade ago
Hi friends, can any body explain it to me I'm helpless ?
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!
Atul said:
1 decade ago
32 << -1 is nothing but 32 << 256 (2's complement of -1 in decimal notation) which will add 0 to all bit positions of 32.
Regi said:
1 decade ago
Can any one explain clearly?
Apurva Nigam said:
1 decade ago
Thanks Atul.
Nikita said:
1 decade ago
2's comp of -1 is 1, how 256?
Rashmi mate said:
1 decade ago
How is 32 << -1 is equal to 0 ? I'm unable to understand this.
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
NITESH said:
1 decade ago
Ok so -1 is being treated here as 256. Thanx Atul.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers