Python Basics -Number
System
CSIR12 Introduction to computer Programming
NIT Trichy
Binary, octal, hexadecimal numbers
• The decimal system is the most widely used number system.
However, computers only understand binary.
• Binary, octal and hexadecimal number systems are closely related.
3/25/24 NIT Trichy 2
Decimal Number
• What does 5634 represents?
• Expanding 5634,
5 x 103 = 5000
+ 6 x 102 = 600
+ 3 x 101 = 30
+ 4 x 100 = 4 -> 5634
• The 10 in the above expansion is called radix or base
• Digits of decimal numbers are 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, that is, there are 10
unique digits called base or radix.
• Radix or base of decimal number is 10
3/25/24 NIT Trichy 3
Binary
• Binary numbers are made of binary digits (bits) 0 and 1.
• The radix of binary is 2.
• Group of 8 bits are called byte (11001001)2
• Group of 4 bits are called nibble (1000)2
3/25/24 NIT Trichy 4
Octal Numbers
• Octal numbers are made of octal digits 0, 1, 2, 3, 4, 5, 6, 7.
• The radix number is 8.
3/25/24 NIT Trichy 5
Hexadecimal Number
• Hexadecimal numbers are made of digits 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A,
B, C, D, E, F (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15).
• The radix number is 16.
3/25/24 NIT Trichy 6
Decimal to binary
• Divide the number repeatedly by 2 until the quotient becomes 0.
• The remainders from bottom to top represents binary equivalent
3/25/24 NIT Trichy 7
Decimal to binary - Repeated Division method
344
344 / 2 = 172 remainder = 0
172 / 2 = 86 remainder = 0
86 / 2 = 43 remainder = 0
43 / 2 = 21 remainder = 1
21 / 2 = 10 remainder = 1
10 / 2 = 5 remainder = 0
5 / 2 = 2 remainder = 1
2 / 2 = 1 remainder = 0
1 / 2 = 0 remainder = 1
Binary Representation is 101011000
3/25/24 NIT Trichy 8
Decimal to binary method 2
• Subtract the largest power of 2 that gives a positive result and record
the power.
• Repeat, subtracting from the prior result, until the reminder is zero.
• Place 1’s in the positions in the binary result corresponding to the
power recorded; in all other positions place 0.
3/25/24 NIT Trichy 9
Method 2
Example 625
625 – 512 (29) = 113 -> 9
113 – 64 (26) = 49 -> 6
49 – 32 (25) = 17 -> 5
17 – 16 (24) = 1 -> 4
1 – 1 (20) = 0 -> 0
The Binary representation is 1001110001
3/25/24 NIT Trichy 10
Try using method 2 convert decimal to binary
• 344
• 1450
3/25/24 NIT Trichy 11
Method 2
Example 344
344 – 256 (28) = 88 -> 8
88 – 64 (26) = 24 -> 6
24 – 16 (24) = 8 -> 4
8 – 8 (23) = 0 -> 3
The Binary representation is 101011000
3/25/24 NIT Trichy 12
Method 2
Example 1450
1450 – 1024 (210) = 426 -> 10
426 – 256 (28) = 170 -> 8
170 – 128 (27) = 42 -> 7
42 – 32 (25) = 10 -> 5
10 – 8 (23) = 2 -> 3
2 – 2 (21) = 0 -> 1
The Binary representation is 10110101010
3/25/24 NIT Trichy 13
Convert Fractional part to binary
• Repeatedly multiply the fraction by radix (2) and save the integer
digits of the result.
• The new radix fraction digits are the integer numbers in computed
order.
3/25/24 NIT Trichy 14
Fractional part
Example 344.6
Binary representation of Integer Part: 101011000
0.6 x 2 = 1.2 -> 1
0.2 x 2 = 0.4 -> 0
0.4 x 2 = 0.8 -> 0
0.8 x 2 = 1.6 -> 1
0.6 x 2 = 1.2 -> 1
Binary Representation of fraction part: 10011
(344.6)10 = (101011000.10011)2
3/25/24 NIT Trichy 15
Convert binary to decimal
To Convert to decimal, use decimal arithmetic to sum the weighted
powers of two:
Converting (11010)2 to N10:
N10 = 1 x 24 + 1 x 23 + 0 x 22 + 1 x 21 + 0 x 20
= 26
3/25/24 NIT Trichy 16
Fractional part binary to decimal – Method 1
• Divide each digit from right side of radix point till the end by
21, 22, 23, … respectively.
• Add all the result from previous step.
• The result is the equivalent fractional decimal number.
3/25/24 NIT Trichy 17
Example Binary to Decimal Fractional part
101011000.10011
! ! ! ! !
1x +0x +0x +1x +1x
"! "" "# "$ "%
= 0.5 + 0 + 0 + 0.0625 + 0.03125
= 0.59375
3/25/24 NIT Trichy 18
Fractional part binary to decimal – Method 2
• Start from the right with the total of 0.
• Take your current total, add the current digit and divide
the result by 2.
• Continue until there are no more digits left.
3/25/24 NIT Trichy 19
Example
101011000.10011
(Start from right)
!
(1 + 0 ) = 0.5
"
!
(1 + 0.5 ) = 0.75
"
!
(0 + 0.75 ) = 0.375
"
!
(0 + 0.375 ) = 0.1875
"
!
(1 + 0.1875 ) = 0.59375
"
Fraction part decimal value is 0.59375
3/25/24 NIT Trichy 20
Decimal to octal
• Divide the number repeatedly by 8 until the quotient becomes 0.
• The remainders from bottom to top represents octal equivalent
3/25/24 NIT Trichy 21
Example
344
344 / 8 = 43 remainder = 0
43 / 8 = 5 remainder = 3
5 / 8 = 0 remainder = 5
Octal Representation is (530)8
3/25/24 NIT Trichy 22
Fractional part
• Multiply the fractional part repeatedly by 8 until it becomes 0.
• If the fractional part does not become 0, stop after a certain number
of steps.
• The resultant integer part from top to bottom represents the octal
equivalent of the fractional part.
3/25/24 NIT Trichy 23
Example
344.6
Integer part octal representation is 530
fractional part
0.6 * 8 = 4.8
0.8 * 8 = 6.4
0.4 * 8 = 3.2
0.2 * 8 = 1.6
0.6 * 8 = 4.8
The Octal representation of 344.6 is 530.46314
3/25/24 NIT Trichy 24
Decimal to hexadecimal
• Divide the number repeatedly by 16 until the quotient
becomes 0.
• Remainders from bottom to top represents hexadecimal
number.
Example: (100)10
100 / 16 = 6 remainder = 4
6 / 16 = 0 remainder = 6
Hexadecimal representation is 64
3/25/24 NIT Trichy 25
Try
• 344
• 910
3/25/24 NIT Trichy 26
Example
(344)10
344 / 16 = 21 remainder = 8
21 / 16 = 1 remainder = 5
1 / 16 = 0 remainder = 1
Hexadecimal representation is (158)16
3/25/24 NIT Trichy 27
Example
(910)10
910 / 16 = 56 remainder = 14 or E
56 / 16 = 3 remainder = 8
3 / 16 = 0 remainder = 3
Hexadecimal representation is (38E)16
3/25/24 NIT Trichy 28
Python program to convert decimal (Integer)
to other number systems
Example:
dec = 344
print("The decimal value of", dec, "is:")
print(bin(dec), "in binary.")
print(oct(dec), "in octal.")
print(hex(dec), "in hexadecimal.")
3/25/24 NIT Trichy 29
Output
The decimal value of 344 is:
0b101011000 in binary.
0o530 in octal.
0x158 in hexadecimal.
3/25/24 NIT Trichy 30