-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinary_ops.rb
68 lines (46 loc) · 1008 Bytes
/
binary_ops.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
## BASE CONVERSIONS
9.to_s(2)
# => "1001"
111.to_s(16)
# => "6f"
"1001".to_i(2)
# => 9
"6f".to_i(16)
# => 111
0b1001
# => 9
0x6f
# => 111
11.to_s(2)
# => "1011"
11[0]
# => 1
11[1]
# => 1
11[2]
# => 0
11[3]
# => 1
## ASCII
'a'.ord
# => 97
97.chr
# => "a"
## BASIC BINARY OPERATIONS
(0b0100110 & 0b0110101).to_s(2) # AND
# => "100100"
(0b0100110 | 0b0110101).to_s(2) # OR
# => "110111"
(0b0100110 ^ 0b0110101).to_s(2) # XOR
# => "10011"
(~0b1001).to_s(2) # NOT
# => "-1010"
# NOTE: We don't get an exact negation because the negative bit was also flipped
# and negative numbers are stored differently than positive ones.
5 << 1 # LEFT-SHIFT
# => 10
# NOTE: Left-shifting a binary number is the same as multiplying by two,
# just like left shifting a decimal number (25) multiplies it by ten (250)
5 >> 1 # RIGHT-SHIFT
# => 2
# NOTE: Right-shifting is like dividing by two and dropping the remainder!