Code Like Zell

Ruby Bitwise Notes

Posted 10 February 2009.

Start with an integer:

  x = 1

Shift by two bit positions:

  x <<= 2

OR with 1:

  x |= 1

AND with 14 (hex E):

  x &= 0xE
  # is the same as:
  x &= 14

Get binary representation:

  x.to_s(2)

Get hex representation:

  x.to_s(16)

XOR (exclusive OR) with 1

  x ^= 1

All CLZ Posts

Hide this
Joypad Banner