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.

ULLAS said:   1 decade ago
@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:   1 decade ago
But what will the value of unsigned int =0xffff;
than what about ox if answer be ffff.

Can any one explain it.

Jailani said:   1 decade ago
No matter about 0x it is just a hexadecimal representation and also it is optional.

Krishan said:   1 decade ago
@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:   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.

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.

Sravan said:   1 decade ago
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:   1 decade ago
#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:   1 decade ago
What does 0xffff means here?


Post your comments here:

Your comments will be displayed after verification.