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 2 of 5.
Anu said:
1 decade ago
Like other unary operator they store the resultant value in operand itself on which the unary operator is applied.
For example a =7 and ++a stores 8 in a so why doesn't ~a stores the result in a.
For example a =7 and ++a stores 8 in a so why doesn't ~a stores the result in a.
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.
Sundar said:
2 decades ago
Hi Ranjit,
The modified value is not stored in 'a' back.
If we write like this a = ~a; we can find the changes by ~ operator.
Hope you understand it. Have a nice day!
The modified value is not stored in 'a' back.
If we write like this a = ~a; we can find the changes by ~ operator.
Hope you understand it. Have a nice day!
Rashmi said:
5 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.
++ 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.
(3)
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
Malreddy said:
10 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.
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)
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.
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
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.
Naumi said:
1 decade ago
It is like Lvalue is not present therr ...Lvalue is a conatiner/variable on left side of the expression.....which would have contained the value of ~a.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers