# Ch-24 : Digital Circuits¶

## Page No. 645 Example 24.1.¶

In [6]:
#convert decimal 12 to an octal number
def base10toN(num, base):
"""Change num'' to given base
Upto base 36 is supported."""

converted_string, modstring = "", ""
currentnum = num
if not 1 < base < 37:
raise ValueError("base must be between 2 and 36")
if not num:
return '0'
while currentnum:
mod = currentnum % base
currentnum = currentnum // base
converted_string = chr(48 + mod + 7*(mod > 10)) + converted_string
return converted_string

o=base10toN(12,8)
print "The procedure is as follows."
print "12 divided by 8 = quotient 1 with a remainder of 4"
print "1 divided by 8 = quotient 0 with a remainder of 1"
print "Therefore,  decimal 12 = octal",o

The procedure is as follows.
12 divided by 8 = quotient 1 with a remainder of 4
1 divided by 8 = quotient 0 with a remainder of 1
Therefore,  decimal 12 = octal 14


## Page No. 646 Example 24.2.¶

In [10]:
# convert octal number to decimal.
d=int('144',8)
print "(i) octal 144 = decimal",d
d1=int("237",8)
print "(ii) octal 237 = decimal",d1
d2=int('120',8)
print "(iii) octal 120 = decimal",d2

(i) octal 144 = decimal 100
(ii) octal 237 = decimal 159
(iii) octal 120 = decimal 80


## Page No. 647 Example 24.3.¶

In [14]:
#convert decimal to hexadecimal number
def base10toN(num, base):
"""Change num'' to given base
Upto base 36 is supported."""

converted_string, modstring = "", ""
currentnum = num
if not 1 < base < 37:
raise ValueError("base must be between 2 and 36")
if not num:
return '0'
while currentnum:
mod = currentnum % base
currentnum = currentnum // base
converted_string = chr(48 + mod + 7*(mod > 10)) + converted_string
return converted_string

h=base10toN(112,16)
print "The procedure is as follows,"
print "(i) 112 divided by 16 = quotient 7 with a remainder of 0"
print "      7 divided by 16 = quotient 0 with a remainder of 7"
print "decimal 112 = hex",h
print "(ii) 253 divided by 16 = quotient 7 with a remainder of 13 i.e. D"
print "      15 divided by 16 = quotient 0 with a remainder of 15 i.e. F"
h=base10toN(253,16)
print "decimal 253 = hex",h

The procedure is as follows,
(i) 112 divided by 16 = quotient 7 with a remainder of 0
7 divided by 16 = quotient 0 with a remainder of 7
decimal 112 = hex 70
(ii) 253 divided by 16 = quotient 7 with a remainder of 13 i.e. D
15 divided by 16 = quotient 0 with a remainder of 15 i.e. F
decimal 253 = hex FD


## Page No. 648 Example 24.4.¶

In [19]:
#convert hexadecimal number to decimal
h=float.fromhex('4AB')
print "(i) hex 4AB = decimal",h
h=float.fromhex('23F')
print "(ii) hex 23F = decimal",h

(i) hex 4AB = decimal 1195.0
(ii) hex 23F = decimal 575.0


## Page No. 650 Example 24.5.¶

In [23]:
# multiply binary numbers
h=int('1101',2)
o=int('1100',2)
p=h*o
z=bin(p)[2:]
print "(i) 1101 x 1100 =",z
h=int('1000',2)
o=int('101',2)
p=h*o
z=bin(p)[2:]
print "(ii) 1000 x 101 =",z
h=int('1111',2)
o=int('1001',2)
p=h*o
z=bin(p)[2:]
print "(iii) 1111 x 1001 =",z

(i) 1101 x 1100 = 10011100
(ii) 1000 x 101 = 101000
(iii) 1111 x 1001 = 10000111


## Page No. 651 Example 24.6.¶

In [27]:
# perform the binary divisions

x=int('110',2)
x1=int('10',2)
x2=x/x1
x3=bin(x2)[2:]
print "(i) 110 / 10"
print "  = binary",x3
print "  = decimal",x2
x=int('1111',2)
x1=int('110',2)
x2=x/x1
x3=bin(x2)[2:]
print "(ii) 1111 / 110"
print "  = binary",x3
print "  = decimal)",x2

(i) 110 / 10
= binary 11
= decimal 3
(ii) 1111 / 110
= binary 10
= decimal) 2