insertion insertonebitnum bit i insert exact one bit at specific position for example input num 10101 21 insertonebitnum 1 2 101101 45 insertonebitnum 0 2 101001 41 insertonebitnum 1 5 110101 53 insertonebitnum 1 0 101011 43 insertmultbitsnum bits len i insert multiple bits with len at specific position for example input num 101 5 insertmultbitsnum 7 3 1 101111 47 insertmultbitsnum 7 3 0 101111 47 insertmultbitsnum 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 create mask keep the bit from 0 position to i position 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 create mask keep the bit from 0 position to i position
def insert_one_bit(num, bit, i):
    
    mask = num >> i
    mask = (mask << 1) | bit
    mask = mask << i
    
    right = ((1 << i) - 1) & num
    return right | mask

def insert_mult_bits(num, bits, len, i):
    mask = num >> i
    mask = (mask << len) | bits
    mask = mask << i
    right = ((1 << i) - 1) & num
    return right | mask