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

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.

Risv said:   1 decade ago
a = 1111 1111 1111 1111

Let b =~ a, means b = 0000 0000 0000 0000

[
Concept : take composite resultant value is b = (-1)*(a+1).
Here 'a' may be +/- in the form of Decimal value.
]

From above eg:

a = 0xffff.

a = -1 [decimal value of 0xffff].

So due to our concept b =~a => b = (-1)*(-1+1) therefor resultant of b = 0

b = 0 [means hexadecimal value is 0000 0000 0000 0000].

But here from above example:

Line 1: a = 1111 1111 1111 1111

Line 2: ~a = 0000 0000 0000 0000

But value of 'a' does not change in line 2.

So a = 0xffff.

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.

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.

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.

Niraj said:   1 decade ago
In the above program it is printing the value of a, which is ffff.

In that program we are taking the complement of 'a' but we we are not stored it in a. so, it is printing a=ffff.

If it is a=~a;

Then it will print 0 for unsigned int 2 byte wide.

Prakash said:   6 years ago
Hi, I am Prakash.

A has not stored any where so it will not affect that is okay but I will take ex: a=1; and I will increment it to a++ but I have not stored anywhere but in the next time if I print a then why will it get effect?

Rupinderjit Singh said:   1 decade ago
since tidle(~) being a unary operator didn't give LVALUE error,otherwise for ~a to take into effect on output it must have Lvalue in which complemented value should be stored.It;s my assumption not sure about that,

Sonia khanna said:   1 decade ago
#include<stdio.h>

int main()
{
unsigned int a=0xffff;
a=~a;
printf("%x\n", a);
return 0;
}

I'm getting output ffff0000 but it should be 0000. Please someone gives the explanation.

Mohammed Umar said:   8 years ago
#include<stdio.h>

int main()
{
int a=0xffffffff,b;
b=~a;
printf("%x\n", a);
return 0;
}

I have taken this value then also it prints the same. Can anyone explain properly?


Post your comments here:

Your comments will be displayed after verification.