Module: BitTwiddle

Defined in:
ext/bit_twiddle/bit_twiddle.c

Class Method Summary collapse

Class Method Details

.add_core_extensionsObject



1406
1407
1408
1409
1410
1411
1412
# File 'ext/bit_twiddle/bit_twiddle.c', line 1406

static VALUE
bt_add_core_extensions(VALUE self)
{
  /* this is so Yardoc can find method definitions */
  init_core_extensions();
  return Qnil;
}

.arith_rshift16Integer

Arithmetic right-shift of the low 16 bits in ‘int`.

If bit 16 is a 1, the vacated bit positions will be filled with 1s. Otherwise, they will be filled with 0s. Or, if the shift distance is negative, a left shift will be performed instead, and the vacated bit positions will be filled with 0s.

Examples:

BitTwiddle.arith_rshift16(0xaabbccdd, 1).to_s(16) # => "aabbe66e"
BitTwiddle.arith_rshift16(0xaabbccdd, 2).to_s(16) # => "aabbf337"

.arith_rshift32Integer

Arithmetic right-shift of the low 32 bits in ‘int`.

If bit 32 is a 1, the vacated bit positions will be filled with 1s. Otherwise, they will be filled with 0s. Or, if the shift distance is negative, a left shift will be performed instead, and the vacated bit positions will be filled with 0s.

Examples:

BitTwiddle.arith_rshift32(0xaabbccddaabbccdd, 1).to_s(16) # => "d55de66e"
BitTwiddle.arith_rshift32(0xaabbccddaabbccdd, 2).to_s(16) # => "eaaef337"

.arith_rshift64Integer

Arithmetic right-shift of the low 64 bits in ‘int`.

If bit 64 is a 1, the vacated bit positions will be filled with 1s. Otherwise, they will be filled with 0s. Or, if the shift distance is negative, a left shift will be performed instead, and the vacated bit positions will be filled with 0s.

Examples:

BitTwiddle.arith_rshift64(0xaabbccddaabbccdd, 1).to_s(16) # => "d55de66ed55de66e"
BitTwiddle.arith_rshift64(0xaabbccddaabbccdd, 2).to_s(16) # => "eaaef3376aaef337"

.arith_rshift8Integer

Arithmetic right-shift of the low 8 bits in ‘int`.

If bit 8 is a 1, the vacated bit positions will be filled with 1s. Otherwise, they will be filled with 0s. Or, if the shift distance is negative, a left shift will be performed instead, and the vacated bit positions will be filled with 0s.

Examples:

BitTwiddle.arith_rshift8(0xaabbccdd, 1).to_s(16) # => "aabbccee"
BitTwiddle.arith_rshift8(0xaabbccdd, 2).to_s(16) # => "aabbccf7"

.bitreverse16Integer

Reverse the low 16 bits in ‘int`.

If ‘int` is negative, raise `RangeError`.

Examples:

BitTwiddle.bitreverse16(0b0110101100001011).to_s(2) # => "1101000011010110"

.bitreverse32Integer

Reverse the low 32 bits in ‘int`.

If ‘int` is negative, raise `RangeError`.

Examples:

BitTwiddle.bitreverse32(0x12341234).to_s(16) # => "2c482c48"

.bitreverse64Integer

Reverse the low 64 bits in ‘int`.

If ‘int` is negative, raise `RangeError`.

Examples:

BitTwiddle.bitreverse64(0xabcd1234abcd1234).to_s(16) # => "2c48b3d52c48b3d5"

.bitreverse8Integer

Reverse the low 8 bits in ‘int`.

If ‘int` is negative, raise `RangeError`.

Examples:

BitTwiddle.bitreverse8(0b01101011).to_s(2) # => "11010110"

.bswap16Integer

Reverse the least-significant and second least-significant bytes of ‘int`. If `int` is negative, raise `RangeError`.

Examples:

BitTwiddle.bswap16(0xFF00) # => 255
BitTwiddle.bswap16(0x00FF) # => 65280

.bswap32Integer

Reverse the least-significant 4 bytes of ‘int`.

Does not reverse bits within each byte. This can be used to swap endianness of a 32-bit integer.

If ‘int` is negative, raise `RangeError`.

Examples:

BitTwiddle.bswap32(0xaabbccdd).to_s(16) # => "ddccbbaa"

.bswap64Integer

Reverse the least-significant 8 bytes of ‘int`.

Does not reverse bits within each byte. This can be used to swap endianness of a 64-bit integer.

If ‘int` is negative, raise `RangeError`.

Examples:

BitTwiddle.bswap64(0xaabbccdd).to_s(16) # => "ddccbbaa00000000"

.hi_bitInteger

Return the index of the highest 1 bit, where the least-significant bit is index 1. If ‘int` is 0, return 0. If `int` is negative, raise `RangeError`.

Examples:

BitTwiddle.hi_bit(1)   # => 1
BitTwiddle.hi_bit(255) # => 8

.lo_bitInteger

Return the index of the lowest 1 bit, where the least-significant bit is index 1. If this integer is 0, return 0. If ‘int` is negative, raise `RangeError`.

Examples:

BitTwiddle.lo_bit(1)   # => 1
BitTwiddle.lo_bit(128) # => 8

.lrot16Integer

Left-rotation (“circular shift”) of the low 16 bits in ‘int`.

If the rotate distance is negative, the bit rotation will be to the right instead.

Examples:

BitTwiddle.lrot16(0b0111000101110001, 1).to_s(2) # => "1110001011100010"
BitTwiddle.lrot16(0b0111000101110001, 3).to_s(2) # => "1000101110001011"

.lrot32Integer

Left-rotation (“circular shift”) of the low 32 bits in ‘int`.

If the rotate distance is negative, the bit rotation will be to the right instead.

Examples:

BitTwiddle.lrot32(0xaabbccdd, 4).to_s(16) # => "abbccdda"

.lrot64Integer

Left-rotation (“circular shift”) of the low 64 bits in ‘int`.

If the rotate distance is negative, the bit rotation will be to the right instead.

Examples:

BitTwiddle.lrot64(0x11223344aabbccdd, 4).to_s(16) # => "1223344aabbccdd1"

.lrot8Integer

Left-rotation (“circular shift”) of the low 8 bits in ‘int`.

If the rotate distance is negative, the bit rotation will be to the right instead.

Examples:

BitTwiddle.lrot8(0b01110001, 1).to_s(2) # => "11100010"
BitTwiddle.lrot8(0b01110001, 3).to_s(2) # => "10001011"

.lshift16Integer

Left-shift of the low 16 bits in ‘int`.

If the shift distance is negative, a right shift will be performed instead. The vacated bit positions will be filled with 0 bits. If shift distance is more than 15 or less than -15, the low 16 bits will all be zeroed.

Examples:

BitTwiddle.lshift16(0x11223344, 1).to_s(16) # => "11226688"
BitTwiddle.lshift16(0x11223344, 2).to_s(16) # => "1122cd10"

.lshift32Integer

Left-shift of the low 32 bits in ‘int`.

If the shift distance is negative, a right shift will be performed instead. The vacated bit positions will be filled with 0 bits. If shift distance is more than 31 or less than -31, the low 32 bits will all be zeroed.

Examples:

BitTwiddle.lshift32(0x11223344, 1).to_s(16) # => "22446688"
BitTwiddle.lshift32(0x11223344, 2).to_s(16) # => "4488cd10"

.lshift64Integer

Left-shift of the low 64 bits in ‘int`.

If the shift distance is negative, a right shift will be performed instead. The vacated bit positions will be filled with 0 bits. If shift distance is more than 63 or less than -63, the low 64 bits will all be zeroed.

Examples:

BitTwiddle.lshift64(0x1122334411223344, 1).to_s(16) # => "2244668822446688"
BitTwiddle.lshift64(0x1122334411223344, 2).to_s(16) # => "4488cd104488cd10"

.lshift8Integer

Left-shift of the low 8 bits in ‘int`.

If the shift distance is negative, a right shift will be performed instead. The vacated bit positions will be filled with 0 bits. If shift distance is more than 7 or less than -7, the low 8 bits will all be zeroed.

Examples:

BitTwiddle.lshift8(0x11223344, 1).to_s(16) # => "11223388"
BitTwiddle.lshift8(0x11223344, 2).to_s(16) # => "11223310"

.popcountInteger

Return the number of 1 bits in ‘int`. If `int` is negative, raise `RangeError`.

Examples:

BitTwiddle.popcount(7)   # => 3
BitTwiddle.popcount(255) # => 8

.rrot16Integer

Right-rotation (“circular shift”) of the low 16 bits in ‘int`.

If the rotate distance is negative, the bit rotation will be to the left instead.

Examples:

BitTwiddle.rrot16(0b0111000101110001, 1).to_s(2) # => "1011100010111000"
BitTwiddle.rrot16(0b0111000101110001, 3).to_s(2) # => "10111000101110"

.rrot32Integer

Right-rotation (“circular shift”) of the low 32 bits in ‘int`.

If the rotate distance is negative, the bit rotation will be to the left instead.

Examples:

BitTwiddle.rrot32(0xaabbccdd, 4).to_s(16) # => "daabbccd"

.rrot64Integer

Right-rotation (“circular shift”) of the low 64 bits in ‘int`.

If the rotate distance is negative, the bit rotation will be to the left instead.

Examples:

BitTwiddle.rrot64(0x11223344aabbccdd, 4).to_s(16) # => "d11223344aabbccd"

.rrot8Integer

Right-rotation (“circular shift”) of the low 8 bits in ‘int`.

If the rotate distance is negative, the bit rotation will be to the left instead.

Examples:

BitTwiddle.rrot8(0b01110001, 1).to_s(2) # => "10111000"
BitTwiddle.rrot8(0b01110001, 3).to_s(2) # => "101110"

.rshift16Integer

Right-shift of the low 16 bits in ‘int`.

If the shift distance is negative, a left shift will be performed instead. The vacated bit positions will be filled with 0 bits. If shift distance is more than 15 or less than -15, the low 16 bits will all be zeroed.

Examples:

BitTwiddle.rshift16(0x11223344, 1).to_s(16) # => "112219a2"
BitTwiddle.rshift16(0x11223344, 2).to_s(16) # => "11220cd1"

.rshift32Integer

Right-shift of the low 32 bits in ‘int`.

If the shift distance is negative, a left shift will be performed instead. The vacated bit positions will be filled with 0 bits. If shift distance is more than 31 or less than -31, the low 32 bits will all be zeroed.

Examples:

BitTwiddle.rshift32(0x11223344, 1).to_s(16) # => "89119a2"
BitTwiddle.rshift32(0x11223344, 2).to_s(16) # => "4488cd1"

.rshift64Integer

Arithmetic right-shift of the low 64 bits in ‘int`.

If bit 64 is a 1, the vacated bit positions will be filled with 1s. Otherwise, they will be filled with 0s. Or, if the shift distance is negative, a left shift will be performed instead, and the vacated bit positions will be filled with 0s.

Examples:

BitTwiddle.arith_rshift64(0xaabbccddaabbccdd, 1).to_s(16) # => "d55de66ed55de66e"
BitTwiddle.arith_rshift64(0xaabbccddaabbccdd, 2).to_s(16) # => "eaaef3376aaef337"

.rshift8Integer

Right-shift of the low 8 bits in ‘int`.

If the shift distance is negative, a left shift will be performed instead. The vacated bit positions will be filled with 0 bits. If shift distance is more than 7 or less than -7, the low 8 bits will all be zeroed.

Examples:

BitTwiddle.rshift8(0x11223344, 1).to_s(16) # => "11223322"
BitTwiddle.rshift8(0x11223344, 2).to_s(16) # => "11223311"