C Programming - Bitwise Operators - Discussion

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

[A]. ffff
[B]. 0000
[C]. 00ff
[D]. ddfd

Answer: Option A

Explanation:

No answer description available for this question.

Ranjit said: (Aug 20, 2010)  
Can any explain why ~operator has no effect here?

Sundar said: (Aug 20, 2010)  
Hi Ranjit,

The modified value is not stored in 'a' back.

If we write like this a = ~a; we can find the changes by ~ operator.

Hope you understand it. Have a nice day!

Pratik said: (Oct 5, 2010)  
Can you explain in detail how answer is a?

Youssef said: (Jan 28, 2011)  
0xfff = 1111 1111 1111 1111
and ~a is not a=~a
~a dose not affect the value of a

Nirlep said: (Feb 6, 2011)  
~a does not assigned any where yaar
so simple

Naumi said: (Feb 23, 2011)  
It is like Lvalue is not present therr ...Lvalue is a conatiner/variable on left side of the expression.....which would have contained the value of ~a.

Sohan Lal Mits said: (Mar 1, 2011)  
Only ~a does not restored the value in a so finaly output is same as previous as assign.

Nikita said: (Jul 2, 2011)  
Is ~a; a valid statement?

If we write a++ or ++a there is still no left sise variable but it changes 'a' so why not ~a changes 'a'.

Chandu said: (Jul 23, 2011)  
The modified variable(~a) must be assigned to another or same(a) variable. then only value will be changed.

Ex:-
a=~a;

Durgaprasad said: (Aug 21, 2011)  
And also NONE OF THE GIVEN ANSWERS ARE CORRECT... becoz when int size is 2 and print that int it will print ffffffff(8 fs).. I have checked it .. So be careful...

Answer here is ffffffff

Ullas said: (Sep 14, 2011)  
@durgaprasad;;.

Ya you are right but here we just printing what is in a.

So a=ffff and prints its value in printf statement.

I didn't check just telling by observing the code.

Pintu Kumat Singh said: (Oct 14, 2011)  
But what will the value of unsigned int =0xffff;
than what about ox if answer be ffff.

Can any one explain it.

Jailani said: (Oct 15, 2011)  
No matter about 0x it is just a hexadecimal representation and also it is optional.

Krishan said: (Nov 3, 2011)  
@durgaprasad, ff is 1 byte and ffff represents 2 bytes of data. where the hell did u get 8fs?
@nikita, ++a means a=a+1. but ~a means nothing. the L-value is missing and it doesnot give an error.

Niraj said: (Dec 16, 2011)  
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.

Rupinderjit Singh said: (Jan 4, 2012)  
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: (Mar 26, 2012)  
#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.

Sravan said: (Apr 4, 2012)  
Your compiler has been taking four bytes for int so you have got that answer. If you write as a = 0xffffffff.

Then you will get 0.

Rajani said: (May 15, 2012)  
#include<stdio.h>
int main()
{
unsigned int a=0xffff,b;
b=~a;
printf("%x\n",b);
return 0;
}
O/P: now you will get 0
As a=~a is not a valid assignment.

Rishabh said: (Jun 5, 2012)  
What does 0xffff means here?

Sree said: (Jun 8, 2012)  
@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: (Jun 29, 2012)  
Does the operator ~ preserves sign of variable when applying on it ?

Cherry said: (Jun 29, 2012)  
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: (Aug 16, 2012)  
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: (Aug 20, 2012)  
@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: (Sep 3, 2012)  
Yes taruna is right.

Jhalak said: (Mar 22, 2013)  
@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: (Apr 19, 2013)  
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: (Jul 8, 2013)  
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: (Aug 9, 2013)  
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.

Priya said: (Aug 29, 2013)  
~ 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?

Deepak Kumar Dubey said: (Sep 26, 2013)  
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.

Amrit Sharma said: (Dec 28, 2013)  
@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.

Don No 1 said: (Jan 20, 2014)  
Here ~a is used but not assign a value back to a so that a will not be affected.

Poulami said: (Mar 10, 2014)  
What is the meaning of 0x..?

Shan said: (Jul 26, 2014)  
What does 0Xfff represents? can anyone explain clearly?

Anu said: (Aug 16, 2014)  
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.

Gopi Krishna said: (Sep 14, 2014)  
Thank you @Taruna.

I satisfy with you answer. What about two byte compiler?

Risv said: (Oct 29, 2014)  
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.

Zeba Anwar said: (Nov 14, 2014)  
Here a = 0*ffff.
Which can also be written as 0000ffff.

So ~a = ffff0000.
Therefore output will be ffff.

Malreddy said: (Nov 2, 2015)  
0xffff = 1111 1111 1111 1111 it is the value is assigned for a ok.

But he/she given that ~a, it doesn't mean that a=~a.

So there is no change in value of a. So a is print as ffff only.

Ram Khanna said: (Nov 27, 2015)  
As there is unsigned int the value is always positive hence ~ has no effect.

Hope you understood.

Lakshmi said: (Sep 19, 2017)  
Thanks for explaining it.

Mohammed Umar said: (Dec 13, 2017)  
#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?

Prakash said: (Mar 21, 2019)  
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?

Rashmi said: (Aug 18, 2020)  
Here ~ has no effect because ~a is not stored back in the variable.
++ operator itself is a self incrementing operator need not be stored.
But a+1 has to be stored back in a.
Like, a = a+1.

Pavan said: (Sep 2, 2020)  
@All.

Answer for What is the use of 0x.

It always remember ox represent an upcoming number is a constant and hexagonal number to C compiler.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.