Bits.wrapSub
func Bits.wrapSub[T: Integer](a: T, b: T): TBits.wrapSub returns a - b, wrapping around in two's complement instead of panicking on overflow.
Details
- The result is
a - breduced to the width ofT: below the type's minimum it wraps around to the top, and above its maximum to the bottom. It never panics.
Basic examples
Subtract below zero in a uint8 and wrap around to the top:
Bits.wrapSub(uint8(0), uint8(1)) // 255Without overflow it is ordinary subtraction:
Bits.wrapSub(10, 3) // 7Scope
Every integer type works, with both arguments of that type. The width decides where it wraps:
Bits.wrapSub(uint16(3), uint16(5)) // 65534
Bits.wrapSub(uint32(10), uint32(20)) // 4294967286
Bits.wrapSub(uint64(0), uint64(1)) // 18446744073709551615Signed types wrap from the minimum to the maximum, and back:
Bits.wrapSub(int8(-128), int8(1)) // 127
Bits.wrapSub(int8(127), int8(-1)) // -128
Bits.wrapSub(Math.minInt, 1) // 9223372036854775807
Bits.wrapSub(Math.maxInt, -1) // -9223372036854775808Negating the minimum gives the minimum back, since its positive does not fit:
Bits.wrapSub(0, Math.minInt) // -9223372036854775808Call it in a pipe, where the value on the left becomes a:
uint8(5) | Bits.wrapSub(uint8(10)) // 251Applications
Measure the time between two readings of a 32-bit tick counter, even after the counter wrapped around between them:
let start = uint32(4294967290)
let now = uint32(4)
Bits.wrapSub(now, start) // 10Compare sequence numbers that wrap: a comes after b when the distance from b forward to a is less than half the range:
func isAfter(a: uint16, b: uint16): bool {
let d = Bits.wrapSub(a, b)
d != uint16(0) && d < uint16(32768)
}
isAfter(uint16(2), uint16(65530)) // true
isAfter(uint16(65530), uint16(2)) // false
isAfter(uint16(7), uint16(7)) // falseCompute a checksum byte that makes the bytes of a record sum to zero, as Intel HEX does:
func checksum(data: Array[byte]): byte {
let sum = data.reduce(byte(0), Bits.wrapAdd)
Bits.wrapSub(byte(0), sum)
}
let record = [0x02, 0x00, 0x00, 0x04, 0xFF, 0xFF].map(byte)
checksum(record) // 252Properties and relations
Adding b back with Bits.wrapAdd undoes the subtraction:
for a in [uint8(0), uint8(7), uint8(255)] {
for b in [uint8(0), uint8(9), uint8(200)] {
assert(Bits.wrapAdd(Bits.wrapSub(a, b), b) == a)
}
}Subtracting b is adding its wrapped negation, Bits.wrapSub(0, b):
let a = int8(100)
let b = int8(-100)
assert(Bits.wrapSub(a, b) == Bits.wrapAdd(a, Bits.wrapSub(int8(0), b)))When the difference fits in the type, it equals the - operator:
for a in [-50, 0, 1000] {
assert(Bits.wrapSub(a, 7) == a - 7)
assert(Bits.wrapSub(a, a) == 0)
}Negating with Bits.wrapSub(0, x) is inverting the bits and adding one, the two's complement:
for x in [int8(-128), int8(-1), int8(0), int8(42)] {
assert(Bits.wrapSub(int8(0), x) == Bits.wrapAdd(Bits.not(x), int8(1)))
}Possible issues
The - operator panics on overflow, including an unsigned result below zero. Use Bits.wrapSub when wrapping is what you want:
uint8(0) - uint8(1) // panic: integer overflowOn an unsigned type the result is never negative. Converting it to the signed type of the same width panics when it is out of range:
int8(Bits.wrapSub(uint8(3), uint8(5))) // panic: 254 out of range for int8Both arguments must have the same type. An integer literal is an int, so convert it:
let b = uint8(3)
let d = Bits.wrapSub(b, 5) // error: cannot use int where byte is expectedA number has no namespace, so a dot call on it finds no function. Call Bits.wrapSub(n, 1) or pipe into it:
let n = 5
let d = n.wrapSub(1) // error: int has no field or function wrapSubA float is not an integer:
let d = Bits.wrapSub(1.5, 0.5)
// error: T would be float64, which is not a member of its bound IntegerA named primitive type is not a member of Integer, so subtract its payload and tag the result:
type Tick = uint32
let t = Tick(uint32(5))
let d = Bits.wrapSub(t, Tick(uint32(7)))
// error: T would be Tick, which is not a member of its bound Integer; unwrap it with uint32(...)type Tick = uint32
let t = Tick(uint32(5))
Tick(Bits.wrapSub(uint32(t), uint32(7))) // Tick(4294967294)See also
- Bits.wrapAdd and Bits.wrapMul: the other wrapping operations.
- Bits.not: inverts the bits, which with one added negates a value.
- Bits: the other bitwise operations.
