Example #1
0
 def test_update_bit(self):
     # 22 = 10110 --> after update bit at 3th position with
     # value 1: 30 = 11110
     self.assertEqual(30, update_bit(22, 3, 1))
     # 22 = 10110 --> after update bit at 2nd position with
     # value 0: 20 = 10010
     self.assertEqual(18, update_bit(22, 2, 0))
Example #2
0
 def test_update_bit(self):
     # 22 = 10110 --> after update bit at 3th position with value 1: 30 = 11110
     self.assertEqual(30, update_bit(22, 3, 1))
     # 22 = 10110 --> after update bit at 2nd position with value 0: 20 = 10010
     self.assertEqual(18, update_bit(22, 2, 0))
Example #3
0
"""
Fundamental bit operation:
    get_bit(num, i): get an exact bit at specific index
    set_bit(num, i): set a bit at specific index
    clear_bit(num, i): clear a bit at specific index
    update_bit(num, i, bit): update a bit at specific index
"""

"""
This function shifts 1 over by i bits, creating a value being like 0001000. By
performing an AND with num, we clear all bits other than the bit at bit i.
Finally we compare that to 0
"""

from algorithms.bit import get_bit,set_bit,clear_bit,update_bit

n=13
print("get_bit")
print(get_bit(n,3))

print("set_bit")
print(set_bit(n,2))


print("clear_bit")
print(clear_bit(n,2))


print("update_bit")
print(update_bit(n,2,3))