Class: BinaryopsString
- Defined in:
- lib/indis-core/binaryops_string.rb
Overview
BinaryopsString manages bitwise operations on a String. This is useful when you expect bitstrings with meaningful leading zeroes.
Class Method Summary collapse
-
.zeroes(n) ⇒ Object
Returns a new BinaryopsString filled in with zeroes of given length.
Instance Method Summary collapse
-
#<<(amount) ⇒ Object
Performs a LSL operation.
-
#>>(amount) ⇒ Object
Performs a LSR operation.
-
#bit(i) ⇒ Object
A Fixnum with given bit value (rightmost bit is 0).
-
#bits(from, to) ⇒ Object
A new BinaryopsString with a given slice (rightmost bit is 0).
-
#concat(other) ⇒ Object
Concatenates the receiver with another BinaryopsString.
-
#initialize(s) ⇒ BinaryopsString
constructor
BinaryopsString can be initialized either from a String or a Fixnum.
-
#rbit(i) ⇒ Object
A Fixnum with given bit value (leftmost bit is 0).
-
#rbits(from, to) ⇒ Object
A new BinaryopsString with a given slice (leftmost bit is 0).
-
#ror(amount) ⇒ Object
Performs a ROR operation (cyclic LSR).
-
#sign_extend(len) ⇒ Object
Sign-extends the receiver to given length (left padding).
-
#to_i ⇒ Object
Outputs an unsigned int value of self.
-
#to_signed_i ⇒ Object
Outputs a signed int value of self.
-
#zero_extend(len) ⇒ Object
Zero-extends the receiver to given length (left padding).
-
#|(other) ⇒ Object
Performs an OR operation.
Methods inherited from String
Constructor Details
#initialize(s) ⇒ BinaryopsString
BinaryopsString can be initialized either from a String or a Fixnum
24 25 26 27 28 29 30 |
# File 'lib/indis-core/binaryops_string.rb', line 24 def initialize(s) if s.class == Fixnum super s.to_s(2) else super end end |
Class Method Details
.zeroes(n) ⇒ Object
Returns a new BinaryopsString filled in with zeroes of given length
127 128 129 |
# File 'lib/indis-core/binaryops_string.rb', line 127 def self.zeroes(n) BinaryopsString.new('0'*n) end |
Instance Method Details
#<<(amount) ⇒ Object
Performs a LSL operation
102 103 104 105 106 |
# File 'lib/indis-core/binaryops_string.rb', line 102 def <<(amount) return self if amount == 0 z = BinaryopsString.zeroes(amount) concat(z).bits(self.length-1, 0) end |
#>>(amount) ⇒ Object
Performs a LSR operation
110 111 112 113 114 |
# File 'lib/indis-core/binaryops_string.rb', line 110 def >>(amount) return self if amount == 0 z = BinaryopsString.zeroes(amount) z.concat(self).rbits(0, self.length-1) end |
#bit(i) ⇒ Object
Returns a Fixnum with given bit value (rightmost bit is 0).
60 61 62 |
# File 'lib/indis-core/binaryops_string.rb', line 60 def bit(i) self[~i].to_i end |
#bits(from, to) ⇒ Object
Returns a new BinaryopsString with a given slice (rightmost bit is 0).
47 48 49 |
# File 'lib/indis-core/binaryops_string.rb', line 47 def bits(from, to) BinaryopsString.new(self.to_s[~from..~to]) end |
#concat(other) ⇒ Object
Concatenates the receiver with another BinaryopsString
80 81 82 |
# File 'lib/indis-core/binaryops_string.rb', line 80 def concat(other) BinaryopsString.new(self.to_s + other.to_s) end |
#rbit(i) ⇒ Object
Returns a Fixnum with given bit value (leftmost bit is 0).
66 67 68 |
# File 'lib/indis-core/binaryops_string.rb', line 66 def rbit(i) self[i].to_i end |
#rbits(from, to) ⇒ Object
Returns a new BinaryopsString with a given slice (leftmost bit is 0).
54 55 56 |
# File 'lib/indis-core/binaryops_string.rb', line 54 def rbits(from, to) BinaryopsString.new(self.to_s[from..to]) end |
#ror(amount) ⇒ Object
Performs a ROR operation (cyclic LSR)
118 119 120 121 122 123 124 |
# File 'lib/indis-core/binaryops_string.rb', line 118 def ror(amount) return self if amount == 0 s = self.to_s moveover = s[-amount..-1] movable = s[0...-amount] BinaryopsString.new(moveover + movable) end |
#sign_extend(len) ⇒ Object
Sign-extends the receiver to given length (left padding)
94 95 96 97 98 |
# File 'lib/indis-core/binaryops_string.rb', line 94 def sign_extend(len) return self unless len-self.length > 0 zbs = self[0]*(len-self.length) BinaryopsString.new(zbs + self.to_s) end |
#to_i ⇒ Object
Outputs an unsigned int value of self
33 34 35 |
# File 'lib/indis-core/binaryops_string.rb', line 33 def to_i super 2 end |
#to_signed_i ⇒ Object
Outputs a signed int value of self
38 39 40 41 42 |
# File 'lib/indis-core/binaryops_string.rb', line 38 def to_signed_i return to_i if self[0] == '0' complement = ('1'*(self.length)).to_i(2) - to_i + 1 -complement end |
#zero_extend(len) ⇒ Object
Zero-extends the receiver to given length (left padding)
86 87 88 89 90 |
# File 'lib/indis-core/binaryops_string.rb', line 86 def zero_extend(len) return self unless len-self.length > 0 zbs = '0'*(len-self.length) BinaryopsString.new(zbs + self.to_s) end |
#|(other) ⇒ Object
Performs an OR operation
73 74 75 76 |
# File 'lib/indis-core/binaryops_string.rb', line 73 def |(other) raise ArgumentError unless self.length == other.length BinaryopsString.new(self.to_i | other.to_i).zero_extend(self.length) end |