Challenge:
I would like to write a method which would take
Return: the modified value based on the inputs.
Input Example:
Expected output:
i.e 3 bits are replaced with ZEROS starting from second bit position.
Current code:
def set_bit(original_int:int, start_bit_pos:int, number_of_bits:int, bit_value:bool):
if number_of_bits < 1: number_of_bits = 1
out = original_int
for i in range(number_of_bits):
if bit_value == 1:
out = out | (1<<(start_bit_pos+i))
else:
out = out & ~(1<<(start_bit_pos+i))
print('old binary: '+ '{:b}'.format(original_int),f' Decimal: {original_int}' )
print('new binary: '+ '{:b}'.format(out), f' Decimal: {out}' )
return out
set_bit(original_int = 255, start_bit_pos= 2, number_of_bits = 3, bit_value = 0)
set_bit(original_int = 64, start_bit_pos= 2, number_of_bits = 3, bit_value = 1)
Current issue:
The above code is functional, however I do feel it is an overkill, so I wonder if there is a standard python function which could do a similar thing?
1
.11
or decimal 3.111
or decimal 7.2^N-1
, which is the same as (1 << N) - 1
.You have to also shift that to the left if the starting position is not 0.
Therefore, you can simply do this:
def set_bit(original_int:int, start_bit_pos:int,
number_of_bits:int, bit_value:bool):
mask = (1 << number_of_bits) - 1 << start_bit_pos
if bit_value:
return original_int | mask
return original_int & ~mask