# Chapter 13: Number Systems¶

## example 13.1, Page No. 474¶

In [20]:
# Decimal to binary conversion: 10

import math
#variable declaration
N = 10                      # decimal no to be convered to binary
k = N
#Calculations
i = 0
b =[0,0,0,0,0,0]
while(N!=0):
b[i] = N%2
N = N/2
i = i+1

#Result
print("Binary equivalent of decimal %d is %d%d%d%d"%(k,b[3],b[2],b[1],b[0]))

Binari equivalent of decimal 10 is 1010


## example 13.2, Page No.474¶

In [23]:
# Decimal to binary conversion: 25

import math
#variable declaration
N = 25                      # decimal no to be convered to binary

#Calculations
k = N
i = 0
b =[0,0,0,0,0,0]
while(N!=0):
b[i] = N%2
N = N/2
i = i+1

#Result
print("Binary equivalent of decimal %d is %d%d%d%d%d"%(k,b[4],b[3],b[2],b[1],b[0]))

Binari equivalent of decimal 25 is 11001


## example 13.3, Page No. 474¶

In [25]:
# Binary to decimal:101110

import math
b0 = 0         # bit 0
b1 = 1         # bit 1
b2 = 1         # bit 2
b3 = 1         # bit 3
b4 = 0         # bit 4
b5 = 1         # bit 5

#Calculations
D = (2**0)*b0+(2**1)*b1+(2**2)*b2+(2**3)*b3+(2**4)*b4+(2**5)*b5

#Result
print("Decimal no is %d"%D)

Decimal no is 46


## example 13.4, Page No. 475¶

In [33]:
# Decimal to binary conversion: 15,31

import math
#######################################################################################
# for N = 15
#variable declaration
N = 15                      # decimal no to be convered to binary

#Calculations
k = N
i = 0
b =[0,0,0,0,0,0]
while(N!=0):
b[i] = N%2
N = N/2
i = i+1

#Result
print("(a)\nBinary equivalent of decimal %d is %d%d%d%d%d"%(k,b[4],b[3],b[2],b[1],b[0]))

########################################################################################

# For N =31
#variable declaration
N = 31                      # decimal no to be convered to binary

#Calculations
k = N
i = 0
b =[0,0,0,0,0,0]
while(N!=0):
b[i] = N%2
N = N/2
i = i+1

#Result
print("Binary equivalent of decimal %d is %d%d%d%d%d"%(k,b[4],b[3],b[2],b[1],b[0]))
##########################################################################################
c= bin(15+31)[2:]
print("(b) Addition of 15 and 31 is %s in binary. Decimal equivalent of %s is %d"%(c,c,int(c,2)))

(a)
Binari equivalent of decimal 15 is 01111
Binari equivalent of decimal 31 is 11111
(b) Addition of 15 and 31 is 101110 in binary. Decimal equivalent of 101110 is 46


## example 13.5, Page No.475¶

In [42]:
# Binary subtraction

import math
#Variable declaration
n1 = 11001             # first number
n2 = 10001             # second number

#calculations
n1 = int(str(n1),2)
n2 = int(str(n2),2)
c = bin(n1-n2)[2:]

#Result
print("Subtraction result in binary = %s"%c)

Subtraction result in binary = 1000


## example 13.6, Page No.475¶

In [41]:
# Binary subtraction

import math
#Variable declaration
n1 = 1010             # first number
n2 = 111              # second number

#calculations
n1 = int(str(n1),2)
n2 = int(str(n2),2)
c = bin(n1-n2)[2:]

#Result
print("Subtraction result in binary = %s"%c)

Subtraction result in binary = 11


## example 13.7, Page No. 476¶

In [87]:
# 16- bit signed binary representation

import math
#Variable declaration
a = 8
b = -8
c = 165
d = -165

#Calculations
#c = bin(d)
a = format(a,'#018b')[2:]
b = format(b,'#018b')[3:]
c = format(c,'#018b')[2:]
d = format(d,'#018b')[3:]
str = '1'
#Result
print("In the leading bit we will have 1 to represent '-' sign\n")
print("(a) +8   --> %s"%(a))
print("(b) -8   --> %s%s"%(str,b))
print("(c) +167 --> %s"%(c))
print("(d) -167 --> %s%s"%(str,d))

a = format(-167%(1<<16),'016b')
b = format(167%(1<<16),'016b')
print a
print b

In the leading bit we will have 1 to represent '-' sign

(a) +8   --> 0000000000001000
(b) -8   --> 1000000000001000
(c) +167 --> 0000000010100101
(d) -167 --> 1000000010100101
1111111101011001
0000000010100111


## example 13.8, Page No.477¶

In [95]:
# 2's complement

import math
# variable declaration
a = int('00011111',2)
b = int('11100101',2)
c = int('11110111',2)

#Calculations
a = format(-a%(1<<8),'08b')
b = format(-b%(1<<8),'08b')
c = format(-c%(1<<8),'08b')
print("(a) 2's complement of 00011111 --> %s " %a)
print("(b) 2's complement of 11100101 --> %s " %b)
print("(c) 2's complement of 11110111 --> %s " %c)

(a) 2's complement of 00011111 --> 11100001
(b) 2's complement of 11100101 --> 00011011
(c) 2's complement of 11110111 --> 00001001


## example 13.9, Page No. 13.9¶

In [97]:
# 8-bit number range

import math
#calculations
a = int('01111111',2)
b = int('10000000',2)

#Result
print("largest positive 8-bit no in %d and smallest negative number is -%d"%(a,b))

largest positive 8-bit no in 127 and smallest negative number is -128


## example 13.10, Page No.10¶

In [116]:
# 2's complement and addition,subtraction

import math
#Variable declaration
A = -24
B = 16
#Calculations
#2's complement
a = format(A%(1<<8),'08b')
b = format(-B%(1<<8),'08b')
c = bin(A+B)[3:]
c = format(-int(c,2)%(1<<8),'08b')
#Subtraction
d = bin(A-B)[3:]
d = format(-int(d,2)%(1<<8),'08b')
#Result
print("(a)\n2's complement of -24 --> %s " %a)
print("2's complement of 16 --> %s " %b)
print("(b) A+B = %s"%c)
print("(c) A-B = %s"%d)

(a)
2's complement of -24 --> 11101000
2's complement of 16 --> 11110000
(b) A+B = 11111000
(c) A-B = 11011000


## example 13.11, Page No. 479¶

In [130]:
# 2's complement and addition,subtraction

import math
#Variable declaration
A = -60
B = -28
#Calculations
#2's complement
a = format(A%(1<<8),'08b')
b = format(B%(1<<8),'08b')
c = bin(A+B)[3:]
c = format(-int(c,2)%(1<<8),'08b')
#Subtraction
x = B-A
d = bin(B-A)[2:]
d = format(int(d,2),'08b')
#Result
print("(a)\n2's complement of A --> %s " %a)
print("2's complement of B --> %s " %b)
print("(b) B+A = %s"%c)
print("(c) B-A = %s"%d)

(a)
2's complement of A --> 11000100
2's complement of B --> 11100100
(b) B+A = 10101000
(c) B-A = 00100000


## example 13.12, Page No. 479¶

In [230]:
# decimal to binary

# For N = 0.6875
#variable declaration
N = 0.6875                      # decimal no to be convered to binary

#Calculations
k = N
i = 0
b =[0,0,0,0,0,0,0,0,0,0,0,0]
while(N!=0):
N = N*2
#print N
b[i]=0
#print b[i]
if (N>1) or(N==1):
N =N-1
b[i]=1
i=i+1

#Result
print("Binary equivalent of decimal %f is 0.%d%d%d%d "%(k,b[0],b[1],b[2],b[3]))

Binary equivalent of decimal 0.687500 is 0.1011


## example 13.13, Page No.480¶

In [222]:
# decimal to binary

# For N = 0.634
#variable declaration
N = 0.634                      # decimal no to be convered to binary

#Calculations
k = N
i = 0
b =[0,0,0,0,0,0,0,0,0,0,0,0]
while(i!=7):
N = N*2
#print N
b[i]=0
#print b[i]
if (N>1) or(N==1):
N =N-1
b[i]=1
i=i+1

#Result
#Result
print("Binary equivalent of decimal %f is 0.%d%d%d%d%d%d%d "%(k,b[0],b[1],b[2],b[3],b[4],b[5],b[6]))

Binary equivalent of decimal 0.634000 is 0.1010001


## example 13.14, Page No.480¶

In [225]:
# decimal to binary

# For N = 0.634
#variable declaration
N = 39.12                      # decimal no to be convered to binary

N1 = 39
k = N1
#Calculations
i = 0
b =[0,0,0,0,0,0,0,0]
while(N1!=0):
b[i] = N1%2
N1 = N1/2
i = i+1

#Result
print("Binary equivalent of decimal %d is %d%d%d%d%d%d%d"%(k,b[6],b[5],b[4],b[3],b[2],b[1],b[0]))

N2 =0.12
#Calculations
k = N2
i = 0
b =[0,0,0,0,0,0,0,0,0,0,0,0]
while(i!=7):
N2 = N2*2
#print N
b[i]=0
#print b[i]
if (N2>1) or(N2==1):
N2 =N2-1
b[i]=1
i=i+1

#Result
print("Binary equivalent of decimal %f is 0.%d%d%d%d%d%d%d "%(k,b[0],b[1],b[2],b[3],b[4],b[5],b[6]))

Binary equivalent of decimal 39 is 0100111
Binary equivalent of decimal 0.120000 is 0.0001111


## example 13.15, Page No.481¶

In [227]:
# binary Addition

import math
#Variable declaration
a1 = int('101101',2)       # integer part of first no
a2 = int('0101',2)         # fractiona part of first no
b1 = int('10001',2)        # integer part of second no
b2 = int('1010',2)         # fractiona part of second no(in fraction we can add any no of 0s)

#Calculations
c1= bin(a1+b1)[2:]
c2 = bin(a2+b2)[2:]


Addition --> 111110.1111


## example 13.16, Page No. 481¶

In [231]:
# binary to decimal conversion

import math
#Variable declaration
i0 = 1               # LSB of integer
i1 = 0
i2 = 0
i3 = 1
i4 = 1               # MSB of integer
f1 = 0
f2 = 0
f3 = 1
f4 = 0
f5 = 1
f6 = 1

#Calculations
D = i0*(2**0)+i1*(2**1)+i2*(2**2)+i3*(2**3)+i4*(2**4)+f1*(2**-1)+f2*(2**-2)+f3*(2**-3)+f4*(2**-4)+f5*(2**-5)+f6*(2**-6)

#Result
print("Decimal equivalent = %f"%D)

Decimal equivalent = 25.171875


## example 13.17, Page No. 482¶

In [236]:
# Hexadecimal to decimal

import math
#variable declaration
a = 3
b = 10  # decimal equivalent of A
c = 8
#Calculations
# binary equvalent of  8            A                   3
#                    1000         1010                0011
# a
Da = (1*2**0)+(1*2**1)+(0*2**2)+(0*2**3)+(0*2**4)+(1*2**5)+(0*2**6)+(1*2**7)+(0*2**8)+(0*2**9)+(0*2**10)+(1*2**11)
print("(a) Decimal equivalent of 8A3 is %d"%Da)
# b
Db = (a*(16**0))+(b*(16**1))+(c*(16**2))
print("(b) Decimal equivalent of 8A3 is %d"%Db)

(a) Decimal equivalent of 8A3 is 2211
(b) Decimal equivalent of 8A3 is 2211


## example 13.18, Page No. 482¶

In [260]:
# decimal to hexadecimal

import math
#Variable declaration
N = 268                # no to be converted into hexadecimal

N1 = 268
k = N1
#Calculations
i = 0
b =[0,0,0,0,0,0,0,0]
while(N1!=0):
b[i] = N1%16
N1 = N1/16
i = i+1

#Result
print("Remainder1: %d = C\nRemainder2: %d\nRemainder3: %d\n\nHexaecimal equivalent of %d = 10C"%(b[0],b[1],b[2],k))

Remainder1: 12 = C
Remainder2: 0
Remainder3: 1

Hexaecimal equivalent of 268 = 10C


## example 13.19, Page No.483¶

In [259]:
# decimal to hexadecimal

import math
#Variable declaration
N = 5741                # no to be converted into hexadecimal

N1 = 5741
k = N1
#Calculations
i = 0
b =[0,0,0,0,0,0,0,0]
while(N1!=0):
b[i] = N1%16
N1 = N1/16
i = i+1

#Result
print("Remainder1: %d = D\nRemainder2: %d\nRemainder3: %d\nRemainder4: %d\n\nHexaecimal equivalent of %d =166D"%(b[0],b[1],b[2],b[3],k))

Remainder1: 13 = D
Remainder2: 6
Remainder3: 6
Remainder4: 1

Hexaecimal equivalent of 5741 =166D


## example 13.20, Page No.483¶

In [262]:
# Hexadecimal to decimal

import math
#variable declaration
a = 0
b = 7
c = 13  #  decimal equivalent of D
#Calculations
D = (a*(16**0))+(b*(16**1))+(c*(16**2))

#Result
print("(b) Decimal equivalent of 8A3 is %d"%D)

(b) Decimal equivalent of 8A3 is 3440