This notebook was prepared by Donne Martin. Source and license info is on GitHub.
number = 0b10001110, index = 3
expected = True
number = 0b10001110, index = 4
expected = 0b10011110
number = 0b10001110, index = 3
expected = 0b10000110
number = 0b10001110, index = 3
expected = 0b00000110
number = 0b10001110, index = 3
expected = 0b10000000
number = 0b10001110, index = 3, value = 1
expected = 0b10001110
number = 0b10001110, index = 3, value = 0
expected = 0b10000110
number = 0b10001110, index = 0, value = 1
expected = 0b10001111
Refer to the Solution Notebook. If you are stuck and need a hint, the solution notebook's algorithm discussion might be a good place to start.
class Bit(object):
def __init__(self, number):
# TODO: Implement me
pass
def get_bit(self, index):
# TODO: Implement me
pass
def set_bit(self, index):
# TODO: Implement me
pass
def clear_bit(self, index):
# TODO: Implement me
pass
def clear_bits_msb_to_index(self, index):
# TODO: Implement me
pass
def clear_bits_index_to_lsb(self, index):
# TODO: Implement me
pass
def update_bit(self, index, value):
# TODO: Implement me
pass
The following unit test is expected to fail until you solve the challenge.
# %load test_bit.py
import unittest
class TestBit(unittest.TestCase):
def test_bit(self):
number = int('10001110', base=2)
bit = Bit(number)
self.assertEqual(bit.get_bit(index=3), True)
expected = int('10011110', base=2)
self.assertEqual(bit.set_bit(index=4), expected)
bit = Bit(number)
expected = int('10000110', base=2)
self.assertEqual(bit.clear_bit(index=3), expected)
bit = Bit(number)
expected = int('00000110', base=2)
self.assertEqual(bit.clear_bits_msb_to_index(index=3), expected)
bit = Bit(number)
expected = int('10000000', base=2)
self.assertEqual(bit.clear_bits_index_to_lsb(index=3), expected)
bit = Bit(number)
self.assertEqual(bit.update_bit(index=3, value=1), number)
bit = Bit(number)
expected = int('10000110', base=2)
self.assertEqual(bit.update_bit(index=3, value=0), expected)
bit = Bit(number)
expected = int('10001111', base=2)
self.assertEqual(bit.update_bit(index=0, value=1), expected)
print('Success: test_bit')
def main():
test = TestBit()
test.test_bit()
if __name__ == '__main__':
main()
Review the Solution Notebook for a discussion on algorithms and code solutions.