October 2006
Intermediate to advanced
888 pages
16h 55m
English
Occasionally we may need to operate on a Fixnum as a binary entity. This is less common in application level programming, but the need still arises.
Ruby has a relatively full set of capabilities in this area. For convenience, numeric constants may be expressed in binary, octal, or hexadecimal. The usual operators AND, OR, XOR, and NOT are expressed by the Ruby operators &, |, ^, and ~, respectively.
x = 0377 # Octal (decimal 255) y = 0b00100110 # Binary (decimal 38) z = 0xBEEF # Hex (decimal 48879) a = x | z # 48895 (bitwise OR) b = x & z # 239 (bitwise AND) c = x ^ z # 48656 (bitwise XOR) d = ~ y # -39 (negation or 1's complement)
The instance method size can be used to determine the word size ...
Read now
Unlock full access