class ClientForPoslynx::BitSequence
Attributes
digits_string[R]
Public Class Methods
from_base64(encoded)
click to toggle source
# File lib/client_for_poslynx/bit_sequence.rb, line 61 def from_base64(encoded) packed_bits = encoded.unpack('m0').first from_packed_bits( packed_bits ) end
from_bit_digits(digits_string)
click to toggle source
# File lib/client_for_poslynx/bit_sequence.rb, line 22 def from_bit_digits(digits_string) new( digits_string ) end
Also aliased as: /
from_packed_bits(packed_string)
click to toggle source
# File lib/client_for_poslynx/bit_sequence.rb, line 11 def from_packed_bits(packed_string) digits_string = packed_string.unpack('B*').first from_bit_digits( digits_string ) end
Also aliased as: ^
from_signed(value, seq_length)
click to toggle source
# File lib/client_for_poslynx/bit_sequence.rb, line 44 def from_signed(value, seq_length) if seq_length > 64 raise TooManyBitsLong, "Can't build a representation more than 64 bits long from a signed number" end max_magnitude = 2 ** (seq_length - 1) if value < -max_magnitude raise NumberOutOfBounds, "The largest negative value representable in #{seq_length} bits is smaller than than #{value}" end if value >= max_magnitude raise NumberOutOfBounds, "The largest positive value representable in #{seq_length} bits is less than #{value}" end packed_bits = [ value ].pack('q>') bit_seq = from_packed_bits( packed_bits ) bit_seq.shift( 64 - seq_length ) bit_seq end
from_unsigned(value, seq_length)
click to toggle source
# File lib/client_for_poslynx/bit_sequence.rb, line 28 def from_unsigned(value, seq_length) if seq_length > 64 raise TooManyBitsLong, "Can't build a representation more than 64 bits long from an unsigned number" end if value < 0 raise NumberOutOfBounds, "Can't build an unsigned representation of a negative number" end if value >= 2 ** seq_length raise NumberOutOfBounds, "The largest value representable in #{seq_length} bits is less than #{value}" end packed_bits = [ value ].pack('Q>') bit_seq = from_packed_bits( packed_bits ) bit_seq.shift( 64 - seq_length ) bit_seq end
new(digits_string)
click to toggle source
# File lib/client_for_poslynx/bit_sequence.rb, line 67 def initialize(digits_string) @digits_string = digits_string end
new_empty()
click to toggle source
# File lib/client_for_poslynx/bit_sequence.rb, line 18 def new_empty from_bit_digits('') end
Public Instance Methods
==(other)
click to toggle source
# File lib/client_for_poslynx/bit_sequence.rb, line 81 def ==(other) return false unless other.respond_to?( :to_bit_digits ) digits_string == other.to_bit_digits end
as_signed()
click to toggle source
# File lib/client_for_poslynx/bit_sequence.rb, line 124 def as_signed if length > 64 raise TooManyBitsLong, "Cannot coerce sequence longer than 64 bits to signed number" end fill = first_bit_digit * 64 little_endian_digits = digits_string.reverse + fill packed_little_endian = [ little_endian_digits ].pack('b*') packed_little_endian.unpack('q<').first end
as_unsigned()
click to toggle source
# File lib/client_for_poslynx/bit_sequence.rb, line 135 def as_unsigned if length > 64 raise TooManyBitsLong, "Cannot coerce sequence longer than 64 bits to unsigned number" end little_endian_digits = digits_string.reverse + '0' * 64 packed_little_endian = [ little_endian_digits ].pack('b*') packed_little_endian.unpack('Q<').first end
base64_encode()
click to toggle source
# File lib/client_for_poslynx/bit_sequence.rb, line 94 def base64_encode [ to_packed_bits ].pack('m0') end
first_bit_digit()
click to toggle source
# File lib/client_for_poslynx/bit_sequence.rb, line 102 def first_bit_digit digits_string[0..0] end
inspect()
click to toggle source
# File lib/client_for_poslynx/bit_sequence.rb, line 71 def inspect "#<#{self.class.name}: #{pretty_digits}>" end
length()
click to toggle source
# File lib/client_for_poslynx/bit_sequence.rb, line 98 def length digits_string.length end
pretty_digits()
click to toggle source
# File lib/client_for_poslynx/bit_sequence.rb, line 75 def pretty_digits nibbles = digits_string.scan(/.{1,4}/) bytes = nibbles.each_slice(2).map{ |n| n.join(' ') } bytes.join(' ') end
push(sequence)
click to toggle source
# File lib/client_for_poslynx/bit_sequence.rb, line 106 def push(sequence) digits_string << sequence.to_bit_digits self end
Also aliased as: <<
shift(bit_count)
click to toggle source
# File lib/client_for_poslynx/bit_sequence.rb, line 113 def shift(bit_count) digits_taken = digits_string[0...bit_count] digits_string[0...bit_count] = '' self.class.from_bit_digits( digits_taken ) end
to_bit_digits()
click to toggle source
# File lib/client_for_poslynx/bit_sequence.rb, line 90 def to_bit_digits digits_string end
to_packed_bits()
click to toggle source
# File lib/client_for_poslynx/bit_sequence.rb, line 86 def to_packed_bits [ digits_string ].pack('B*') end
unshift( bit_sequence )
click to toggle source
# File lib/client_for_poslynx/bit_sequence.rb, line 119 def unshift( bit_sequence ) digits_string[0...0] = bit_sequence.to_bit_digits self end