File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree 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 x < 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+
13+ puts binary_and ( 1 , 1 )
14+ # 0b1
15+ puts binary_and ( 0 , 1 )
16+ # 0b0
17+ puts binary_and ( 1024 , 1024 )
18+ # 0b10000000000
19+ puts binary_and ( 0 , 1023 )
20+ # 0b0000000000
21+ puts binary_and ( 16 , 58 )
22+ # 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+
15+ puts binary_count_setbits ( 0 )
16+ # 0
17+
18+ puts binary_count_setbits ( 1 )
19+ # 1
20+
21+ puts binary_count_setbits ( 1024 )
22+ # 1
23+
24+ puts binary_count_setbits ( 1023 )
25+ # 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+
22+ puts binary_count_trailing_zeroes ( 0 )
23+ # 1
24+
25+ puts binary_count_trailing_zeroes ( 1023 )
26+ # 0
27+
28+ puts binary_count_trailing_zeroes ( 1024 )
29+ # 10
30+
31+ puts binary_count_trailing_zeroes ( 54 )
32+ # 1
33+
34+ puts binary_count_trailing_zeroes ( 121024 )
35+ # 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+
13+ puts binary_or ( 1 , 1 )
14+ # 0b1
15+ puts binary_or ( 0 , 1 )
16+ # 0b1
17+ puts binary_or ( 1024 , 1024 )
18+ # 0b10000000000
19+ puts binary_or ( 0 , 1023 )
20+ # 0b1111111111
21+ puts binary_or ( 16 , 58 )
22+ # 0b110010
23+
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+
34+ puts binary_xor ( 1 , 1 )
35+ # 0b0
36+ puts binary_xor ( 0 , 1 )
37+ # 0b1
38+ puts binary_xor ( 1024 , 1024 )
39+ # 0b00000000000
40+ puts binary_xor ( 0 , 1023 )
41+ # 0b1111111111
42+ puts binary_xor ( 16 , 58 )
43+ # 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+ def clear_bit ( x , position )
20+ raise "position must be > 0" if position < 0
21+
22+ x & ~( 1 << position )
23+ end
24+
25+ puts clear_bit ( 0 , 0 )
26+ # 0
27+
28+ puts clear_bit ( 0 , 4 )
29+ # 0
30+
31+ puts clear_bit ( 8 , 3 )
32+ # 0
33+
34+ puts clear_bit ( 24 , 4 )
35+ # 8
36+
37+ def flip_bit ( x , position )
38+ raise "position must be > 0" if position < 0
39+
40+ x ^ ( 1 << position )
41+ end
42+
43+ puts flip_bit ( 0 , 0 )
44+ # 1
45+
46+ puts flip_bit ( 0 , 4 )
47+ # 16
48+
49+ puts flip_bit ( 8 , 3 )
50+ # 0
51+
52+ puts flip_bit ( 24 , 4 )
53+ # 8
54+
55+ def is_bit_set ( x , position )
56+ raise "position must be > 0" if position < 0
57+
58+ ( ( x >> position ) & 1 ) == 1
59+ end
60+
61+ puts is_bit_set ( 0 , 0 )
62+ # false
63+
64+ puts is_bit_set ( 1 , 0 )
65+ # true
66+
67+ puts is_bit_set ( 8 , 3 )
68+ # true
69+
70+ puts is_bit_set ( 24 , 4 )
71+ # true
You can’t perform that action at this time.
0 commit comments