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.
Sohan lal mits said:
1 decade ago
Only ~a does not restored the value in a so finaly output is same as previous as assign.
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'.
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;
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
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.
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.
Ranjit said:
2 decades ago
Can any explain why ~operator has no effect here?
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.
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.
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,
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers