Class: RubyLabs::BitLab::Code
- Inherits:
-
Object
- Object
- RubyLabs::BitLab::Code
- Defined in:
- lib/bitlab.rb
Overview
Way cool – this class used to have a maxcodesize (set to 60) to make sure codes fit within
a single 64-bit word. But a typo during one experiment created an 80-bit code. Turns out
indexing etc work just fine on Bignums:
>> c1 = s[1].code(80)
=> 00000000000000000000000000000000000000000000000000000000000000000000000001101000
>> c1.flip(0)
=> 10000000000000000000000000000000000000000000000000000000000000000000000001101000
Instance Attribute Summary collapse
-
#base ⇒ Object
Returns the value of attribute base.
-
#length ⇒ Object
Returns the value of attribute length.
-
#value ⇒ Object
Returns the value of attribute value.
Instance Method Summary collapse
- #+(bit) ⇒ Object
- #<<(x) ⇒ Object
- #<=>(x) ⇒ Object
- #[](i) ⇒ Object
- #add_parity_bit ⇒ Object
- #chr ⇒ Object
- #even_parity? ⇒ Boolean
- #flip(i) ⇒ Object
-
#initialize(value, length = 0, base = :binary) ⇒ Code
constructor
A new instance of Code.
- #inspect ⇒ Object (also: #to_s)
- #parity_bit ⇒ Object
Constructor Details
#initialize(value, length = 0, base = :binary) ⇒ Code
Returns a new instance of Code.
355 356 357 358 359 360 361 |
# File 'lib/bitlab.rb', line 355 def initialize(value, length = 0, base = :binary) raise "Code: unknown base: #{base}" unless [:binary, :hex].include?(base) @value = value @length = length @base = base @has_parity_bit = false end |
Instance Attribute Details
#base ⇒ Object
Returns the value of attribute base.
353 354 355 |
# File 'lib/bitlab.rb', line 353 def base @base end |
#length ⇒ Object
Returns the value of attribute length.
353 354 355 |
# File 'lib/bitlab.rb', line 353 def length @length end |
#value ⇒ Object
Returns the value of attribute value.
353 354 355 |
# File 'lib/bitlab.rb', line 353 def value @value end |
Instance Method Details
#+(bit) ⇒ Object
363 364 365 366 367 |
# File 'lib/bitlab.rb', line 363 def +(bit) raise "operation not defined for hex codes" unless @base == :binary val = (@value << 1) | bit[0] return Code.new(val, @length+1) end |
#<<(x) ⇒ Object
369 370 371 372 373 374 375 376 377 378 379 380 381 382 |
# File 'lib/bitlab.rb', line 369 def <<(x) raise "operation not defined for hex codes" unless @base == :binary if x.class == Code val = x.value # val known to fit in x.length bits n = x.length else val = x[0] # val is a single bit n = 1 end @value <<= n @value |= val # val can't have any higher order bits set -- see above @length += n return self end |
#<=>(x) ⇒ Object
430 431 432 |
# File 'lib/bitlab.rb', line 430 def <=>(x) return @value <=> x.value end |
#[](i) ⇒ Object
384 385 386 387 388 389 390 391 392 393 |
# File 'lib/bitlab.rb', line 384 def [](i) if i.class == Range res = 0 n = 0 i.each { |j| res <<= 1; res |= @value[@length-j-1]; n += 1 } return Code.new(res, n) else return @value[@length-i-1] end end |
#add_parity_bit ⇒ Object
403 404 405 406 |
# File 'lib/bitlab.rb', line 403 def add_parity_bit @has_parity_bit = true return self << parity_bit end |
#chr ⇒ Object
418 419 420 421 422 423 424 425 426 427 428 |
# File 'lib/bitlab.rb', line 418 def chr if @has_parity_bit if even_parity? return (@value >> 1).chr else return "?" end else return @value.chr end end |
#even_parity? ⇒ Boolean
408 409 410 |
# File 'lib/bitlab.rb', line 408 def even_parity? return parity_bit() == 0 end |
#flip(i) ⇒ Object
412 413 414 415 416 |
# File 'lib/bitlab.rb', line 412 def flip(i) raise "bit index out of range" unless i < @length @value ^= (1 << (@length - i - 1)) return self end |
#inspect ⇒ Object Also known as: to_s
434 435 436 437 438 439 440 441 442 |
# File 'lib/bitlab.rb', line 434 def inspect return "" if @length == 0 case @base when :binary return sprintf "%0#{@length}b", @value when :hex return sprintf "%0#{@length/4}X", @value end end |
#parity_bit ⇒ Object
395 396 397 398 399 400 401 |
# File 'lib/bitlab.rb', line 395 def parity_bit bit = 0 for i in 0...@length bit ^= @value[i] end return bit end |