C Programming - Bitwise Operators - Discussion
Discussion Forum : Bitwise Operators - Find Output of Program (Q.No. 8)
8.
What will be the output of the program?
#define P printf("%d\n", -1^~0);
#define M(P) int main()\
{\
P\
return 0;\
}
M(P)
Discussion:
27 comments Page 1 of 3.
Vinod Kr. Rai said:
1 decade ago
Calculate: -1^~0
-1 => 1111 1111 1111 1111
~0 => 1111 1111 1111 1111
--------------------------------
-1^~0 => 0000 0000 0000 0000 => is binary of 0 (zero).
--------------------------------
Firstly find the binary of -1:
1 => 0000 0000 0000 0001
1's compliment of 0000 0000 0000 0001 is:
All 0 is converted into 1 and all 1 into 0, now
1111 1111 1111 1110
Now 2's complement of 1111 1111 1111 1110 is:
Add 1 into 1's complement
1111 1111 1111 1110
+ 0000 0000 0000 0001
----------------------------
1111 1111 1111 1111 => is binary of -1
----------------------------
Now, binary of 0 is:
0 => 0000 0000 0000 0000 => is binary of 0
~0 => to convert all 0 into 1 and all 1 into 0, i.e.
1111 1111 1111 1111 => is binary of ~0
-----------------------------------
^ is called Bitwise XOR operator
0 ^ 0 = 0
0 ^ 1 = 1
1 ^ 0 = 1
1 ^ 1 = 0
------------------------------------
-1 => 1111 1111 1111 1111
~0 => 1111 1111 1111 1111
--------------------------------
-1^~0 => 0000 0000 0000 0000 => is binary of 0 (zero).
--------------------------------
Firstly find the binary of -1:
1 => 0000 0000 0000 0001
1's compliment of 0000 0000 0000 0001 is:
All 0 is converted into 1 and all 1 into 0, now
1111 1111 1111 1110
Now 2's complement of 1111 1111 1111 1110 is:
Add 1 into 1's complement
1111 1111 1111 1110
+ 0000 0000 0000 0001
----------------------------
1111 1111 1111 1111 => is binary of -1
----------------------------
Now, binary of 0 is:
0 => 0000 0000 0000 0000 => is binary of 0
~0 => to convert all 0 into 1 and all 1 into 0, i.e.
1111 1111 1111 1111 => is binary of ~0
-----------------------------------
^ is called Bitwise XOR operator
0 ^ 0 = 0
0 ^ 1 = 1
1 ^ 0 = 1
1 ^ 1 = 0
------------------------------------
(1)
Saiprabha said:
1 decade ago
-1 means it will treated as 2's complement
that means adding 1 to the result of 1's complement.
Binary form of 1 is :
0000 0000 0000 0001
1's :---- 1111 1111 1111 1110
adding 1 then
:----- 1111 1111 1111 1111
and ^ means it will print 0 if both are same(1,1---0)(0,0----0)
otherwise 1 (1,0 -------1)
0's is :-- 1111 1111 1111 1111
now for -1 **** 1111 1111 1111 1111
for ~0 **** 1111 1111 1111 1111
now we will perform the ^ operaiton
1111 1111 1111 1111
1111 1111 1111 1111
-------------------------------------
0000 0000 0000 0000
so the answer is zero
that means adding 1 to the result of 1's complement.
Binary form of 1 is :
0000 0000 0000 0001
1's :---- 1111 1111 1111 1110
adding 1 then
:----- 1111 1111 1111 1111
and ^ means it will print 0 if both are same(1,1---0)(0,0----0)
otherwise 1 (1,0 -------1)
0's is :-- 1111 1111 1111 1111
now for -1 **** 1111 1111 1111 1111
for ~0 **** 1111 1111 1111 1111
now we will perform the ^ operaiton
1111 1111 1111 1111
1111 1111 1111 1111
-------------------------------------
0000 0000 0000 0000
so the answer is zero
Laxman said:
7 years ago
Let you take int i=1;
int is 2-byte range in turbo c, 4 bytes in gcc, now consider turbo c==>2 bytes means 16 bits right so these 16 bits stored in memory for 1 is (0000 0000 0000 0001). Before discoursing about -1; what is 2's complement of x, that is exactly equal to -x. note: negative numbers stored in the memory is 2's comp of a positive number. so we have to find what is 2's comp of 1. 2's comp=1'comp+1;
0000 0000 0000 0001==>1'comp is==>1111 1111 1111 1110 ==>add 1==>1111 1111 1111 1111. So finally the -1 is stored in memory is 1111 1111 1111 1111.
int is 2-byte range in turbo c, 4 bytes in gcc, now consider turbo c==>2 bytes means 16 bits right so these 16 bits stored in memory for 1 is (0000 0000 0000 0001). Before discoursing about -1; what is 2's complement of x, that is exactly equal to -x. note: negative numbers stored in the memory is 2's comp of a positive number. so we have to find what is 2's comp of 1. 2's comp=1'comp+1;
0000 0000 0000 0001==>1'comp is==>1111 1111 1111 1110 ==>add 1==>1111 1111 1111 1111. So finally the -1 is stored in memory is 1111 1111 1111 1111.
Sree said:
1 decade ago
After the First macro function executed
#define P printf("%d\n", -1^~0);
#define M(P) int main()\
{\
P\
return 0;\
}
M(printf("%d\n", -1^~0);)
After the second Macro function executed
#define P printf("%d\n", -1^~0);
#define M(P) int main()\
{\
P\
return 0;\
}
int main()
{
printf("%d\n", -1^~0);
return 0;
}
#define P printf("%d\n", -1^~0);
#define M(P) int main()\
{\
P\
return 0;\
}
M(printf("%d\n", -1^~0);)
After the second Macro function executed
#define P printf("%d\n", -1^~0);
#define M(P) int main()\
{\
P\
return 0;\
}
int main()
{
printf("%d\n", -1^~0);
return 0;
}
Vikas said:
1 decade ago
If an integer on this machine is 2-bytes, why is the answer not 32,766?
0000 0000 0000 0001=1
1111 1111 1111 1110=-1 1's compliment of -1
1111 1111 1111 1111=-1 in 2's compliment
^1111 1111 1111 1111=0 after negation or inverting
........................
0000 0000 0000 0000
so therefore we know that 0^0=0
1^1=0
so the answer is 0(Zero).
thanks frds.
all the best.
0000 0000 0000 0001=1
1111 1111 1111 1110=-1 1's compliment of -1
1111 1111 1111 1111=-1 in 2's compliment
^1111 1111 1111 1111=0 after negation or inverting
........................
0000 0000 0000 0000
so therefore we know that 0^0=0
1^1=0
so the answer is 0(Zero).
thanks frds.
all the best.
Nitin said:
1 decade ago
00000000 00000000 00000000 00000001 = 1
11111111 11111111 11111111 11111110 = -1 in 1's compliment
we now have to add 1 to make it into 2's compliment .
So,
11111111 11111111 11111111 11111111 = -1 in 2's compliment
11111111 11111111 11111111 11111111 (0 after negation or inverting)
for 0^0 = 0
1^1 = 0
So the answer is 0 (zero).
11111111 11111111 11111111 11111110 = -1 in 1's compliment
we now have to add 1 to make it into 2's compliment .
So,
11111111 11111111 11111111 11111111 = -1 in 2's compliment
11111111 11111111 11111111 11111111 (0 after negation or inverting)
for 0^0 = 0
1^1 = 0
So the answer is 0 (zero).
Hareesh said:
1 decade ago
main() is function first it will call , after that printf will work.
Note : Printf function you can write in inside main function.
Like main(printf("hai"))
If anybody ask Printf function how can write without ends with semicolon means this is solution.
Note : Printf function you can write in inside main function.
Like main(printf("hai"))
If anybody ask Printf function how can write without ends with semicolon means this is solution.
(1)
Ishan said:
1 decade ago
Most of the part is correctly explained.
Except ~ is 1s complement not negation
Negation of 0 is negation is logical operation
1s complement is bitwise operation
Negation of 0 will yield 1
But
~0= 1111 1111 1111 1111
Except ~ is 1s complement not negation
Negation of 0 is negation is logical operation
1s complement is bitwise operation
Negation of 0 will yield 1
But
~0= 1111 1111 1111 1111
Rakesh said:
9 years ago
Answer = 0 because,
Binary of -1(in 2 byte) 1111 1111 1111 1111.
Binary of ~0(in 2 byte) 1111 1111 1111 1111.
Now, apply Xor (-1 ^ ~0) = 0000 0000 0000 0000(ans).
Hint:use Xor truth table.
Binary of -1(in 2 byte) 1111 1111 1111 1111.
Binary of ~0(in 2 byte) 1111 1111 1111 1111.
Now, apply Xor (-1 ^ ~0) = 0000 0000 0000 0000(ans).
Hint:use Xor truth table.
(2)
Ishan said:
1 decade ago
\ split the macros into multiple line
For example
#define P(int i) { \
printf("%d",i);\
}
Or
#define P(int i) { printf("%d",i);}
For example
#define P(int i) { \
printf("%d",i);\
}
Or
#define P(int i) { printf("%d",i);}
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers