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.

Rashmi said:   5 years ago
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.
(3)

Pavan said:   5 years ago
@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.
(2)

Malreddy said:   10 years ago
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.
(1)

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

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.

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.