Bits.and
func Bits.and[T: Integer](a: T, b: T): TBits.and returns the bitwise AND of a and b.
Details
- Each bit of the result is set when that bit is set in both
aandb. - Negative values take part as their two's complement bits, so
-1has every bit set.
Basic examples
Keep the bits that are set in both numbers:
Bits.and(0b1100, 0b1010) // 8
Bits.and(12, 10) // 8Mask off all but the low byte:
Bits.and(0x1234, 0xFF) // 52Scope
Every integer type works, with both arguments of that type:
Bits.and(uint8(0xF0), uint8(0x3C)) // 48
Bits.and(int8(-1), int8(5)) // 5
Bits.and(uint64(6), uint64(3)) // 2Negative numbers use their two's complement bits. -1 keeps every bit of the other operand, and -8 clears the low three:
Bits.and(-1, 42) // 42
Bits.and(-8, 7) // 0
Bits.and(-8, 29) // 24
Bits.and(Math.maxInt, -1) // 9223372036854775807Zero clears everything:
Bits.and(0, 99) // 0Call it in a pipe, where the value on the left becomes a:
0x1234 | Bits.and(0xFF) // 52Call it inside a lambda to test each element:
[1, 2, 3, 4, 5, 6].filter({ Bits.and(x, 1) == 0 }) // [2, 4, 6]Named arguments
Name the arguments when the mask should stand out:
Bits.and(a: 0b0110, b: 0b0011) // 2Applications
Test whether a flag is set in a set of flags:
let READ = 0b100
let WRITE = 0b010
let EXEC = 0b001
func has(flags: int, flag: int): bool {
Bits.and(flags, flag) != 0
}
let mode = Bits.or(READ, EXEC)
has(mode, READ) // true
has(mode, WRITE) // falseCheck that all the flags of a mask are set, not only one of them:
let required = 0b0110
Bits.and(0b0111, required) == required // true
Bits.and(0b0101, required) == required // falseSplit a color packed as 0xRRGGBB into its channels:
type Rgb = { r: int, g: int, b: int }
func unpack(color: int): Rgb {
Rgb{
r: color | Bits.shiftRight(16) | Bits.and(0xFF),
g: color | Bits.shiftRight(8) | Bits.and(0xFF),
b: color | Bits.and(0xFF),
}
}
unpack(0x3366CC) // Rgb{ r: 51, g: 102, b: 204 }Round a size down to a multiple of a power of two by clearing its low bits:
func alignDown(n: int, align: int): int {
Bits.and(n, Bits.not(align - 1))
}
alignDown(1000, 256) // 768
alignDown(4096, 256) // 4096Tell a power of two from other positive numbers: it has one bit set, which n - 1 clears:
func isPowerOfTwo(n: int): bool {
n > 0 && Bits.and(n, n - 1) == 0
}
[1, 6, 64, 100].map(isPowerOfTwo) // [true, false, true, false]Properties and relations
The order and grouping of the operands do not matter:
let a = 0b1101
let b = 0b0111
let c = 0b1011
assert(Bits.and(a, b) == Bits.and(b, a))
assert(Bits.and(Bits.and(a, b), c) == Bits.and(a, Bits.and(b, c)))ANDing a value with itself or with -1 gives the value back, and with 0 gives 0:
for x in [-5, 0, 7, 300] {
assert(Bits.and(x, x) == x)
assert(Bits.and(x, -1) == x)
assert(Bits.and(x, 0) == 0)
}The result has no more set bits than either operand:
let a = 0b1101
let b = 0b0111
let both = Bits.popCount(Bits.and(a, b))
assert(both <= Bits.popCount(a) && both <= Bits.popCount(b))It is Bits.or with every bit inverted on both sides:
let a = 0b1101
let b = 0b0111
assert(Bits.and(a, b) == Bits.not(Bits.or(Bits.not(a), Bits.not(b))))Possible issues
Ant has no & operator; bitwise AND is this function:
let low = 0x1234 & 0xFF // error: unexpected character '&'An integer is not reached through ., since numbers have no namespace. Call Bits.and or pipe into it:
let flags = 5
let low = flags.and(1) // error: int has no field or function andBoth arguments must have the same type. An integer literal is an int, so convert it:
let b = uint8(0xF0)
let low = Bits.and(b, 0x0F) // error: cannot use int where byte is expectedlet b = uint8(0xF0)
Bits.and(b, uint8(0x0F)) // 0A float is not an integer:
let x = Bits.and(6.0, 3.0) // error: T would be float64, which is not a member of its bound IntegerA named primitive type is not a member of Integer, so AND its payload and tag the result:
type Perm = int
let p = Perm(0o755)
let owner = Bits.and(p, Perm(0o700))
// error: T would be Perm, which is not a member of its bound Integer; unwrap it with int(...)type Perm = int
let p = Perm(0o755)
Perm(Bits.and(int(p), 0o700)) // Perm(448)See also
- Bits.or, Bits.xor, and Bits.not: the other bitwise operations.
- Bits.shiftRight: move the bits before masking them.
- Bits.popCount: count the bits a mask keeps.
