#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
#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 )
#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
#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
#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
#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)
#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"
#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