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 2 of 5.

Anu said:   1 decade ago
Like other unary operator they store the resultant value in operand itself on which the unary operator is applied.

For example a =7 and ++a stores 8 in a so why doesn't ~a stores the result in a.

Shan said:   1 decade ago
What does 0Xfff represents? can anyone explain clearly?

Poulami said:   1 decade ago
What is the meaning of 0x..?

Don no 1 said:   1 decade ago
Here ~a is used but not assign a value back to a so that a will not be affected.

Amrit sharma said:   1 decade ago
@Deepak.

1st (i|j&j|i).
Where i = 4= 0100(binary) and j = 8 = 1000(binary).
Now i|j where'|' is OR operator i.e it will 1100(12).
Similarly j|i will be 1100(12).

Now i|j&j|i where '&' operator as AND operator
1100(i|j)
& 1100(j|i)
1100(i|j&j|i) i.e 12.

Now second(i|j&&j|i)

Hence (12&&12) where '&&' is logical operator which output is either 0 or 1.

Hence here Rvalue = Lvalue so it is true.
is 1. so print 1.

Now 3rd (i^j) where '^' is XOR operator
i = 4 (0100)
^ j = 8 (1000)
(i^j) 1100.

So output is as 12 1 12 respectively.

Deepak Kumar Dubey said:   1 decade ago
int i=4, j=8;

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

Can some explain me the way above question going to work.

Priya said:   1 decade ago
~ is a unary operator.

Then it should have operated on a and saved the result in the same variable. But why isn't it happening?

Can anyone explain?

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.

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);

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.


Post your comments here:

Your comments will be displayed after verification.