C Programming - Bitwise Operators - Discussion

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)

[A]. 1
[B]. 0
[C]. -1
[D]. 2

Answer: Option B

Explanation:

No answer description available for this question.

Ankit Anand said: (Jul 9, 2010)  
10000000 00000000 00000000 00000001
11111111 11111111 11111111 11111111 (after negation or inverting)

for 0^0 = 0
1^1 = 0

So the answer is 0 (zero).

Kumar said: (Jul 9, 2010)  
Hi Ankit Anand,

Thank you very much for your kind explanation.

Can you please explain, how the program will look after preprocessing (Phase I of compilation) ?

Thanks in advance.

Sree said: (Nov 25, 2010)  
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;
}

Naveen said: (Dec 17, 2010)  
Ankit anand your answer is correct. But one small mistake. you assign integer as 4 bytes but integer only having 2 bytes

Madhureddy said: (Feb 5, 2011)  
@Naveen

Ankit is correct only, because C is a compiler dependent language, int occupies 2 bytes in Turbo C compiler, where as GCC is conncern it allots 4-bytes to integer.

Dough said: (Jun 29, 2011)  
If an integer on this machine is 2-bytes, why is the answer not 32,766?

i.e.
1000 0000 0000 0001 (-1)
1111 1111 1111 1111 (65,535)

Bitwise XORed together:
0111 1111 1111 1110 (32,766)

Nitin said: (Jul 7, 2011)  
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).

Nandu said: (Jan 2, 2012)  
@Nitin you are correct.

Vikas said: (Feb 16, 2012)  
@Nitin

You are so intelligent.

Vikas said: (Feb 16, 2012)  
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.

Vinod Kr. Rai said: (Jun 21, 2012)  
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
------------------------------------

Jessie said: (Aug 30, 2012)  
@Nitin is correct.

Saiprabha said: (Sep 6, 2012)  
-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

Suman said: (Oct 2, 2012)  
What are those forward slashes in the program doing?

Ishan said: (Nov 4, 2012)  
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

Ishan said: (Nov 4, 2012)  
\ split the macros into multiple line
For example

#define P(int i) { \
printf("%d",i);\
}

Or

#define P(int i) { printf("%d",i);}

Shiv said: (Jul 23, 2013)  
I'm very confuse, what and how exactly program is executing please explain compilation and execution phase?

Abcd said: (Sep 20, 2013)  
Please explain multiple macros how to split?

Madhav said: (Sep 21, 2013)  
BY using \ we can use multiple line macro, it indicates that first line is continued.

Yogesh Kumar said: (Sep 28, 2013)  
Can anyone ans me how the execution is reaching to PRINTF block as main is defined after it?

Hareesh said: (Apr 10, 2015)  
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.

Rakesh said: (Feb 5, 2017)  
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.

Xyz said: (Oct 26, 2017)  
Please explain me why \ is used?

Ramesharavind said: (Nov 21, 2017)  
@Vinod.

Your explanation is good, Thanks.

Yasaswini said: (Jan 21, 2018)  
Can anyone give the representation of 1 and -1 in binary digits?

Laxman said: (Jan 23, 2019)  
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.

Nj Nath said: (Mar 29, 2020)  
^ is Bitwise XOR operator, perform -1 XOR -1, the answer will be 0000.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.