Method: Integer#bit

Defined in:
lib/core/facets/integer/bitmask.rb

#bit(bit) ⇒ Object

Set a bit.

0.bit(4)  #=> 16

Using a negative figure will clear a bit.

10.bit(-4)      #=> 2

This is more easily seen using binary.

0b0100.bit(-3)  #=> 0

CREDIT: Thomas Sawyer, George Moschovitis



17
18
19
20
21
22
23
24
25
# File 'lib/core/facets/integer/bitmask.rb', line 17

def bit(bit)
  if bit < 0
    mask = (1 << ~bit)
    self & ~mask
  else
    mask = (1 << bit)
    self | mask
  end
end