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.

Pavan said:   4 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.
(1)

Rashmi said:   4 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.
(2)

Prakash said:   5 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?

Mohammed Umar said:   7 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?

Lakshmi said:   7 years ago
Thanks for explaining it.

Ram khanna said:   9 years ago
As there is unsigned int the value is always positive hence ~ has no effect.

Hope you understood.

Malreddy said:   9 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)

Zeba Anwar said:   10 years ago
Here a = 0*ffff.
Which can also be written as 0000ffff.

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

Risv said:   10 years 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.

Gopi krishna said:   1 decade ago
Thank you @Taruna.

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


Post your comments here:

Your comments will be displayed after verification.