简体   繁体   中英

Bitwise XOR operation in C

I'm learning bitwise operation and i came across a xor operation,

#include<stdio.h>
#include<conio.h>
int main
{
    printf("%d\n",10 ^ 9);
    getch();
    return 0;
}

the binary form of 10 ---> 1 0 1 0 the binary form of 9 ---> 1 0 0 1

So in XOR the output is 1 when one of the input is 1 and other is 0.

So the output of 10 ^ 9 is 0 0 1 1 => 3

So when trying for the -10 ^ 9, I'm getting the output as -1.

#include<stdio.h>
#include<conio.h>
int main
{
    printf("%d\n",-10 ^ 9);
    getch();
    return 0;
}

Can some one explain me how it is -1?

Thanks in advance for all who helps!

Because the operator precedence of XOR is lower than the unary minus.

That is, -10 ^ 9 is equal to (-10) ^ 9 .
-10 ^ 9 is not equal to -(10 ^ 9) .

-10 is 11110110 (2) and 9 is 00001001 (2) .

11110110 (2) XOR 00001001 (2) = 11111111 (2)

11111111 (2) is -1 in 2's complement representation.

Continuing from the comment.

In a two's complement system, negative values are represented by values that are sign-extended to the width of the type. Where 10 is 1010 in binary, the two-complement representation for -10 for a 4-byte integer is:

11111111111111111111111111110110

(which has an unsigned value of 4294967286 )

Now you see what happens when you xor with 9 (binary 1001 ),

  11111111111111111111111111110110
^                             1001
----------------------------------
  11111111111111111111111111111111  (-1 for a signed integer)

The result is 1111 which is sign-extended to 32-bits, or 11111111111111111111111111111111 for a signed int , which is -1 .

Binary representation of negative numbers uses a concept called two's complement . Basically, every bit is first flipped and then you add 1.

For example, the 8-bit representation positive 10 would be 00001010 . To make -10, first you flip the bits: 11110101 , and then you add 1: 11110101 + 1 = 11110110 .

So the binary representation of -10 is therefore 11110110

If you XOR this value with 9, it would look then look like this: 11110110 XOR 00001001 = 11111111 .

11111111 is the two's complement of 1, therefore the final answer is -1.

The minus '-' sign have higher precedence than the xor '^' operator . So first we find the value of -10.

The binary equivalent of 10 is 1010 & representation in terms of 8-bits becomes 0000 1010 .

For signed numbers , we take a 2's complement of 10 . First find 1's complement of 0000 1010

   0000 1010  ----- 1's complement ---- 1111 0101 

Now find 2's complement by adding 1 in 1's complement result .

   1's complement   ---------  1111 0101
   Adding 1         ---------          1
   2's complement   ---------  1111 0110

Now perform -10^9 (XOR operator gives 1 when both bits are different other wise it gives 0)

  -10   -------  1111 0110
    9   -------  0000 1001
 --------------------------
 -10^9  -------  1111 1111

-10^9 = 1111 1111 which is equal to the -1 in signed numbers.

Thats why the output becomes -1 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM