C Programming - Bitwise Operators - Discussion
Discussion Forum : Bitwise Operators - Find Output of Program (Q.No. 2)
2.
If an unsigned int is 2 bytes wide then, What will be the output of the program ?
#include<stdio.h>
int main()
{
unsigned int m = 32;
printf("%x\n", ~m);
return 0;
}
Discussion:
35 comments Page 4 of 4.
Jishan said:
8 years ago
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.
Binary of 32(2byte) : 0000 0000 0010 0000.
Representing -32:
1s complement of 32(2byte) : 1111 1111 1101 1111.
Adding 1 to 1's comp. result : 1111 1111 1110 0000.
Hexadecimal : f f e 0.
This is not given in the option, checked on Ubuntu machine.
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.
Binary of 32(2byte) : 0000 0000 0010 0000.
Representing -32:
1s complement of 32(2byte) : 1111 1111 1101 1111.
Adding 1 to 1's comp. result : 1111 1111 1110 0000.
Hexadecimal : f f e 0.
This is not given in the option, checked on Ubuntu machine.
(3)
Zakar said:
7 years ago
32 complement = 1111 1111 1101 1111.
In hexadecimal f f d f.
In hexadecimal f f d f.
(3)
Nikhil tekade said:
3 years ago
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.
Binary of 32(2byte) : 0000 0000 0010 0000.
Representing -32:
1s complement of 32(2byte) : 1111 1111 1101 1111.
Adding 1 to 1's comp. result : 1111 1111 1110 0000.
Hexadecimal: f f df.
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.
Binary of 32(2byte) : 0000 0000 0010 0000.
Representing -32:
1s complement of 32(2byte) : 1111 1111 1101 1111.
Adding 1 to 1's comp. result : 1111 1111 1110 0000.
Hexadecimal: f f df.
(5)
Meghana k said:
2 years ago
Thank you it was helpful.
Mallesh said:
3 months ago
#include<stdio.h>
int main()
{
unsigned int m = 32;
printf("%x\n", ~m);
return 0;
}
Ans:-
1's complement: Inverting the bits ( all 1s to 0s and all 0s to 1s).
Binary of 32(2 byte) : 0000 0000 0010 0000.
Representing -32: Make it 1's complement then,
1s complement of 32(2byte) : 1111 1111 1101 1111.
This is the result: ffdf
int main()
{
unsigned int m = 32;
printf("%x\n", ~m);
return 0;
}
Ans:-
1's complement: Inverting the bits ( all 1s to 0s and all 0s to 1s).
Binary of 32(2 byte) : 0000 0000 0010 0000.
Representing -32: Make it 1's complement then,
1s complement of 32(2byte) : 1111 1111 1101 1111.
This is the result: ffdf
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers