File tree 6 files changed +253
-0
lines changed
6 files changed +253
-0
lines changed Original file line number Diff line number Diff line change
1
+ def binary_and ( x , y )
2
+ raise 'Input must only contain positive integers' if x < 0 or y < 0
3
+
4
+ "0b" + ( x & y ) . to_s ( 2 )
5
+ end
6
+
7
+ begin
8
+ binary_and ( -1 , 0 )
9
+ rescue => e
10
+ puts e . message
11
+ end
12
+ # Input must only contain positive integers
13
+
14
+ puts binary_and ( 1 , 1 )
15
+ # 0b1
16
+ puts binary_and ( 0 , 1 )
17
+ # 0b0
18
+ puts binary_and ( 1024 , 1024 )
19
+ # 0b10000000000
20
+ puts binary_and ( 0 , 1023 )
21
+ # 0b0000000000
22
+ puts binary_and ( 16 , 58 )
23
+ # 0b010000
Original file line number Diff line number Diff line change
1
+ def binary_count_setbits ( x )
2
+ raise 'Input must be a positive integer' if x < 0
3
+
4
+ binary = x . to_s ( 2 )
5
+
6
+ binary . chars . map { |c | c . to_i } . reduce ( :+ )
7
+ end
8
+
9
+ begin
10
+ binary_count_setbits ( -1 )
11
+ rescue => e
12
+ puts e . message
13
+ end
14
+ # Input must be a positive integer
15
+
16
+ puts binary_count_setbits ( 0 )
17
+ # 0
18
+
19
+ puts binary_count_setbits ( 1 )
20
+ # 1
21
+
22
+ puts binary_count_setbits ( 1024 )
23
+ # 1
24
+
25
+ puts binary_count_setbits ( 1023 )
26
+ # 10
Original file line number Diff line number Diff line change
1
+ def binary_count_trailing_zeroes ( x )
2
+ raise 'Input must be a positive integer' if x < 0
3
+
4
+ binary = x . to_s ( 2 )
5
+
6
+ count = 0
7
+ binary . chars . reverse_each do |char |
8
+ break if char == "1"
9
+
10
+ count += 1
11
+ end
12
+
13
+ count
14
+ end
15
+
16
+ begin
17
+ binary_count_trailing_zeroes ( -1 )
18
+ rescue => e
19
+ puts e . message
20
+ end
21
+ # Input must be a positive integer
22
+
23
+ puts binary_count_trailing_zeroes ( 0 )
24
+ # 1
25
+
26
+ puts binary_count_trailing_zeroes ( 1023 )
27
+ # 0
28
+
29
+ puts binary_count_trailing_zeroes ( 1024 )
30
+ # 10
31
+
32
+ puts binary_count_trailing_zeroes ( 54 )
33
+ # 1
34
+
35
+ puts binary_count_trailing_zeroes ( 121024 )
36
+ # 6
Original file line number Diff line number Diff line change
1
+ def binary_or ( x , y )
2
+ raise 'Input must only contain positive integers' if x < 0 or y < 0
3
+
4
+ "0b" + ( x | y ) . to_s ( 2 )
5
+ end
6
+
7
+ begin
8
+ binary_or ( -1 , 0 )
9
+ rescue => e
10
+ puts e . message
11
+ end
12
+ # Input must only contain positive integers
13
+
14
+ puts binary_or ( 1 , 1 )
15
+ # 0b1
16
+ puts binary_or ( 0 , 1 )
17
+ # 0b1
18
+ puts binary_or ( 1024 , 1024 )
19
+ # 0b10000000000
20
+ puts binary_or ( 0 , 1023 )
21
+ # 0b1111111111
22
+ puts binary_or ( 16 , 58 )
23
+ # 0b110010
24
+
Original file line number Diff line number Diff line change
1
+ def binary_xor ( x , y )
2
+ raise 'Input must only contain positive integers' if x < 0 or y < 0
3
+
4
+ binary_x = x . to_s ( 2 )
5
+ binary_y = y . to_s ( 2 )
6
+
7
+ if binary_x . length > binary_y . length
8
+ prefix = "0" * ( binary_x . length - binary_y . length )
9
+ binary_y = prefix + binary_y
10
+ elsif binary_y . length > binary_x . length
11
+ prefix = "0" * ( binary_y . length - binary_x . length )
12
+ binary_x = prefix + binary_x
13
+ end
14
+ result = "0b"
15
+ binary_x . each_char . with_index do |x_char , i |
16
+ y_char = binary_y [ i ]
17
+
18
+ if ( x_char == "1" && y_char != "1" ) || ( x_char != "1" && y_char == "1" )
19
+ result += "1"
20
+ else
21
+ result += "0"
22
+ end
23
+ end
24
+
25
+ result
26
+ end
27
+
28
+ begin
29
+ binary_xor ( -1 , 0 )
30
+ rescue => e
31
+ puts e . message
32
+ end
33
+ # Input must only contain positive integers
34
+
35
+ puts binary_xor ( 1 , 1 )
36
+ # 0b0
37
+ puts binary_xor ( 0 , 1 )
38
+ # 0b1
39
+ puts binary_xor ( 1024 , 1024 )
40
+ # 0b00000000000
41
+ puts binary_xor ( 0 , 1023 )
42
+ # 0b1111111111
43
+ puts binary_xor ( 16 , 58 )
44
+ # 0b101010
Original file line number Diff line number Diff line change
1
+ def set_bit ( x , position )
2
+ raise "position must be >= 0" if position < 0
3
+
4
+ x | ( 1 << position )
5
+ end
6
+
7
+ puts set_bit ( 0 , 0 )
8
+ # 1
9
+
10
+ puts set_bit ( 0 , 4 )
11
+ # 16
12
+
13
+ puts set_bit ( 8 , 3 )
14
+ # 8
15
+
16
+ puts set_bit ( 8 , 4 )
17
+ # 24
18
+
19
+ begin
20
+ puts set_bit ( 8 , -4 )
21
+ rescue => e
22
+ puts e . message
23
+ end
24
+ # position must be >= 0
25
+
26
+
27
+ def clear_bit ( x , position )
28
+ raise "position must be > 0" if position < 0
29
+
30
+ x & ~( 1 << position )
31
+ end
32
+
33
+ puts clear_bit ( 0 , 0 )
34
+ # 0
35
+
36
+ puts clear_bit ( 0 , 4 )
37
+ # 0
38
+
39
+ puts clear_bit ( 8 , 3 )
40
+ # 0
41
+
42
+ puts clear_bit ( 24 , 4 )
43
+ # 8
44
+
45
+ begin
46
+ puts clear_bit ( 0 , -4 )
47
+ rescue => e
48
+ puts e . message
49
+ end
50
+ # position must be > 0
51
+
52
+ def flip_bit ( x , position )
53
+ raise "position must be > 0" if position < 0
54
+
55
+ x ^ ( 1 << position )
56
+ end
57
+
58
+ puts flip_bit ( 0 , 0 )
59
+ # 1
60
+
61
+ puts flip_bit ( 0 , 4 )
62
+ # 16
63
+
64
+ puts flip_bit ( 8 , 3 )
65
+ # 0
66
+
67
+ puts flip_bit ( 24 , 4 )
68
+ # 8
69
+
70
+ begin
71
+ puts flip_bit ( 0 , -4 )
72
+ rescue => e
73
+ puts e . message
74
+ end
75
+ # position must be > 0
76
+
77
+ def is_bit_set ( x , position )
78
+ raise "position must be > 0" if position < 0
79
+
80
+ ( ( x >> position ) & 1 ) == 1
81
+ end
82
+
83
+ puts is_bit_set ( 0 , 0 )
84
+ # false
85
+
86
+ puts is_bit_set ( 1 , 0 )
87
+ # true
88
+
89
+ puts is_bit_set ( 8 , 3 )
90
+ # true
91
+
92
+ puts is_bit_set ( 24 , 4 )
93
+ # true
94
+
95
+ begin
96
+ puts is_bit_set ( 0 , -4 )
97
+ rescue => e
98
+ puts e . message
99
+ end
100
+ # position must be > 0
You can’t perform that action at this time.
0 commit comments