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;
}
Discussion:
47 comments Page 3 of 5.
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.
Madhukar said:
1 decade ago
Yes taruna is right.
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
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.
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.
Cherry said:
1 decade ago
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.
Gowda said:
1 decade ago
Does the operator ~ preserves sign of variable when applying on it ?
Sree said:
1 decade ago
@Rishabh: 0Xffff means 1111 1111 1111 1111. u knew digital systems rite? In that how will u specify 15 in hexadecimal form? 'F' na?
Rishabh said:
1 decade ago
What does 0xffff means here?
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.
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.
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.
Then you will get 0.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers