MCPcopy
hub / github.com/jwasham/practice-python / main

Function main

bit_manipulation/bits.py:126–209  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

124
125
126def main():
127 for x in range(0, 20):
128 print(x, bin(x), hex(x))
129
130 print("Hamming distance: 1010111100 and 1001010101: ", hamming_distance(0b1010111100, 0b1001010101))
131
132 print("Bit 0: 1011 1100 set?", is_bit_set(0b10111100, 0))
133 print("Bit 1: 1011 1100 set?", is_bit_set(0b10111100, 1))
134 print("Bit 2: 1011 1100 set?", is_bit_set(0b10111100, 2))
135 print("Bit 3: 1011 1100 set?", is_bit_set(0b10111100, 3))
136 print("Bit 4: 1011 1100 set?", is_bit_set(0b10111100, 4))
137 print("Bit 5: 1011 1100 set?", is_bit_set(0b10111100, 5))
138 print("Bit 6: 1011 1100 set?", is_bit_set(0b10111100, 6))
139 print("Bit 7: 1011 1100 set?", is_bit_set(0b10111100, 7))
140
141 print("Hamming weight: 1010111100:", hamming_weight(0b1010111100))
142 print("Hamming weight: 1001000001:", hamming_weight(0b1001000001))
143 print("Hamming weight: 0001000000:", hamming_weight(0b0001000000))
144 print("Hamming weight: 0000000000:", hamming_weight(0b0000000000))
145 print("Hamming weight: 11111111:", hamming_weight(0b11111111))
146
147 print("PopCount: " + str(bin(1231424)) + ":", pop_count(1231424))
148 print("PopCount: " + str(bin(123)) + ":", pop_count(123))
149 print("PopCount: " + str(bin(8568)) + ":", pop_count(8568))
150 print("PopCount: " + str(bin(1)) + ":", pop_count(1))
151 print("PopCount: " + str(bin(-1)) + ":", pop_count(-1))
152 print("PopCount: " + str(bin(4)) + ":", pop_count(4))
153 print("PopCount: " + str(bin(903523125)) + ":", pop_count(903523125))
154
155 for x in range(0, 5):
156 print(str(x) + " is even?", is_even(x))
157
158 for x in range(0, 17):
159 print(str(x) + " is power of 2?", is_power_of_2(x))
160
161 print("Get 3 bits starting at position 5 in 1010111100", bin(get_bits(0b1010111100, 5, 3)))
162 print("Get 3 bits starting at position 8 in 1010111100", bin(get_bits(0b1010111100, 9, 3)))
163
164 print("Set bit 4 to 0: 01010010", bin(clear_bit(0b01010010, 4)))
165 print("Set bit 0 to 1: 01010010", bin(set_bit(0b01010010, 0)))
166 print("Toggle bit 1: 01010010", bin(toggle_bit(0b01010010, 1)))
167 print("Toggle bit 3: 01010010", bin(toggle_bit(0b01010010, 3)))
168
169 print("Rotate left 5 positions: 01001000100101011001010010011011:",
170 bin(rotate_left(0b01001000100101011001010010011011, 5)))
171
172 print("3 + 5 = ", add(3, 5))
173 print("33 + 51 = ", add(33, 51))
174 print("40 + 90 = ", add(40, 90))
175 print("45 + 15 = ", add(45, 15))
176
177 print("Sign of 45", get_sign(45))
178 print("Sign of 0", get_sign(0))
179 print("Sign of -1", get_sign(-1))
180 print("Sign of -23", get_sign(-23))
181
182 for x in range(0, 12):
183 print("has parity: " + str(x), has_parity(x), bin(x))

Callers 1

bits.pyFile · 0.70

Calls 15

hamming_distanceFunction · 0.85
is_bit_setFunction · 0.85
hamming_weightFunction · 0.85
pop_countFunction · 0.85
is_evenFunction · 0.85
is_power_of_2Function · 0.85
get_bitsFunction · 0.85
clear_bitFunction · 0.85
set_bitFunction · 0.85
toggle_bitFunction · 0.85
rotate_leftFunction · 0.85
addFunction · 0.85

Tested by

no test coverage detected