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 2 of 5.
Vineet said:
1 decade ago
Hello,
Why are you all taking -1 as 8 bit number? I can't understand because by default every integer constant is of 16 bits.
Why are you all taking -1 as 8 bit number? I can't understand because by default every integer constant is of 16 bits.
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.
Kumar said:
1 decade ago
How can 1111 1111 be 256, its 255 no? if I am wrong please tell me how it is?
Kumar said:
1 decade ago
32<<-1 is similar to -1>>32.Try this you will get answer 0 for that
Kasi said:
1 decade ago
I need bief explanation of >>, << for these two operartors with an example.
Can any one help me.
Can any one help me.
Ravishankara Asakapalli said:
1 decade ago
I need a complete Explanation on the 0xfffff. i didn't get what exactly it means.............
Kgp vinod said:
1 decade ago
Ravishankara Asakapalli :
0xfffff this is hexadecimal number.
where f=1111
so ffff=1111 1111 1111 1111
0xfffff this is hexadecimal number.
where f=1111
so ffff=1111 1111 1111 1111
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.
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.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers