0

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!

Jayakumar
  • 153
  • 1
  • 13
  • Your `-10` sign-extends. (which is why bitwise operations are generally done on *unsigned* types) – David C. Rankin Feb 20 '18 at 04:56
  • Possible duplicate of [Meaning of bitwise and(&) of a positive and negative number?](https://stackoverflow.com/questions/15395317/meaning-of-bitwise-and-of-a-positive-and-negative-number) – BadZen Feb 20 '18 at 04:57
  • 2
    What did you expect the answer to be? If you want -3, you could use brackets like `-(10 ^ 9)`, but I'm not sure what you are really wanting to do. – Ken Y-N Feb 20 '18 at 04:59
  • Possible duplicate of [How does C++ do bitwise "or" operations on negative numbers?](https://stackoverflow.com/questions/14326900/how-does-c-do-bitwise-or-operations-on-negative-numbers) – tech_enthusiast Feb 20 '18 at 04:59
  • It is a duplicate question except that here its a c language and the post has c++, but the answer is already explained. So, do your search before asking a question ! – tech_enthusiast Feb 20 '18 at 05:00
  • @DavidC.Rankin yes i get the sign bit is present, can you explain more? on how the bits will be shown? – Jayakumar Feb 20 '18 at 05:00
  • @JModi ok thank you – Jayakumar Feb 20 '18 at 05:02

4 Answers4

7

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.

Naetmul
  • 14,544
  • 8
  • 57
  • 81
2

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.

David C. Rankin
  • 81,885
  • 6
  • 58
  • 85
  • Thank you so much ! – Jayakumar Feb 20 '18 at 05:26
  • Sure, glad to help. To convert from positive to negative representations in a two's compliment system, you simply use bitwise negation (flip all bits) in the positive number and add `1`. (to get back, you just do it again) – David C. Rankin Feb 20 '18 at 05:30
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.

0

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 .

Usman
  • 1,983
  • 15
  • 28