title: Bit masking in Python
body: <p>I have a byte (from some other vendor) where the potential bit masks are as follows:</p>
  
  <p>value1 = 0x01
  value2 = 0x02
  value3 = 0x03
  value4 = 0x04
  value5 = 0x05
  value6 = 0x06
  value7 = 0x40
  value8 = 0x80</p>
  
  <p>I can count on ONE of value1 through value6 being present.  And then value7 may or may not be set.  value8 may or may not be set.</p>
  
  <p>So this is legal: value2 | value7 | value8
  This is not legal: value1 | value3 | value7</p>
  
  <p>I need to figure out whether value 7 is set, value8 is set, and what the remaining value is.</p>
  
  <p>I have the following python code.  Is there a more elegant way to do this?</p>
  
  <pre><code>value1 = 0x01
  value2 = 0x02
  value3 = 0x03
  value4 = 0x04
  value5 = 0x05
  value6 = 0x06
  value7 = 0x40
  value8 = 0x80
  
  def format_byte_as_bits(value):
      return format(value,'b').zfill(8)
  
  def mask_bits_on_byte(byte,mask):
      inverse_of_mask = mask ^ 0b11111111
      return byte &amp; inverse_of_mask
  
  def parse_byte(byte):
  
      value7_set = byte &amp; value7 == value7
      value8_set = byte &amp; value8 == value8
      byte = mask_bits_on_byte(byte,value7)
      byte = mask_bits_on_byte(byte,value8)
      base_value = byte
      return value7_set,value8_set,base_value
  
  # Example 1
  byte = value3 | value7
  value7_set,value8_set,base_value = parse_byte(byte)
  print("base_value = "+str(base_value))
  print("value7_set = "+str(value7_set))
  print("value8_set = "+str(value8_set))
  print()
  
  # Output:
  # base_value = 3
  # value7_set = True
  # value8_set = False
  
  # Example 2
  byte = value5
  value7_set,value8_set,base_value = parse_byte(byte)
  print("base_value = "+str(base_value))
  print("value7_set = "+str(value7_set))
  print("value8_set = "+str(value8_set))
  print()
  
  # Output:
  # base_value = 5
  # value7_set = False
  # value8_set = False
  
  # Example 3
  byte = value1 | value7 | value8
  value7_set,value8_set,base_value = parse_byte(byte)
  print("base_value = "+str(base_value))
  print("value7_set = "+str(value7_set))
  print("value8_set = "+str(value8_set))
  
  # Output:
  # base_value = 1
  # value7_set = True
  # value8_set = True
  </code></pre>
  
  <p>EDIT - I LOVE stackoverflow.  So many useful answers, so quickly!  You guys are awesome!  Wish I could mark all the answers.  But I'll at least give everyone an up vote!</p>
  
  <p>EDIT2 - Based on the answers below, the code is simplified to the following:</p>
  
  <pre><code>value1 = 0x01
  value2 = 0x02
  value3 = 0x03
  value4 = 0x04
  value5 = 0x05
  value6 = 0x06
  value7 = 0x40
  value8 = 0x80
  
  def parse_byte(byte):
      return byte &amp; value7, byte &amp; 0x80, byte &amp; 7
  
  # Example 1
  byte = value3 | value7
  value7_set,value8_set,base_value = parse_byte(byte)
  print("base_value = "+str(base_value))
  if value7_set: print("value7_set")
  if value8_set: print("value8_set")
  print()
  
  # Example 2
  byte = value5
  value7_set,value8_set,base_value = parse_byte(byte)
  print("base_value = "+str(base_value))
  if value7_set: print("value7_set")
  if value8_set: print("value8_set")
  print()
  
  # Example 3
  byte = value1 | value7 | value8
  value7_set,value8_set,base_value = parse_byte(byte)
  print("base_value = "+str(base_value))
  if value7_set: print("value7_set")
  if value8_set: print("value8_set")
  print()
  </code></pre>