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

#[](range) ⇒ Object

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

#bitlenObject



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

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

#bitlen=(bitlen) ⇒ Object



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

def bitlen=(bitlen)
  @bitlen = bitlen
end

#msbObject



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

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

#old_rangeObject



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

alias :old_range :[]

#set_bitlen(i) ⇒ Object



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

def set_bitlen(i)
  @bitlen = i
  self
end

#to_boObject

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

#to_boz(len) ⇒ Object

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

#to_signedObject



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