## page no 622
## example no A.1
## BINARY INTO HEX AND OCTAL
def dec2hex(n):
return "%X" % n
def bin2dec(string_num):
return int(string_num, 2)
def dec2oct(n):
return "%o" % n;
print ('Binary no= 10011010 \n \n')#
st='10011010'#
h=bin2dec(st)#
H=dec2hex(h)#
print ('Hex Equivalent= ')#
print(H)#
print('\n')
O=dec2oct(h)#
print ('Octal Equivalent= ')
print(O)#
## page no 623
## example no A.2
## SUBTRACTION OF TWO NUMBERS
print ('Minuend: 52 \n');
print ('Subtrahend: 23 \n \n')
print ('BORROW METHOD \n \n');
m=5*10+2; ## minuend
s=2*10+3; ## subtrahend
## to subtract 3 from 2, 10 must be borrowed from the second place of the minuend.
m=4*10+12;
sub=m-s;
print ('Subtraction= ')
print (sub);
print ('\n \n 10s COMPLEMENT METHOD \n \n');
## 9's complement of 23 is
n=99-23;
## add 1 to the 9's complement to find the 10's complement
t=n+1;
## add the 10's complement of the subtrahend(23) to minuend(52) to subtract 23 from 52
a=m+t;
## subtract 100 from a to compensate for the 100 that was added to find the 10's complement of 23
sub=a-100;
print ('Subtraction= ');
print (sub);
## page no 624
## example no A.3
## SUBTRACTION OF TWO NUMBERS
print ('Minuend: 23 \n');
print ('Subtrahend: 52 \n \n')
print ('BORROW METHOD \n \n');
m=2*10+3; ## minuend
s=5*10+2; ## subtrahend
## subtraction of the digits in the first place results in
a=3-2;
## to subtract the digits in the second place a borrow is required from the third place. assuming 1 at third place.
x=12-5; ## with a borrowed 1 from the third place
sub=10*x+a;
print ('Subtraction= ')
print (sub);
print ('this is negative 29, expressed in 10s complement. \n negative sign is verified by the borrowed 1 from the third place.');
print ('\n \n 10s COMPLEMENT METHOD \n \n');
## 9's complement of 52 is
n=99-52;
## add 1 to the 9's complement to find the 10's complement
t=n+1;
## add the 10's complement of the subtrahend(23) to minuend(52) to subtract 23 from 52
a=m+t;
print ('Subtraction= ');
print (a);
print ('this is negative 29, expressed in 10s complement');
# page no 625
# example no A.4
# 2's COMPLIMENT OF BINARY NUMBER
def bin2dec(b):
x = int(b, 2)
if b[0] == '1': # "sign bit", big-endian
x -= 2**len(b)
return x
def bitcmp(a):
mask = (1 << a.bit_length()) - 1
b=(a ^ mask)
return int(b)
def dec2bin(n):
return ''.join(str(1 & int(n) >> i) for i in range(8)[::-1])
print('Given binary no= 00011100 \n \n')#
string='00011100'
d=bin2dec(string)#
x=bitcmp(d)#
s=x+1
f=s-32
y=dec2bin(f)#
print('2s complement=')#
print(y)#
## page no 626
## example no A.5
## SUBTRACTION OF TWO NUMBERS
def hex2dec(s):
return int(s, 16)
def dec2hex(n):
return "%X" % n
def bitcmp(a):
mask = (1 << a.bit_length()) - 1
b=(a ^ mask)
return int(b)
print ('Subtrahend= 32H \n')#
print ('Minuend= 45H \n \n')#
## finding 2's complement of subtrahend (32H)#
m=hex2dec('45')#
x=hex2dec('32')#
y=bitcmp(x)# ## 1's compliment of 32H
z=y+1# ## 2's compliment of 32H
s=m+z#
f=s-64# ## to compensate the effect of 2's compliment
e=dec2hex(f)#
print ('Subtraction= ')#
print (e)#
## page no 626
## example no A.6
## SUBTRACTION OF TWO NUMBERS
def hex2dec(s):
return int(s, 16)
def dec2hex(n):
return "%X" % n
def bitcmp(a):
mask = (1 << a.bit_length()) - 1
b=(a ^ mask)
return int(b)
def bin2dec(b):
x = int(b, 2)
if b[0] == '1': # "sign bit", big-endian
x -= 2**len(b)
return x
def dec2bin(n):
return ''.join(str(1 & int(n) >> i) for i in range(8)[::-1])
print ('Subtrahend= 45H \n')#
print ('Minuend= 32H \n \n')#
## finding 2's complement of subtrahend (32H)#
m=hex2dec('32')#
x=hex2dec('45')#
y=bitcmp(x)# ## 1's compliment of 32H
z=y+1# ## 2's compliment of 32H
s=m+z#
f=s+128
r=dec2hex(f)#
print ('Subtraction= ')#
print (r)#
print('in hexadecimal \n')
d=hex2dec(r)#
x=bitcmp(d)#
s=x-1
f=s+2
y=dec2hex(f)#
print ('The result is negative & it is expressed in 2s complement.')
print(y)#
print('in hexadecimal \n')
## page no 628
## example no A.7
## SUBTRACTION OF UNSIGNED NUMBERS
def dec2hex(n):
return "%X" % n
def hex2dec(s):
return int(s, 16)
def bitcmp(a):
mask = (1 << a.bit_length()) - 1
b=(a ^ mask)
return int(b)
print ('Part a \n \n')
print ('Subtrahend= 62H \n')#
print ('Minuend= FAH \n \n')#
## finding 2's complement of subtrahend (62H)#
m=hex2dec('FA')#
x=hex2dec('62')#
y=bitcmp(x)
z=y+1# ##2's compliment of 62H
s=m+z#
f=s-128# ## to compensate the effect of 2's compliment
e=dec2hex(f)#
print ('Subtraction= ')#
print (e)#
print ('This result is positive \n \n')#
print ('Part b \n \n')
print ('Subtrahend= FAH \n')#
print ('Minuend= 62H \n \n')#
## finding 2's complement of subtrahend (FAH)#
m=hex2dec('62')#
x=hex2dec('FA')#
y=bitcmp(x)# ## 1's compliment of FAH
z=y+1# ##2's compliment of 62H
s=m+z#
r=dec2hex(s)#
print ('Subtraction= ')#
print (r)#
print ('The result is negative & it is expressed in 2s complement.')
# page no 629
# example no A.8
# SUBTRACTION OF SIGNED NUMBERS
def hex2dec(s):
return int(s, 16)
def bitcmp(a):
mask = (1 << a.bit_length()) - 1
b=(a ^ mask)
return int(b)
def dec2hex(n):
return "%X" % n
print('Part a \n \n')
print('Minuend= FAH \n \n')#
print('It is a negative no since D7= 1 for FAH, this must be represented in \n2s compliment form. \n')#
# finding 2's complement of subtrahend (FAH)#
m=hex2dec('FA')#
x=hex2dec('62')#
y=bitcmp(m)# # 1's compliment of FAH
z=y+1# # 2's compliment of FAH
print('2s compliment of minuend is= ')#
print(z)#
print('in hexadecimal \n')
print('\n \n Subtrahend= 62H \n')#
print('It is a positive no since D7= 0 for 62H. \n')#
# subtraction can be represented as
# FAH-62H= (-06H)-(+62H)
s=-x-z#
a=-s#
d=dec2hex(a)#
print('Subtraction= ')#
print(s)#
print('\n')
print(d)#
print('in hexadecimal with a negative sign \n \n')#
g=bitcmp(a)# # 1's compliment of result
q=g+129# # 2's compliment of result
e=dec2hex(q)#
print('2s compliment of result would be= ')#
print(e)#
##page no 629
##example no A.9
## ADDITION OF TWO POSITIVE NUMBERS
##the given numbers are 41H & 54H.
def hex2dec(s):
return int(s, 16)
def dec2hex(n):
return "%X" % n
A=hex2dec('41')#
B=hex2dec('54')
Y=A+B#
X=dec2hex(Y)#
print ('Sum =')
print (X)#
print('in hexadecimal \n')
if Y>255: ## checking the carry flag.
print ('CY=1 \n \n')
else:
print ('CY=0 \n \n')
if Y>127: ## checking the sign flag.
print ('S=1 \n \n')
else:
print ('S=0 \n \n')
if Y>0: ## checking the zero flag.
print ('Z=0 \n \n')
else:
print ('Z=1 \n \n')