Questions tagged [xor]

For questions involving exclusive-or operations (typically bitwise).

Exclusive-disjunction or exclusive-or (XOR; operator ⊕) is a logical operation like AND and OR, having the following truth table:

0 ⊕ 1 == 1 ⊕ 0 == 1 # different inputs produce 1/True
0 ⊕ 0 == 1 ⊕ 1 == 0 # equal inputs produce 0/False

XOR operations are useful for parity (eg providing the +1 redundancy in a RAID-5 disk configuration), swapping registers in CPUs without SWAP instructions, graphic overlays, cryptographic functions etc.

Resources

1618 questions
296
votes
19 answers

Creating a "logical exclusive or" operator in Java

Observations: Java has a logical AND operator. Java has a logical OR operator. Java has a logical NOT operator. Problem: Java has no logical XOR operator, according to sun. I would like to define one. Method Definition: As a method it is simply…
eleven81
  • 6,301
  • 11
  • 37
  • 48
283
votes
20 answers

Why is there no logical XOR?

Why is there no logical XOR in JavaScript?
DarkLightA
  • 14,980
  • 18
  • 49
  • 57
190
votes
9 answers

Why is XOR the default way to combine hashes?

Say you have two hashes H(A) and H(B) and you want to combine them. I've read that a good way to combine two hashes is to XOR them, e.g. XOR( H(A), H(B) ). The best explanation I've found is touched briefly here on these hash function…
Nate Murray
  • 3,841
  • 5
  • 32
  • 33
163
votes
4 answers

What is inverse function to XOR?

There is XOR function in Java - a^b For exemple: 5^3 = 6 Can you tell me inverse function? If I have 6 and 3 can i get range of numbers which include number 5?
A.N.R.I
  • 1,931
  • 2
  • 15
  • 20
161
votes
12 answers

Is it good practice to use the xor operator for boolean checks?

I personally like the exclusive or, ^, operator when it makes sense in the context of boolean checks because of its conciseness. I much prefer to write if (boolean1 ^ boolean2) { //do it } than if((boolean1 && !boolean2) || (boolean2 &&…
Peter
  • 29,498
  • 21
  • 89
  • 122
106
votes
12 answers

Conditional XOR?

How come C# doesn't have a conditional XOR operator? Example: true xor false = true true xor true = false false xor false = false
Gilad Naaman
  • 6,390
  • 15
  • 52
  • 82
103
votes
2 answers

Is there no XOR operator for booleans in golang?

Is there no XOR operator for booleans in golang? I was trying to do something like b1^b2 but it said it wasn't defined for booleans.
Charlie Parker
  • 5,884
  • 57
  • 198
  • 323
85
votes
12 answers

How does XOR variable swapping work?

Can someone explain to me how XOR swapping of two variables with no temp variable works? void xorSwap (int *x, int *y) { if (x != y) { *x ^= *y; *y ^= *x; *x ^= *y; } } I understand WHAT it does, but can someone walk…
mmcdole
  • 91,488
  • 60
  • 186
  • 222
77
votes
4 answers

Why does swapping values with XOR fail when using this compound form?

I found this code to swap two numbers without using a third variable, using the XOR ^ operator. Code: int i = 25; int j = 36; j ^= i; i ^= j; j ^= i; Console.WriteLine("i:" + i + " j:" + j); //numbers Swapped correctly //Output: i:36…
Javed Akram
  • 15,024
  • 26
  • 81
  • 118
74
votes
8 answers

What does bitwise XOR (exclusive OR) mean?

I'm trying to understand the binary operators in C# or in general, in particular ^ - exclusive or. For example: Given an array of positive integers. All numbers occur even number of times except one number which occurs odd number of times. Find the…
DarthVader
  • 52,984
  • 76
  • 209
  • 300
67
votes
8 answers

T-SQL XOR Operator

Is there an XOR operator or equivalent function in SQL Server (T-SQL)?
ses011
  • 1,216
  • 1
  • 9
  • 15
59
votes
13 answers

What's wrong with XOR encryption?

I wrote a short C++ program to do XOR encryption on a file, which I may use for some personal files (if it gets cracked it's no big deal - I'm just protecting against casual viewers). Basically, I take an ASCII password and repeatedly XOR the…
Paul
  • 6,435
  • 4
  • 34
  • 45
56
votes
7 answers

XOR operation with two strings in java

How to do bitwise XOR operation to two strings in java.
yasitha
  • 709
  • 2
  • 6
  • 12
56
votes
4 answers

Why are XOR often used in java hashCode() but another bitwise operators are used rarely?

I often see code like int hashCode(){ return a^b; } Why XOR?
Andrei N
  • 4,716
  • 4
  • 29
  • 29
54
votes
6 answers

Two elements in array whose xor is maximum

Given an array of integers ,You have to find two elements whose XOR is maximum. There is naive approach --just by picking each element and xoring with other elements and then comparing the results to find the pair. Other than this ,Is there any…
Anil Arya
  • 3,100
  • 7
  • 43
  • 69
1
2 3
99 100