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 5 of 5.
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.
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.
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.
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?
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?
Gowda said:
1 decade ago
Does the operator ~ preserves sign of variable when applying on it ?
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.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers