Bits.shiftLeft
func Bits.shiftLeft[T: Integer](a: T, n: int): TBits.shiftLeft returns a shifted left by n bits, filling with zeros. The bits shifted past the width of T are dropped, into the sign bit too, and never overflow. It panics if n < 0 or n is at least the bit width of T.
Details
- Bits shifted past the top of the type are dropped, so the result never panics on overflow, unlike
*. For a signed type, a bit shifted into the sign position makes the result negative.
Basic examples
Shift 1 left by 4 bits, which multiplies it by 2⁴:
Bits.shiftLeft(1, 4) // 16
Bits.shiftLeft(3, 2) // 12The result has the type of a:
Bits.shiftLeft(byte(1), 7) // 128Scope
A count of zero returns a unchanged:
Bits.shiftLeft(5, 0) // 5Bits that leave the top of the type are lost; zeros come in at the bottom:
Bits.shiftLeft(byte(255), 4) // 240
Bits.shiftLeft(uint16(0x1234), 8) // 13312On a signed type the sign bit is an ordinary bit, so a shift can change the sign:
Bits.shiftLeft(int8(64), 1) // -128
Bits.shiftLeft(-3, 2) // -12
Bits.shiftLeft(int32(1), 31) // -2147483648The largest count is one less than the width of the type:
Bits.shiftLeft(1, 63) // -9223372036854775808
Bits.shiftLeft(uint64(1), 63) // 9223372036854775808In a pipe, the value is the first argument:
1 | Bits.shiftLeft(10) // 1024The arguments can be named:
Bits.shiftLeft(a: 1, n: 3) // 8Applications
Define flags as single bits and combine them with Bits.or:
let READ = Bits.shiftLeft(1, 0)
let WRITE = Bits.shiftLeft(1, 1)
let EXEC = Bits.shiftLeft(1, 2)
Bits.or(READ, EXEC) // 5
Bits.and(Bits.or(READ, WRITE), EXEC) == 0 // trueBuild a mask of the low n bits:
func lowMask(n: int): uint32 {
Bits.shiftLeft(uint32(1), n) - uint32(1)
}
println(lowMask(4))
println(lowMask(12))15
4095Pack a color into one integer, one byte per channel:
type Rgb = { r: byte, g: byte, b: byte }
func pack(c: Rgb): uint32 {
let r = Bits.shiftLeft(uint32(c.r), 16)
let g = Bits.shiftLeft(uint32(c.g), 8)
Bits.or(Bits.or(r, g), uint32(c.b))
}
println(String.fromInt(pack(Rgb{ r: byte(255), g: byte(128), b: byte(0) }), base: 16))ff8000Turn an array of bits, most significant first, into a number:
let bits = [1, 0, 1, 1]
bits.reduce(0, func(n: int, bit: int) { n | Bits.shiftLeft(1) | Bits.or(bit) }) // 11Test whether bit i is set, with the index in a variable:
func hasBit(flags: int, i: int): bool {
Bits.and(flags, Bits.shiftLeft(1, i)) != 0
}
println(hasBit(5, 2))
println(hasBit(5, 1))true
falseProperties and relations
While the product fits the type, shifting by n multiplies by 2ⁿ:
let a = 7
for n in Iter.range(0, 10) {
assert(Bits.shiftLeft(a, n) == a * Bits.shiftLeft(1, n))
}On an unsigned type, Bits.shiftRight undoes a shift that lost no bits:
let a = byte(0b0001_0110)
assert(Bits.shiftRight(Bits.shiftLeft(a, 3), 3) == a)On a signed type Bits.shiftRight copies the sign bit, so a shift that moved a 1 into the sign position does not come back:
Bits.shiftRight(Bits.shiftLeft(int8(64), 1), 1) // -64Two shifts add their counts, as long as the sum stays below the width:
let a = uint16(0xBEEF)
assert(Bits.shiftLeft(Bits.shiftLeft(a, 3), 5) == Bits.shiftLeft(a, 8))Possible issues
A count equal to the width of the type panics; it does not give zero:
Bits.shiftLeft(byte(1), 8) // panic: Bits.shiftLeft needs a count from 0 up to but excluding 8, got 8A negative count panics instead of shifting right:
Bits.shiftLeft(1, -1) // panic: Bits.shiftLeft needs a count from 0 up to but excluding 64, got -1Where * panics on overflow, the shift drops the high bits without a word:
Bits.shiftLeft(Math.maxInt, 1) // -2
Math.maxInt * 2 // panic: integer overflowThe count must be an int, even when a is a smaller type:
let n = byte(3)
Bits.shiftLeft(1, n)
// error: cannot use byte where int is expectedIntegers have no shiftLeft of their own, so a dot call does not compile. Use the namespace form or a pipe:
let x = 5
x.shiftLeft(1)
// error: int has no field or function shiftLeftSee also
Bits.shiftRight · Bits.rotateLeft · Bits.or · Bits.and · Bits.leadingZeros · Bits.wrapMul · Bits
