C Programming - Bitwise Operators - Discussion

Discussion Forum : Bitwise Operators - Find Output of Program (Q.No. 4)
4.
If an unsigned int is 2 bytes wide then, What will be the output of the program ?
#include<stdio.h>

int main()
{
    unsigned int a=0xffff;
    ~a;
    printf("%x\n", a);
    return 0;
}
ffff
0000
00ff
ddfd
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
47 comments Page 3 of 5.

Sree said:   1 decade ago
@Rishabh: 0Xffff means 1111 1111 1111 1111. u knew digital systems rite? In that how will u specify 15 in hexadecimal form? 'F' na?

Gowda said:   1 decade ago
Does the operator ~ preserves sign of variable when applying on it ?

Cherry said:   1 decade ago
Hi. Guys. Here a is absolutely complemented but it didn't assigned to any other variable. So that original 'a' is printed. So simple. Have a great day.

Vivek said:   1 decade ago
Hi guys...'~' is an unary operator...which means that there s no need to assign '~a' to any variable...the complemented value is automatically stored in variable 'a'...
For eg: consider i=3;
i++;
cout<<i;//It ll print the value of i as 4.which means that the value is incremented and stored in the variable 'i'.So,i think the answer for the above question s 0000.

Taruna said:   1 decade ago
@Sonia Khanna if using 32 bit compiler a=0xffff means
0000 0000 0000 0000 1111 1111 1111 1111
complement it u get
1111 1111 1111 1111 0000 0000 0000 0000
i.e. 0xffff0000

Madhukar said:   1 decade ago
Yes taruna is right.

JHALAK said:   1 decade ago
@Sonia khanna I did run your code on my dos box and the output is not 0xffff0000 but it is 0000 only so this clears the doubt that ~a does not store the complemented value in a. IF you write a=~a or any other variable b=~a then it would give you 0000 as an output.

Prateek said:   1 decade ago
It is unsigned int so in that highest of the high is supposed to be zero.

So ~0 is = 0-1 = -1.

And as you know in -1 all the bit is set so ffff.

Deepak kumar Dubey said:   1 decade ago
What will be the order of printing?

int i=4, j=8;

printf("%d %d %d\n",i|j&j|i,i|j&&j|i,i^j);

Rockz said:   1 decade ago
Order of printing is as usual. But the order of execution is like a stack.
You will get 12 1 12 as answer.

Because i^j ll execute 1st as it push into stack 1st. It performs ex-or operation. 0100 ^ 1000=1100. in decimal it is 12.

Then i|j && j|i. v knw both i|j and j|i will give same value of 12. So on doing logical and we'll get 1.

At last i|j and j|i. Its normal and operation.


Post your comments here:

Your comments will be displayed after verification.