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