Bits.xor
func Bits.xor[T: Integer](a: T, b: T): TBits.xor returns the bitwise exclusive OR of a and b.
Details
- A bit of the result is set when it is set in exactly one of
aandb. - Negative values take part as their two's complement bits, so XOR with
-1inverts every bit. - It never panics.
Basic examples
Keep the bits that differ between two numbers:
Bits.xor(0b1100, 0b1010) // 6
Bits.xor(12, 10) // 6
Bits.xor(7, 7) // 0Show the result in binary with String.fromInt:
String.fromInt(Bits.xor(0b1100, 0b1010), base: 2) // "110"Scope
Every integer type works, and the result keeps its type:
Bits.xor(byte(0xFF), byte(0x0F)) // 240
Bits.xor(uint16(0x1234), uint16(0xFF)) // 4811
Bits.xor(int8(-128), int8(127)) // -1
Bits.xor(uint64(1), Math.maxUint64) // 18446744073709551614Negative values are two's complement. XOR with -1 flips every bit, and the sign bit flips like any other:
Bits.xor(-1, 5) // -6
Bits.xor(Math.minInt, Math.maxInt) // -1
Bits.xor(-8, 3) // -5In a pipe, the value on the left becomes a:
0b0110 | Bits.xor(0b0011) // 5Pass Bits.xor as a function value to combine an array:
[0b0001, 0b0011, 0b0111].reduce(0, Bits.xor) // 5Applications
Toggle a flag: the first call sets it, the second clears it, and the other bits stay:
let bold = 0b001
let italic = 0b010
var style = bold
style = Bits.xor(style, italic)
println(style)
style = Bits.xor(style, italic)
println(style)3
1Find the one value without a pair, since each pair cancels out:
[4, 9, 7, 4, 7].reduce(0, Bits.xor) // 9Count the bits in which two values differ, their Hamming distance, with Bits.popCount:
func distance(a: int, b: int): int {
Bits.popCount(Bits.xor(a, b))
}
distance(0b1011, 0b1001) // 1
distance(0, 255) // 8
distance(42, 42) // 0Tell whether two integers have opposite signs: the sign bit of their XOR is set exactly then:
func oppositeSigns(a: int, b: int): bool {
Bits.xor(a, b) < 0
}
oppositeSigns(5, -3) // true
oppositeSigns(-5, -3) // false
oppositeSigns(0, 3) // falseScramble bytes with a key and get them back with the same key:
func scramble(data: Bytes, key: byte): Bytes {
data.toArray().map({ Bits.xor(x, key) }) | Bytes.fromArray()
}
let key = byte(0x5A)
let secret = "Ant".toBytes() | scramble(key)
secret.toArray() // [27, 52, 46]
secret | scramble(key) | String.fromBytes() // "Ant"Properties and relations
Bits.xor is commutative and associative, 0 changes nothing, and a value XORed with itself is 0:
for [a, b, c] in [[12, 10, 3], [-8, 3, 0], [0, 255, -1]] {
assert(Bits.xor(a, b) == Bits.xor(b, a))
assert(Bits.xor(Bits.xor(a, b), c) == Bits.xor(a, Bits.xor(b, c)))
assert(Bits.xor(a, 0) == a)
assert(Bits.xor(a, a) == 0)
}XOR with -1 is Bits.not, and XOR is the Bits.or bits without the Bits.and bits:
for [a, b] in [[12, 10], [-8, 3], [Math.minInt, 1]] {
assert(Bits.xor(a, -1) == Bits.not(a))
assert(Bits.xor(a, b) == Bits.and(Bits.or(a, b), Bits.not(Bits.and(a, b))))
}Applying the same key twice undoes it, at every integer type:
let x = uint8(0b1010_0110)
let k = uint8(0b0011_1100)
assert(Bits.xor(Bits.xor(x, k), k) == x)Possible issues
Ant has no ^ operator; bitwise XOR is this function:
let x = 5 ^ 3 // error: unexpected character '^'Bits.xor is not in the namespace of int, so a dot call does not reach it. Call it in namespace form or in a pipe:
let x = 5
let y = x.xor(3) // error: int has no field or function xorlet x = 5
x | Bits.xor(3) // 6Both arguments need the same type. An integer literal is always an int, so convert it:
let b = byte(0xF0)
let y = Bits.xor(b, 1) // error: cannot use int where byte is expectedlet b = byte(0xF0)
Bits.xor(b, byte(1)) // 241A named primitive type is not a member of Integer. Unwrap it, then tag the result again:
type Flags = int
let f = Bits.xor(Flags(1), Flags(4))
// error: T would be Flags, which is not a member of its bound Integer; unwrap it with int(...)type Flags = int
let f = Flags(5)
Flags(Bits.xor(int(f), 4)) // Flags(1)bool is not an integer. For exclusive OR of two bool values, compare them with !=:
let t = Bits.xor(true, false) // error: T would be bool, which is not a member of its bound Integertrue != false // true
true != true // falseSee also
Bits.and · Bits.or · Bits.not · Bits.popCount · Bits namespace
