Class: Integer

Inherits:
Object
  • Object
show all
Defined in:
ext/bit_twiddle/bit_twiddle.c

Overview

Ruby’s good old Integer.

‘require “bit-twiddle/core_ext”` before trying to use any of the below methods.

Instance Method Summary collapse

Instance Method Details

#arith_rshift16Integer

Arithmetic right-shift of the low 16 bits in this integer.

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:

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

#arith_rshift32Integer

Arithmetic right-shift of the low 32 bits in this integer.

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:

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

#arith_rshift64Integer

Arithmetic right-shift of the low 64 bits in this integer.

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:

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

#arith_rshift8Integer

Arithmetic right-shift of the low 8 bits in this integer.

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:

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

#bitreverse16Integer

Reverse the low 16 bits in this integer.

If the receiver is negative, raise ‘RangeError`.

Examples:

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

#bitreverse32Integer

Reverse the low 32 bits in this integer.

If the receiver is negative, raise ‘RangeError`.

Examples:

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

#bitreverse64Integer

Reverse the low 64 bits in this integer.

If the receiver is negative, raise ‘RangeError`.

Examples:

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

#bitreverse8Integer

Reverse the low 8 bits in this integer.

If the receiver is negative, raise ‘RangeError`.

Examples:

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

#bswap16Integer

Reverse the least-significant and second least-significant bytes of this integer.

If the receiver is negative, raise ‘RangeError`.

Examples:

0xFF00.bswap16 # => 255
0x00FF.bswap16 # => 65280

#bswap32Integer

Reverse the least-significant 4 bytes of this integer.

Does not reverse bits within each byte. This can be used to swap endianness of a 32-bit integer. If the receiver is negative, raise ‘RangeError`.

Examples:

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

#bswap64Integer

Reverse the least-significant 8 bytes of this integer.

Does not reverse bits within each byte. This can be used to swap endianness of a 64-bit integer. If the receiver is negative, raise ‘RangeError`.

Examples:

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

#hi_bitInteger

Return the index of the highest 1 bit, where the least-significant bit is index 1. If the receiver is 0, return 0.

If the receiver is negative, raise ‘RangeError`.

Examples:

1.hi_bit   # => 1
255.hi_bit # => 8

#lo_bitInteger

Return the index of the lowest 1 bit, where the least-significant bit is index 1. If the receiver is 0, return 0.

If the receiver is negative, raise ‘RangeError`.

Examples:

1.lo_bit   # => 1
128.lo_bit # => 8

#lrot16Integer

Left-rotation (“circular shift”) of the low 16 bits in this integer.

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

Examples:

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

#lrot32Integer

Left-rotation (“circular shift”) of the low 32 bits in this integer.

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

Examples:

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

#lrot64Integer

Left-rotation (“circular shift”) of the low 64 bits in this integer.

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

Examples:

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

#lrot8Integer

Left-rotation (“circular shift”) of the low 8 bits in this integer.

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

Examples:

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

#lshift16Integer

Left-shift of the low 16 bits in this integer.

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:

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

#lshift32Integer

Left-shift of the low 32 bits in this integer.

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:

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

#lshift64Integer

Left-shift of the low 64 bits in this integer.

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:

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

#lshift8Integer

Left-shift of the low 8 bits in this integer.

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:

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

#popcountInteger

Return the number of 1 bits in this integer.

If the receiver is negative, raise ‘RangeError`.

Examples:

7.popcount   # => 3
255.popcount # => 8

#rrot16Integer

Right-rotation (“circular shift”) of the low 16 bits in this integer.

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

Examples:

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

#rrot32Integer

Right-rotation (“circular shift”) of the low 32 bits in this integer.

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

Examples:

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

#rrot64Integer

Right-rotation (“circular shift”) of the low 64 bits in this integer.

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

Examples:

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

#rrot8Integer

Right-rotation (“circular shift”) of the low 8 bits in this integer.

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

Examples:

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

#rshift16Integer

Right-shift of the low 16 bits in this integer.

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:

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

#rshift32Integer

Right-shift of the low 32 bits in this integer.

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:

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

#rshift64Integer

Right-shift of the low 64 bits in this integer.

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 63 or less than -63, the low 64 bits will all be zeroed.

Examples:

0x1122334411223344.rshift64(1).to_s(16) # => "89119a2089119a2"
0x1122334411223344.rshift64(2).to_s(16) # => "4488cd104488cd1"

#rshift8Integer

Right-shift of the low 8 bits in this integer.

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:

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