Bits.rotateRight
func Bits.rotateRight[T: Integer](a: T, n: int): TBits.rotateRight returns a rotated right by n bits. The count is taken modulo the bit width of T, so n may exceed it. It panics if n < 0.
Details
- The bits that fall off the low end come back in at the high end, so no bit is lost.
- The width is the width of
T: 8 foruint8, 64 forint. - For a signed type the rotation works on the two's complement bits, so a bit can move into or out of the sign bit.
Basic examples
Bits.rotateRight(uint8(1), 1) // 128
Bits.rotateRight(uint8(3), 1) // 129
Bits.rotateRight(uint8(0xab), 4) // 186Written in base 2, the low bit moves to the top:
String.fromInt(uint8(0b00000110), 2) // "110"
String.fromInt(Bits.rotateRight(uint8(0b00000110), 2), 2) // "10000001"Scope
Every integer type, each at its own width:
Bits.rotateRight(uint8(1), 1) // 128
Bits.rotateRight(uint16(1), 1) // 32768
Bits.rotateRight(uint32(1), 1) // 2147483648
Bits.rotateRight(uint64(1), 1) // 9223372036854775808A signed type rotates its two's complement bits. A low bit that comes around lands in the sign bit:
Bits.rotateRight(int8(1), 1) // -128
Bits.rotateRight(int8(-128), 7) // 1
Bits.rotateRight(int8(-1), 3) // -1A count at or beyond the width wraps around:
Bits.rotateRight(uint8(2), 9) // 1
Bits.rotateRight(uint8(2), 8) // 2
Bits.rotateRight(uint64(5), 64) // 5A count of 0 returns the value unchanged:
Bits.rotateRight(uint8(5), 0) // 5In a pipe, the value is the first argument:
uint16(0x1234) | Bits.rotateRight(4) // 16675The result has the type of a, so a uint32 stays a uint32:
let word = uint32(0x12345678)
let rotated: uint32 = Bits.rotateRight(word, 8)
String.fromInt(rotated, 16) // "78123456"Applications
Swap the two halves of a byte, its high and low nibbles:
let b = uint8(0x3c)
String.fromInt(Bits.rotateRight(b, 4), 16) // "c3"Mix the bits of a word the way SHA-256 does in its round function, with three rotations combined by xor:
func bigSigma0(x: uint32): uint32 {
let a = Bits.rotateRight(x, 2)
let b = Bits.rotateRight(x, 13)
let c = Bits.rotateRight(x, 22)
Bits.xor(Bits.xor(a, b), c)
}
println(String.fromInt(bigSigma0(uint32(0x6a09e667)), 16))ce20b47eStep through a ring of eight flags, one position to the right on each turn:
var lamp = uint8(0b10000000)
for _ in Iter.range(0, 4) {
println(String.fromInt(lamp, 2))
lamp = Bits.rotateRight(lamp, 1)
}10000000
1000000
100000
10000Properties and relations
assert(Bits.rotateRight(Bits.rotateLeft(x, n), n) == x)
assert(Bits.rotateRight(x, n) == Bits.rotateLeft(x, 8 - n))
assert(Bits.rotateRight(x, n + 8) == Bits.rotateRight(x, n))
assert(Bits.popCount(Bits.rotateRight(x, n)) == Bits.popCount(x))For an unsigned value and a count between 1 and the width minus 1, a rotation is two shifts joined by or:
assert(Bits.rotateRight(x, n) == Bits.or(Bits.shiftRight(x, n), Bits.shiftLeft(x, 8 - n)))Possible issues
A negative count panics. Use Bits.rotateLeft to rotate the other way:
Bits.rotateRight(uint8(5), -1) // panic: Bits.rotateRight needs a count of at least 0, got -1
Bits.rotateLeft(uint8(5), 1) // 10A literal is an int, 64 bits wide, so its low bit comes around at bit 63, not bit 7:
Bits.rotateRight(1, 1) // -9223372036854775808
Bits.rotateRight(uint8(1), 1) // 128Bits.rotateRight is not a function on the integer types, so a dot call does not compile. Use the namespace or a pipe:
let x = uint8(5)
let y = x.rotateRight(1) // error: byte has no field or function rotateRightSee also
Bits.rotateLeft, Bits.shiftRight, Bits.shiftLeft, Bits.popCount, Bits.xor, String.fromInt
