Python-Ref > Data structures > Bit operations > Comparing bitfields
 
 

<-^^
Moduly
Knihovní funkce

Comparing bitfields

Expand/Shrink
  1   def int2bin( num, length=0):
  2     if not num:
  3       return "0".rjust( length, "0")
  4     binary = ""
  5     while num:
  6       binary = str(num & 1) + binary
  7       num >>= 1
  8     return binary.rjust( length, "0")
  9   
 10   if __name__ == "__main__":
 11     print int2bin( 8)
 12     print int2bin( 51, 8)
 13     
Zdroj: (bits5-1.py)
  1   field1 = int( "00011101", 2)
  2   field2 = int( "00010001", 2)
  3   
  4   # has field1 all bits in field2
  5   print field1 & field2 == field1
  6   
  7   # has field2 all bits in field1
  8   print field1 & field2 == field2
stdout:
False
True
Doba běhu: 20.7 ms