Exemple #1
0
 def test_insert_mult_bits(self):
     """
     Input: num = 101 (5)
     insert_mult_bits(num, 7, 3, 1): 101111 (47)
     insert_mult_bits(num, 7, 3, 0): 101111 (47)
     insert_mult_bits(num, 7, 3, 3): 111101 (61)
     """
     self.assertEqual(47, insert_mult_bits(5, 7, 3, 1))
     self.assertEqual(47, insert_mult_bits(5, 7, 3, 0))
     self.assertEqual(61, insert_mult_bits(5, 7, 3, 3))
Exemple #2
0
 def test_insert_mult_bits(self):
     """
     Input: num = 101 (5)
     insert_mult_bits(num, 7, 3, 1): 101111 (47)
     insert_mult_bits(num, 7, 3, 0): 101111 (47)
     insert_mult_bits(num, 7, 3, 3): 111101 (61)
     """
     self.assertEqual(47, insert_mult_bits(5, 7, 3, 1))
     self.assertEqual(47, insert_mult_bits(5, 7, 3, 0))
     self.assertEqual(61, insert_mult_bits(5, 7, 3, 3))
Exemple #3
0
insert_mult_bits(num, bits, len, i): insert multiple bits with len at specific position
For example:
Input: num = 101 (5)
insert_mult_bits(num, 7, 3, 1): 101111 (47)
insert_mult_bits(num, 7, 3, 0): 101111 (47)
insert_mult_bits(num, 7, 3, 3): 111101 (61)
"""

"""
Insert exact one bit at specific position
Algorithm:
1. Create a mask having bit from i to the most significant bit, and append the new bit at 0 position
2. Keep the bit from 0 position to i position ( like 000...001111)
3. Merge mask and num
"""
from algorithms.bit import insert_one_bit,insert_mult_bits

num=21
print(bin(num))

b=insert_one_bit(num, 1, 2)
print(bin(b))

num=5
print(bin(num))

b=insert_mult_bits(num, 7, 3, 1)
print(bin(b))

# 박제준 5/28