Questions tagged [bitwise-xor]

Anything related to the bitwise-XOR operation, i.e. a binary operation carried out on two integer operands whose result is obtained performing the logical XOR between each pair of corresponding bits in the operands.

Anything related to the bitwise-XOR operation, i.e. a binary operation carried out on two integer operands whose result is obtained performing the logical XOR between each pair of corresponding bits in the operands.

170 questions
56
votes
10 answers

Can XOR of two integers go out of bounds?

I had been studying the algorithm for finding lonely integers in an array, and here is the implementation: int arr[] = {10, 20, 30, 5, 20, 10, 30}; int LonelyInteger = 0; for(int i=0; i< 7; i++) { LonelyInteger = LonelyInteger ^ arr[i]; } The…
Expert Novice
  • 1,943
  • 4
  • 22
  • 47
16
votes
7 answers

Why does negating a value change the result when XORing it with 1?

I know the working of XOR, Console.WriteLine(1^1); // returns 0 results to 00000001 00000001 -------- 00000000 but how does this return 2? Console.WriteLine(-(-1^1)); // returns 2
Toshi
  • 2,532
  • 4
  • 17
  • 45
14
votes
3 answers

Find a number with even number of occurrences

Given an array where number of occurrences of each number is odd except one number whose number of occurrences is even. Find the number with even occurrences. e.g. 1, 1, 2, 3, 1, 2, 5, 3, 3 Output should be: 2 The below are the…
Green goblin
  • 9,898
  • 13
  • 71
  • 100
12
votes
2 answers

recent Google interview puzzle on bitwise operation

This is a recent interview question from Google: We define f(X, Y) as number of different corresponding bits in binary representation of X and Y. For example, f(2, 7) = 2, since binary representation of 2 and 7 are 010 and 111, respectively.…
pankaj
  • 1,316
  • 3
  • 16
  • 27
11
votes
2 answers

Computing the Parity

I don't fully understand this algorithm of calculating the parity bit. Can someone please explain in detail? The following code is taken from the 'Hacker's Delight' book: int parity(unsigned x) { unsigned y; y = x ^ (x >> 1); y = y ^ (y >>…
Andrey
  • 111
  • 1
  • 1
  • 3
10
votes
6 answers

What is C# exclusive or `^` usage?

Can anyone explain this operator with a good example? I know what this operator is. I mean a real-life example.
Navid Rahmani
  • 7,848
  • 9
  • 39
  • 57
10
votes
1 answer

Java string comparison using bitwise xor

I came across the below code snippet in a product's code. It is using bitwise XOR for string comparison. Is this better than the String.equals(Object o) method? What is the author trying to achieve here? private static boolean compareSecure(String…
Abhishek
  • 1,130
  • 1
  • 12
  • 25
8
votes
9 answers

Xor encryption in PHP

I'm new to Xor encryption, and I'm having some trouble with the following code: function xor_this($string) { // Let's define our key here $key = ('magic_key'); // Our plaintext/ciphertext $text =$string; // Our output text $outText = ''; …
emersonthis
  • 32,822
  • 59
  • 210
  • 375
8
votes
5 answers

How does Xor work in swapping values?

Here is the original code: public static String reverseString(String s){ if(s == null) return ""; char[] rev = s.toCharArray(); int i = 0, j = s.length() - 1; while(i < j) { rev[i] ^= rev[j]; rev[j] ^=…
scionx
  • 167
  • 1
  • 6
7
votes
2 answers

Why can't you xor bytes objects in python?

I think I understand python bytes objects, but supporting bitwise operations on byte strings seems like such an obvious feature. I don't understand why it is not supported. >>>'abcdefg'.encode('ascii') b'abcdefg' Okay. I went from a string to…
cowlicks
  • 1,037
  • 1
  • 12
  • 20
7
votes
2 answers

Implementing XOR function with Prolog CLPFD for 32-bit numbers

I try to implement efficient exclusive-or (XOR) in Prolog CLPFD. This should be simple predicate like: xor(A, B, AxorB). A, B, AxorB are natural numbers (with 0) and AxorB is a result of A xor B. My main problem is with efficiency. Firstly, I…
Grzegorz Adam Kowalski
  • 5,243
  • 3
  • 29
  • 40
6
votes
2 answers

Order of precedence in sequential XOR operations

I have the following expression to calculate a parity bit: AB0E XOR 73C9 XOR D46A XOR 06E3 How would this be evaluated? Is it: (((AB0E XOR 73C9) XOR D46A) XOR 06E3) or is it: (AB0E XOR (73C9 XOR (D46A XOR 06E3)))
rdasxy
  • 1,641
  • 4
  • 16
  • 22
6
votes
3 answers

Xoring Two characters in PHP?

I was reading about xor in php , and I saw this example: $aa = "A" ^ "}"; echo $aa; // < So I search in internet about how it works and they said its sum the ascii code , but the above code not sum. A = 65 } = 125 65 + 125 = 190 I tried to do…
learn99
  • 93
  • 6
6
votes
1 answer

Efficient algorithm to find whether a subset of an integer array exists,the xor of all its elements is a given value?

I have a positive integer array- {1,5,8,2,10} and a given value 7. I need to find whether a subset of the array exists such that the XOR of its elements is the value 7. In this case the subset is {5,2} because 5 xor 2 is 7. One naive solution is to…
user3522401
  • 229
  • 2
  • 10
6
votes
2 answers

Simplify (a + b) XOR (c + b)

Is it possible to simplify (a+b)xor(c+b)? What is the contribution of b to the final result? Note that I'm mixing boolean algebra with arithmetic, xor is a bitwise exclusive or on corresponding bits and + is a standard addition on 8 bits, that wraps…
Cano64
  • 237
  • 1
  • 2
  • 8
1
2 3
11 12