Bits
Bits holds the bitwise operations and wrapping arithmetic over the integer types, which have no operators for them. A signed value is taken as its two's complement bits.
Details
- Where other languages write
a & b,a | b,a ^ b,~a,a << n, anda >> n, Ant calls Bits.and, Bits.or, Bits.xor, Bits.not, Bits.shiftLeft, and Bits.shiftRight. In Ant,|is the pipe. - The two operands of
Bits.and,Bits.or,Bits.xor, and the wrap functions share one integer type. The count of a shift or rotation is always anint, soBits.shiftLeft(uint8(1), 3)needs no conversion. - Negative values take part as their two's complement bits at the width of their type, so
-1has every bit set. - A shift panics when its count is negative or not below the width of the type. A rotation takes its count modulo the width and panics only on a negative count.
+,-, and*panic on overflow. Bits.wrapAdd, Bits.wrapSub, and Bits.wrapMul wrap around instead and never panic.
Basic examples
Combine, mask, and flip bits:
Bits.and(0b1100, 0b1010) // 8
Bits.or(0b1100, 0b1010) // 14
Bits.xor(0b1100, 0b1010) // 6
Bits.not(uint8(0)) // 255Shift and rotate:
Bits.shiftLeft(1, 10) // 1024
Bits.shiftRight(0x1234, 8) // 18
Bits.rotateLeft(uint8(129), 1) // 3
Bits.rotateRight(uint8(1), 1) // 128Count bits:
Bits.popCount(0b1011) // 3
Bits.leadingZeros(uint8(1)) // 7
Bits.trailingZeros(0b1000) // 3Add past the maximum without a panic:
Bits.wrapAdd(uint8(255), uint8(1)) // 0
uint8(255) + uint8(1) // panic: integer overflowScope
The type of the operands sets the width. The same bits give different answers at different widths:
Bits.not(uint8(0)) // 255
Bits.not(uint16(0)) // 65535
Bits.not(0) // -1
Bits.leadingZeros(uint8(1)) // 7
Bits.leadingZeros(1) // 63A right shift copies the sign bit of a signed type and fills an unsigned one with zeros:
Bits.shiftRight(int8(-8), 1) // -4
Bits.shiftRight(uint8(248), 1) // 124Negative values count their two's complement bits:
Bits.popCount(int8(-1)) // 8
Bits.popCount(-1) // 64
Bits.trailingZeros(-8) // 3
Bits.leadingZeros(int8(-1)) // 0A left shift drops the bits pushed past the width; only the count can make it panic:
Bits.shiftLeft(uint8(200), 1) // 144
Bits.shiftLeft(1, 63) // -9223372036854775808Write the bits out with String.fromInt in base 2:
String.fromInt(0b1011, base: 2) // "1011"Zero has no set bit, so both zero counts are the full width:
Bits.leadingZeros(uint16(0)) // 16
Bits.trailingZeros(uint16(0)) // 16The functions chain through a pipe, where the value on the left is the first argument:
0x3366CC | Bits.shiftRight(8) | Bits.and(0xFF) // 102Functions by job
Logic
- Bits.and: the bits set in both
aandb - Bits.or: the bits set in either
aorb - Bits.xor: the bits set in exactly one of
aandb - Bits.not:
awith every bit inverted
Shifts and rotations
- Bits.shiftLeft:
amoved left bynbits, zeros shifted in - Bits.shiftRight:
amoved right bynbits, arithmetic for a signed type and logical for an unsigned one - Bits.rotateLeft, Bits.rotateRight:
aturned bynbits, the bits shifted out coming back in at the other end
Counting
- Bits.popCount: the number of one bits
- Bits.leadingZeros: the zero bits above the highest one bit
- Bits.trailingZeros: the zero bits below the lowest one bit
Wrapping arithmetic
- Bits.wrapAdd, Bits.wrapSub, Bits.wrapMul:
a + b,a - b, anda * b, wrapping around in two's complement instead of panicking on overflow
Applications
Keep a set of permissions as flags in one integer:
let READ = 0b100
let WRITE = 0b010
let EXEC = 0b001
func has(mode: int, flag: int): bool {
Bits.and(mode, flag) != 0
}
let mode = Bits.or(READ, EXEC)
has(mode, READ) // true
has(mode, WRITE) // falseClear a flag by ANDing with its inverse, and toggle one with Bits.xor:
let READ = 0b100
let EXEC = 0b001
let mode = 0b101
Bits.and(mode, Bits.not(EXEC)) // 4
Bits.xor(mode, READ) // 1Pack a color into one integer and take it apart again:
type Rgb = { r: int, g: int, b: int }
func pack(c: Rgb): int {
Bits.shiftLeft(c.r, 16) | Bits.or(Bits.shiftLeft(c.g, 8)) | Bits.or(c.b)
}
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),
}
}
pack(Rgb{ r: 51, g: 102, b: 204 }) // 3368652
unpack(0x3366CC) // Rgb{ r: 51, g: 102, b: 204 }Count the bits that differ between two values, their Hamming distance:
func distance(a: int, b: int): int {
Bits.popCount(Bits.xor(a, b))
}
distance(0b1011, 0b1110) // 2Hash a string with FNV-1a, whose multiplication is meant to overflow:
func fnv1a(s: string): uint32 {
var hash = uint32(2166136261)
for b in s.toBytes() {
hash = Bits.wrapMul(Bits.xor(hash, uint32(b)), uint32(16777619))
}
hash
}
fnv1a("") // 2166136261
fnv1a("ant") // 522836950Properties and relations
A left shift by n multiplies by 2^n while the result fits, and a right shift of a non-negative value divides by it:
var power = 1
for n in Iter.range(0, 8) {
assert(Bits.shiftLeft(3, n) == 3 * power)
assert(Bits.shiftRight(3 * power, n) == 3)
power *= 2
}Bits.xor with the same value twice gives the value back:
let key = 0b1010_0101
for x in [0, 7, 200] {
assert(Bits.xor(Bits.xor(x, key), key) == x)
}A rotation keeps every bit, so it keeps the count of one bits:
let x = uint8(0b1001_0110)
for n in Iter.range(0, 8) {
assert(Bits.popCount(Bits.rotateLeft(x, n)) == Bits.popCount(x))
assert(Bits.rotateRight(Bits.rotateLeft(x, n), n) == x)
}Bits.wrapSub undoes Bits.wrapAdd, even across the wrap:
let a = uint8(250)
let b = uint8(10)
assert(Bits.wrapSub(Bits.wrapAdd(a, b), b) == a)Possible issues
Ant has no & operator, and | is the pipe, not bitwise OR:
let low = 0x1234 & 0xFF // error: unexpected character '&'Integers have no namespace, so a dot call does not reach Bits. Use the namespace form or a pipe:
let flags = 5
let low = flags.and(1) // error: int has no field or function andBoth operands share one 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 expectedA shift by the full width panics instead of giving zero:
Bits.shiftLeft(uint8(1), 8)
// panic: Bits.shiftLeft needs a count from 0 up to but excluding 8, got 8See also
- Math: numeric functions, the integer limits, and the
Integerbound - Bytes: binary data, a mutable sequence of
byte - String.fromInt: an integer's digits in any base, such as base 2 to show its bits
