Class: BitInByte

Inherits:
Object
  • Object
show all
Defined in:
lib/core/bit_in_byte.rb

Overview

The BitInByte object represents a single bit inside a byte value. He accepts a postion of himself which must be in the range of 1-8 and the actualbyte value which must be in the range of 0-255. The byte value may be decimal, hex, octal, or binary.

author

Niklas Schultz

version

0.1.2

license

MIT

Instance Method Summary collapse

Constructor Details

#initialize(pos, byte) ⇒ BitInByte

Returns a new instance of BitInByte.



32
33
34
35
# File 'lib/core/bit_in_byte.rb', line 32

def initialize(pos, byte)
  @pos = pos
  @byte = byte
end

Instance Method Details

#valueObject

Raises:

  • (ArgumentError)


37
38
39
40
41
42
43
# File 'lib/core/bit_in_byte.rb', line 37

def value
  raise ArgumentError, 'pos must be 1-8' if @pos <= 0 || @pos > 8
  raise ArgumentError, 'byte must be 0-255' if @byte < 0 || @byte > 255

  position = @pos - 1
  ((1 << position) & @byte) != 0
end