Chapter 11: Bit operations

Example 11.1, Page number: 193

In [1]:
i1 = 4
i2 = 2

# Calculation and result
if ((i1 != 0) and (i2 != 0)) :
   print ('Both are not zero\n')
Both are not zero

Example 11.2, Page number: 201

In [2]:
HIGH_SPEED = 1 << 7
DIRECT_CONNECT = 1 << 8

flags = 0
flags |= HIGH_SPEED
flags |= DIRECT_CONNECT

# Calculation and result
if ((flags & HIGH_SPEED) != 0) :
   print ('High speed set\n')

if ((flags & DIRECT_CONNECT) != 0) :
   print ('Direct connect set\n')
High speed set

Direct connect set

Example 11.4, Page number: 207

In [3]:
i = 0x80

# Calculation and result
if (i != 0) :
   print ('i is %x (%d) \n' % (i, i))
   i = i >> 1
i is 80 (128)