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 4 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.
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,
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.
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.
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.
@nikita, ++a means a=a+1. but ~a means nothing. the L-value is missing and it doesnot give an error.
Jailani said:
1 decade ago
No matter about 0x it is just a hexadecimal representation and also it is optional.
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.
than what about ox if answer be ffff.
Can any one explain it.
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.
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.
Durgaprasad said:
1 decade ago
And also NONE OF THE GIVEN ANSWERS ARE CORRECT... becoz when int size is 2 and print that int it will print ffffffff(8 fs).. I have checked it .. So be careful...
Answer here is ffffffff
Answer here is ffffffff
Chandu said:
1 decade ago
The modified variable(~a) must be assigned to another or same(a) variable. then only value will be changed.
Ex:-
a=~a;
Ex:-
a=~a;
Nikita said:
1 decade ago
Is ~a; a valid statement?
If we write a++ or ++a there is still no left sise variable but it changes 'a' so why not ~a changes 'a'.
If we write a++ or ++a there is still no left sise variable but it changes 'a' so why not ~a changes 'a'.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers