Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# bitwise XOR (^) compared to Java bitwise XOR (^)

Tags:

java

c#

I'm trying to convert some java code to C# and it's been working flawlessly so far but I've encountered an issue with the ^ operator. In C# Console.WriteLine(127 ^ 0xffffffff); prints 4294967168 whereas in Java System.out.println(127 ^ 0xffffffff); prints -128. I've been looking around to see if there is something else that I need to use instead but I haven't come across anything.

like image 832
Orion Avatar asked Sep 18 '15 15:09

Orion


1 Answers

C# supports signed as well as unsigned integers (Java supports signed ones only):

  unchecked {
    // you want signed int
    int result = (int) (127 ^ 0xffffffff);

    Console.WriteLine(result);
  }
like image 150
Dmitry Bychenko Avatar answered Sep 22 '22 13:09

Dmitry Bychenko