0

For example, I have a number 255 that is 1111 1111 in binary form. I want to turn the first bit to 0 and get the 0111 1111 (127). What is the easiest way to do this? How I can change the bits of number directly?

splash27
  • 1,907
  • 6
  • 23
  • 47
  • 3
    The more generic question and solution: http://stackoverflow.com/questions/12173774/modify-bits-in-an-integer-in-python – amito Sep 06 '15 at 07:36

1 Answers1

5
a = 0b11111111    # = 255
mask = 0b01111111 # = 127

res = a & mask
print('{0} {0:08b}'.format(res))
# -> 127 01111111
hiro protagonist
  • 40,708
  • 13
  • 78
  • 98