Fundamentals of Computer Programming: Jehangir Arshad Meo
Fundamentals of Computer Programming: Jehangir Arshad Meo
Computer Programming
Jehangir Arshad Meo
Lecture 3
Example:
10112 or 1011 B
Binary Representation of numbers
Binary means 2 bit number system [0 , 1].
2 Bits represent 22 = 4 3 10 2
4 11 3
Converting Decimal to Binary
For example convert 13 to Base 2
2 13
2 6 ---- 1
2 3 ---- 0
1 ---- 1
(1101)2
Converting decimal to binary
Example: 162:
162 / 2 = 81 rem 0
81 / 2 = 40 rem 1
40 / 2 = 20 rem 0
20 / 2 = 10 rem 0
10 / 2 = 5 rem 0
5/2 = 2 rem 1
2/2 = 1 rem 0
1/2 = 0 rem 1
1001000B
72D = (?) B
Some Exercises
59D = (?) B 111011B
10010001 B
145 D = (?) B
Conversion from binary number system to
decimal system
Exercises: Binary to decimal conversion
Class Room Practice:
Decimal to Binary Conversion:
(1458)10 = (10110110010)2
(66697)10 = (10000010010001001)2
(654785 )10 = (10011111110111000001)2
(110101)2 = (53)10
(10001011)2 = (139)10
(100011110110)2 = (2294)10
Example:
(3AB)H
Conversion from Decimal to Hexadecimal
Repeatedly divide by 16
16 93
5 ---- D
Conversion from Hexadecimal to Decimal
(3AB4)H to Base 10
Binary to Hexadecimal
Combine every 4 bits into one hexadecimal digit
Example: (0101 | 1111 | 1010 | 0110)2 = (5FA6)16
Octal Number System
Base is 8
8 symbols: {0,1,2,3,4,5,6,7}
Each place is weighted by the power of 8
So how many bits required to represent an octal number?????
Example:
(345)8
Conversion from Decimal to Octal
Repeatedly divide by 8
Binary to Octal
Combine every 3 bits into one octal digit.
Example: (110 | 010 | 011)2 = (623)8
Some important Conversions to Remember
Base 10 Base 2 Base 16
0 0 0
1 1 1
2 10 2
3 11 3
4 100 4
5 101 5
6 110 6
7 111 7
8 1000 8
9 1001 9
10 1010 A
11 1011 B
12 1100 C
13 1101 D
14 1110 E
15 1111 F
Signed number representation
In ordinary arithmetic, a negative number is
indicated by a minus sign and a positive number by a
plus sign.
22
Decimal to Binary :
To convert decimal fractions to binary fraction, continually multiply
fraction by base (2). The integer parts of the results give the digits from
right to left.
0.6875
2
1.375
2
0.750 0.10112
2
1.500
2
1.000 23
Example
12.3910
Separate the integer and fraction part
0.39
Convert the integer part
* 2
1210 = 11002
0.78
Convert the fractional part
* 2
0.3910 = 01102 1.56
* 2
12.3910 = 1100.01102
1.12
* 2
0.24
24
Binary addition:
Example 1:
0+0=0
1+0=1
0+1=1
1 + 1 = 10
1 + 1 + 1 = 11
Example 2: 1 1 1 carry
1001101 1001001 value 1
+0010010 + 0011001 value 2
1011111 1100010 sum
Binary Subtraction:
Example 1: