Class: Fixnum

Inherits:
Object
  • Object
show all
Defined in:
lib/indis-core/binaryops_fixnum.rb,
lib/indis-core/binaryops_string.rb

Overview

BinaryopsFixnum is a lightweight BinaryopsString, a few methods extending Fixnum with enough operations so that it's usable in bit calculations.

Instance Method Summary (collapse)

Instance Method Details

- (Object) [](range)

Raises:

  • (ArgumentError)


49
50
51
52
53
# File 'lib/indis-core/binaryops_fixnum.rb', line 49

def [](range)
  return old_range(range) unless range.is_a?(Range)
  raise ArgumentError, "Only inclusive ranges are supported" if range.exclude_end?
  (self >> range.begin) & ((1 << (range.end-range.begin+1)) - 1)
end

- (Object) bitlen



26
27
28
# File 'lib/indis-core/binaryops_fixnum.rb', line 26

def bitlen
  @bitlen ||= self.to_s(2).length
end

- (Object) bitlen=(bitlen)



22
23
24
# File 'lib/indis-core/binaryops_fixnum.rb', line 22

def bitlen=(bitlen)
  @bitlen = bitlen
end

- (Object) msb



35
36
37
# File 'lib/indis-core/binaryops_fixnum.rb', line 35

def msb
  self >> (self.bitlen-1)
end

- (Object) old_range



47
# File 'lib/indis-core/binaryops_fixnum.rb', line 47

alias :old_range :[]

- (Object) set_bitlen(i)



30
31
32
33
# File 'lib/indis-core/binaryops_fixnum.rb', line 30

def set_bitlen(i)
  @bitlen = i
  self
end

- (Object) to_bo

Helper method to convert Fixnum to BinaryopsString



141
142
143
# File 'lib/indis-core/binaryops_string.rb', line 141

def to_bo
  BinaryopsString.new(self.to_s(2))
end

- (Object) to_boz(len)

Helper method to convert Fixnum to BinaryopsString with given length (zero-padded)



147
148
149
# File 'lib/indis-core/binaryops_string.rb', line 147

def to_boz(len)
  BinaryopsString.new(self.to_s(2)).zero_extend(len)
end

- (Object) to_signed



39
40
41
42
43
44
45
# File 'lib/indis-core/binaryops_fixnum.rb', line 39

def to_signed
  if msb == 1
    - ((1 << @bitlen) - self)
  else
    self
  end
end