Module: Ciri::Utils::Number

Extended by:
Number
Included in:
Ciri::Utils, Number
Defined in:
lib/ciri/utils/number.rb

Constant Summary collapse

UINT_256_MAX =
2 ** 256 - 1
UINT_256_CEILING =
2 ** 256
UINT_255_MAX =
2 ** 255 - 1
UINT_255_CEILING =
2 ** 255

Instance Method Summary collapse

Instance Method Details

#big_endian_decode(input) ⇒ Object



34
35
36
# File 'lib/ciri/utils/number.rb', line 34

def big_endian_decode(input)
  input.each_byte.reduce(0) {|s, i| s * 256 + i}
end

#big_endian_encode(n, zero = ''.b, size: nil) ⇒ Object



29
30
31
32
# File 'lib/ciri/utils/number.rb', line 29

def big_endian_encode(n, zero = ''.b, size: nil)
  b = big_endian_encode_raw(n, zero)
  size.nil? ? b : b.rjust(size, "\x00".b)
end

#ceil_div(n, ceil) ⇒ Object



51
52
53
54
# File 'lib/ciri/utils/number.rb', line 51

def ceil_div(n, ceil)
  size, m = n.divmod ceil
  m.zero? ? size : size + 1
end

#signed_to_unsigned(n) ⇒ Object



47
48
49
# File 'lib/ciri/utils/number.rb', line 47

def signed_to_unsigned(n)
  n >= 0 ? n : n + UINT_256_CEILING
end

#unsigned_to_signed(n) ⇒ Object



43
44
45
# File 'lib/ciri/utils/number.rb', line 43

def unsigned_to_signed(n)
  n <= UINT_255_MAX ? n : n - UINT_256_CEILING
end