CHAPTER 14: OPERATIONS ON BITS

EXAMPLE ON PAGE:449

In [2]:
 
def showbits(x):
    return bin(x)[2:].zfill(16)
for j in range(0,6,1):
    print "Decimal %d is same as binary" % (j)
    print showbits(j)
Decimal 0 is same as binary
0000000000000000
Decimal 1 is same as binary
0000000000000001
Decimal 2 is same as binary
0000000000000010
Decimal 3 is same as binary
0000000000000011
Decimal 4 is same as binary
0000000000000100
Decimal 5 is same as binary
0000000000000101

EXAMPLE ON PAGE:450

In [15]:
 
def showbits(x):
    return bin(x)[2:].zfill(16)
for j in range(0,4,1):
    print "Decimal %d is same as binary" % (j)
    print showbits(j)
    k=j^65535                                       #using xor for making one's complement
    print "One's complement of %d is" % (j)
    print showbits(k)
Decimal 0 is same as binary
0000000000000000
One's complement of 0 is
1111111111111111
Decimal 1 is same as binary
0000000000000001
One's complement of 1 is
1111111111111110
Decimal 2 is same as binary
0000000000000010
One's complement of 2 is
1111111111111101
Decimal 3 is same as binary
0000000000000011
One's complement of 3 is
1111111111111100

EXAMPLE ON PAGE:451

In [3]:
 
import sys
def encrypt():
    try:
        fs=open('SOURCE.txt','r')               #normal file
        ft=open('TARGET.txt','w')               #encrypted file
    except:
        print "File opening error!"
        sys.exit(1)
    while True:
        ch=fs.read(1)
        if not ch:
            break
        ft.write(bytearray([ord(ch)^0xff]))
    fs.close()
    ft.close()
encrypt()

EXAMPLE ON PAGE:452

In [3]:
 
def showbits(x):
    return bin(x)[2:].zfill(16)
i=5225
print "Decimal %d is same as binary" % (i)
print showbits(i)
for j in range(0,6,1):
    k=i>>j                                              #right shift operator
    print "%d right shift %d gives" % (i,j)
    print showbits(k)
Decimal 5225 is same as binary
0001010001101001
5225 right shift 0 gives
0001010001101001
5225 right shift 1 gives
0000101000110100
5225 right shift 2 gives
0000010100011010
5225 right shift 3 gives
0000001010001101
5225 right shift 4 gives
0000000101000110
5225 right shift 5 gives
0000000010100011

EXAMPLE ON PAGE:453-454

In [4]:
 
def showbits(x):
    return bin(x)[2:].zfill(16)
i=5225
print "Decimal %d is same as binary" % (i)
print showbits(i)
for j in range(0,5,1):
    mask = 2 ** 16 - 1
    k = (i << j) & mask                               #left shift operator
    print "%d right shift %d gives" % (i,j)
    print showbits(k)
Decimal 5225 is same as binary
0001010001101001
5225 right shift 0 gives
0001010001101001
5225 right shift 1 gives
0010100011010010
5225 right shift 2 gives
0101000110100100
5225 right shift 3 gives
1010001101001000
5225 right shift 4 gives
0100011010010000

EXAMPLE ON PAGE:457-458

In [11]:
 
d=9
m=3
y=1990
date=(y-1980)*512+m*32+d
print "Date=%u\n" % (date)
year=1980+(date>>9)
month=((date<<7)>>12)
day=((date<<11)>>11)
print "Year=%u" % (year)
print "Month=%u" % (month)
print "Day=%u" % (day)
Date=5225

Year=1990
Month=163
Day=5225

EXAMPLE ON PAGE:460

In [12]:
 
i=65
print "Value of i=%d\n" % (i)
j=i&32
if j==0:
    print "and its fifth bit is off\n"
else:
    print "and its fifth bit is on\n"
j=i&64
if j==0:
    print "whereas its sixth bit is off\n"
else:
    print "whereas its sixth bit is on\n"
Value of i=65

and its fifth bit is off

whereas its sixth bit is on

EXAMPLE ON PAGE:463

In [13]:
 
b=50
b=b^12                            #XOR operator
print "%d\n" % (b)                #this will print 62
b=b^12
print "%d\n" % (b)                #this will print 50
62

50

EXAMPLE ON PAGE:463-464

In [14]:
 
def showbit(n):
    for i in range(15,-1,-1):
        andmask=1<<i
        k=n&andmask
        if k==0:
            print 0,  
        else:
            print 1, 
    print "\n"
showbit(32)
0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0