Chapter 14: Operations On Bits

Binary Conversion, Page number: 483

In [1]:
#Function definition
def showbits ( n ):
    a = 15
    for i in range(0,16):
        andmask = 1 << a
        k = n & andmask
        if k == 0:
            print  "0"
        else:
            print  "1" 
        a = a-1


#Result
for j in range(0,6):
    print "Decimal %d is same as binary "%( j )
    showbits ( j ) #function call
Decimal 0 is same as binary 
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
Decimal 1 is same as binary 
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1
Decimal 2 is same as binary 
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1
0
Decimal 3 is same as binary 
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1
1
Decimal 4 is same as binary 
0
0
0
0
0
0
0
0
0
0
0
0
0
1
0
0
Decimal 5 is same as binary 
0
0
0
0
0
0
0
0
0
0
0
0
0
1
0
1

One's Complement, Page number: 484

In [3]:
#Function definition
def showbits ( n ):
    a = 15
    for i in range(0,16):
        andmask = 1 << a
        k = n & andmask
        if k == 0:
            print  "0" 
        else:
            print  "1" 
        a = a-1


for j in range(0,4):
    print  "Decimal %d is same as binary "%(j )
    showbits ( j ) 
    k = ~j 
    print "One’s complement of %d is "%( j ) 
    showbits ( k )
Decimal 0 is same as binary 
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
One’s complement of 0 is 
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
Decimal 1 is same as binary 
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1
One’s complement of 1 is 
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
0
Decimal 2 is same as binary 
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1
0
One’s complement of 2 is 
1
1
1
1
1
1
1
1
1
1
1
1
1
1
0
1
Decimal 3 is same as binary 
0
0
0
0
0
0
0
0
0
0
0
0
0
0
1
1
One’s complement of 3 is 
1
1
1
1
1
1
1
1
1
1
1
1
1
1
0
0

File Encryption, Page number: 485

In [ ]:
#Function definition
def encrypt( ):
    fs = open ( "C:/Users/Akshatha M/Desktop/carrot.txt", "r" ) #normal file 
    ft = open ( "C:/Users/Akshatha M/Desktop/temp.txt", "w" ) # encrypted file 
    if ( not fs or not ft ):
        print "File opening error!"  
        exit()
    while (1):
        ch = fs.read(1)
        if(not ch): #EOF
            break
        else:
            ft.write(ascii(~ord(ch))) #complemented
    #close files
    fs.close()
    ft.close()


encrypt() #function call
    

Right Shift Operator, Page number: 487

In [5]:
#Function definition
def showbits ( n ):
    a = 15
    for i in range(0,16):
        andmask = 1 << a
        k = n & andmask
        if k == 0:
            print  "0" 
        else:
            print  "1" 
        a = a-1

#Variable declaration
i = 5225

print "Decimal %d is same as binary "%( i )
showbits(i)#function call

for j in range(0,6):
    k = i >>j #right shift 
    print "%d right shift %d gives " %(i, j )
    showbits ( k ) #function call 
Decimal 5225 is same as binary 
0
0
0
1
0
1
0
0
0
1
1
0
1
0
0
1
5225 right shift 0 gives 
0
0
0
1
0
1
0
0
0
1
1
0
1
0
0
1
5225 right shift 1 gives 
0
0
0
0
1
0
1
0
0
0
1
1
0
1
0
0
5225 right shift 2 gives 
0
0
0
0
0
1
0
1
0
0
0
1
1
0
1
0
5225 right shift 3 gives 
0
0
0
0
0
0
1
0
1
0
0
0
1
1
0
1
5225 right shift 4 gives 
0
0
0
0
0
0
0
1
0
1
0
0
0
1
1
0
5225 right shift 5 gives 
0
0
0
0
0
0
0
0
1
0
1
0
0
0
1
1

Left Shift Operator, Page number: 488

In [6]:
#Function definition
def showbits ( n ):
    a = 15
    for i in range(0,16):
        andmask = 1 << a
        k = n & andmask
        if k == 0:
            print  "0" 
        else:
            print  "1" 
        a = a-1

#Variable declaration
i = 5225

print  "Decimal %d is same as binary "%( i )
showbits(i)#function call

for j in range(0,6):
    k = i <<j #left shift 
    print  "%d left shift %d gives " %(i, j )
    showbits ( k ) #function call 
Decimal 5225 is same as binary 
0
0
0
1
0
1
0
0
0
1
1
0
1
0
0
1
5225 left shift 0 gives 
0
0
0
1
0
1
0
0
0
1
1
0
1
0
0
1
5225 left shift 1 gives 
0
0
1
0
1
0
0
0
1
1
0
1
0
0
1
0
5225 left shift 2 gives 
0
1
0
1
0
0
0
1
1
0
1
0
0
1
0
0
5225 left shift 3 gives 
1
0
1
0
0
0
1
1
0
1
0
0
1
0
0
0
5225 left shift 4 gives 
0
1
0
0
0
1
1
0
1
0
0
1
0
0
0
0
5225 left shift 5 gives 
1
0
0
0
1
1
0
1
0
0
1
0
0
0
0
0

Date Field in Directory, Page number: 492

In [7]:
#Variable declaration
d = 9
m = 3
y = 1990

#Calculation and result
date = ( y - 1980 ) * 512 + m * 32 + d 
print "Date = %u"%( date ) 
year = 1980 + ( date >> 9 ) 
month = ( (date << 7 ) >> 12 ) 
day = ( (date << 11 ) >> 11 ) 
print  "Year =  ", year 
print  "Month = %u" %(m)
print  "Day = %u"  %(d)
Date = 5225
Year =   1990
Month = 3
Day = 9

Bitwise AND Operator, Page number: 495

In [8]:
#Variable declaration
i = 65

print "value of i = ", i 

j = i & 32 #bitwise and
if ( j == 0 ):
    print  "and its fifth bit is off" 
else:
    print "and its fifth bit is on"

j = i & 64 #bitwise and
if ( j == 0 ):
    print "whereas its sixth bit is off" 
else:
    print "whereas its sixth bit is on" 
value of i =  65
and its fifth bit is off
whereas its sixth bit is on

Bitwise XOR Operator, Page number: 500

In [9]:
#Variable declaration
b = 50

#Calculation and result
b = b ^ 12 
print b  # this will print 62 
b = b ^ 12 
print  b  # this will print 50 
62
50
In [ ]: