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;
}
ffff
0000
ffdf
ddfd
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
35 comments Page 1 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.
(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.
(5)

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

Sudheer kumar raina said:   1 decade ago
1) 32--> in binary--> for 2 bytes --> 0000 0000 0010 0000.

2) Now ~m makes all 0's to 1's and viceversa. ---> 1111 1111 1101 1111.

3) Now lets convert them into hexa, as %x is specified as format specifier.

4) Now the output in 2nd step becomes "f (15) f (d (13) f" in hexa decimal.

5) Finally "ffdf" is the answer.

Mounika kursing said:   1 decade ago
32 16 8 4 2 1

2^5 2^4 2^3 2^2 2^1 2^0

0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0
1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1
f f d f

We want 32, So keep 1 at 32 all other 0.
For negation replace 0's with 1's.

Jagajjeevan said:   1 decade ago
~a=-(a+1)
~32=-(32+1)=-33

Binary of 33 : 0000 0000 0001 0001
1's of 33 : 1111 1111 1110 1110
2's of 33(-33): 1
1111 1111 1110 1111
f f d f
~32 is ffdf

Sravanthi said:   1 decade ago
Assume each digit from right to left as 2 power i.
Where i =0,1,2...

Now 32 is 0000 0000 0010 0000.

Which mean 0*(2 power 0)+0*(2 power 1)+0*(2 power 2)+0*(2 power 3)+0*(2 power 4)+1*(2 power 5)+0*(2 power 6)+..

Gaurav bisht said:   1 decade ago
Since unsigned 2 byte m = 32 can be represented as:

0000 0000 0010 0000

To do ~m means just complimenting m i.e.

~m = 1111 1111 1101 1111

In Hexa 1111 = f and 1101 = d

Therefore result is ffdf.

Abani said:   1 decade ago
Binary representation of 32 in 16bit(turbo c) is 0000000000100000

Hence 1's complement of 32 is 1111111111011111 and whose hexadecimal form is FFDF which is option C.

Sudhir kushwaha said:   1 decade ago
m=32
In binary: 0000 0000 0010 0000
~ it called tiled i.e. 1's complement of given no.
Hence ~m : 1111 1111 1101 1111

f f d f

Hence C is answer


Post your comments here:

Your comments will be displayed after verification.