Chapter-4
Number Systems and Computer Codes: Binary Representation of Data:
In order to work with data, the data must be represented inside the computer. Digital computers represent data by means of an easily identified symbol called a digit.
Bit is the form of ‘binary digit’. It is smallest possible unit of data. A bit can be either 0 or 1. Nibble is a group of four bits.
Byte is a group of eight bits. Word is a group of two bytes.
Term Size(bits) Example
Bit 1 0
Nibble 4 1010
Byte 8 0101 1100
word 16 0101 1100 0101 1100
Numbering Systems:
Each number system has a base also called a Radix. A decimal number system is a system of base 10; Binary number system is a system of base 2; Octal number system is a system of base 8;
Hexadecimal number system is a system of base 16. In base 10, we can count from 0 to 9, that is,10 digits.
Number System Base Symbols used
Binary 2 0,1
Decimal 10 0,1,2,3,4,5,6,7,8,9
Octal 8 0,1,2,3,4,5,6,7
Hexadecimal 16 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F
Model-1:
Convert Binary to Decimal
Convert Binary to Octal
Convert Binary to Hexadecimal
Binary Number:
A number is said to be a binary number, when its base is 2 and it contains only two digits, 0 and 1.
In binary number, each and every numbers are represented by these two digits . Example 1: 0101 is equal to 5.
Example 2: 1111 is equal to 15. Decimal Number:
A number is said be a decimal number, when its base is 10 and it can be written with the help of ten digits that are 0, 1, 2, 3, 4, 5, 6, 7, 8, 9.
All the number can be written with the help of these ten digits. Example 1: 41
Example 2: 551 Example 3: 674
Converting Binary to Decimal:
Question 1: Convert 1101 to decimal number? Solution:
Binary number is 1101. So, 1101 = (1 X 23
) + (1 X 22
) + (0 X 21
) + (1 X 20
) = (1 X 8) + (1 X 4) + (0 X 2) + (1 X 1) = 8 + 4 + 0 + 1
Correct answer is 13
Question 2: Convert 1001 to decimal number? Solution:
Binary number is 1001. So, 1001 = (1 X 23
) + (0 X 2+2) + (0 X 21
) + (1 X 20
) = (1 X 8) + (0 X 4) + (0 X 2) + (1 X 1) = + 0 + 0 + 1
Correct answer is 9
In this article we are going to learn about binary to octal conversion. Here we convert a number to the base 2 to the number to the base 8.
We cannot directly convert binary to octal, so we first convert binary to decimal the decimal to octal number system. Binary numbers are commonly used in computers and octal numbers are used in electronics.
Octal number:
The number to the base 8 is called as octal numbers. It uses numbers from 0 to 7. The numbers 8 and 9 are not there in octal number system. It is denoted by a8 where a is a
number with digits 0 to 7.
Examples : (1) 21458 (2) 71658 (3)468
Question 1: Convert (1010101)2 to octal
Solution:
Given binary number is (1010101)2
First we convert given binary to decimal
(1010101)2 = (1 * 26) + (0 * 25 ) + (1 * 24) + (0 * 23) + (1 * 22) + (0 * 21) + (1 * 20)
= 64 + 0 + 16 + 0 + 4 + 0 + 1 = 64 + 21
= 85 (Decimal form)
Now we will convert this decimal to octal form 8 | 85
---8 | 10 -- 5 ---8 | 1 -- 2 ---8 | 0 --1
Correct answer is equivalent octal form is (125)8
Question 2: Convert (01101)2 to octal
Solution:
Given binary number is (01101)2
First we convert given binary to decimal
(01101)2 = (0 * 24) + (1 * 23) + (1 * 22) + (0 * 21) + (1 *20)
Now we will convert this decimal to octal form 8 | 13
---8 | 1 -- 5 ---8 | 0 -- 1
Correct answer is equivalent octal form is (15)8
Convert to Binary Hexadecimal:
Both binary numbers and hexadecimal numbers are generally used in digital electronic system.
In binary, we represent the number in two digits, 0 and 1. In binary number, the base is two. In hexadecimal number, the base is sixteen.
The sixteen digits used in this number system are numbers from 0-9 and alphabets from A - F. We can't directly convert binary to hexadecimal. First, we have to convert binary to decimal number system. Then, convert that decimal number to hexadecimal number system.
Binary Decimal Hexadecimal
0000 0 0
0001 1 1
0010 2 2
0011 3 3
0100 4 4
0101 5 5
0110 6 6
0111 7 7
1000 8 8
1001 9 9
1010 10 A
1011 11 B
1100 12 C
1101 13 D
1110 14 E
Question 1: Convert 01011011 in hexadecimal number. Solution:
The given binary number is 01011011 Now, we convert it first to decimal number So, 01011011 =(0 X 27
) + (1 X 26
) + (0 X 25
) + (1 X 24
) + (1 X 23
) + (0 X 22
) + (1 X 21
) + (1 X 20
) = (0 X 128) + (1 X 64) + (0 X 32) + (1 X 16) + (1 X 8) + (0 X 4) + (1 X 2) + (1 X 1)
= 0 + 64 + 0 + 16 + 8 + 0 + 2 + 1
= 91 (decimal form of binary number).
Now, we have to change it into hexadecimal number. So, 91 is greater than 16. So, we have to divide by 16. After dividing by 16, quotient is 5 and remainder is 11. Remainder is less than 16.
Hexadecimal number of remainder is B.
Quotient is 5 and hexadecimal number of remainder is B. That is, 91 = 16 * 5 +11
5 = 16 * 0 + 5
So, 5B is the hexadecimal number equivalent to decimal number 91.
Question 2: Convert 00010111 in hexadecimal number. Solution:
The given binary number is 00010111 Now, we convert it first to decimal number
So, 00010111 =(0 X 27) + (0 X 26) + (0 X 25) + (1 X 24) + (0 X 23) + (1 X 22) + (1 X 21) + (1 X 20)
= (0 X 128) + (0 X 64) + (0 X 32) + (1 X 16) + (0 X 8) + (1 X 4) + (1 X 2) + (1 X 1) = 0 + 0 + 0 + 16 + 0 + 4 + 2 + 1
= 23 ( It is in decimal from)
Now, we have to change it to hexadecimal number. So, 23 is greater than 16, so we have to divide it by 16. After dividing by 16 , quotient is 1 and remainder is 7. Remainder is less than 16.
The hexadecimal number of remainder is 7.
Model-2:
Convert Octal to Binary
Convert Octal to Decimal
Convert Octal to Hexadecimal.
Convert Octal to Binary:
Here we are going to learn how to convert an octal to binary. That is here we are converting a number to the base 8 to the equivalent number to the base 2.
In converting an octal number to an binary number we follow two steps. First we convert the given octal number to the equivalent decimal number. Then the second step it to convert this decimal number to its equivalent binary number. The obtained binary number will be
equivalent to given octal number. Examples
Question 1: Convert (41)8 to a binary number.
Solution:
Given number is (41)8
(41)8 = (4 * 81) + (1 * 8+0)
= 4 * 8 + 1 * 1 = 32+1
= 33(Decimal number)
Now convert this decimal number to a binary number. 2 | 33
2 | 16 -- 1 2 | 8 -- 0 2 | 4 --0 2 | 2 -- 0 1 -- 0
The binary number is (100001)2
Question 2: Convert (27)8 to a binary number.
Solution:
Given number is (27)8
(27)8 = (2 * 8+1) + (7 * 80)
= 2 * 8 + 7 * 1 = 16 + 7
= 23(Decimal number)
Now convert this decimal number to a binary number. 2 | 23
2 | 11 -- 1 2 | 5 -- 1 2 | 2 --1 2 | 1 -- 0 2 | 0 -- 1
The binary number is (10111)2
(27)8 = (10111)2
Question 3: Convert (10)8to a binary number.
Solution:
Given number is (10)8
(10)8 = (1 * 81) + (0 * 80)
= 1 * 8 + 0 * 1 = 8 + 0
Now convert this decimal number to a binary number. 2 | 8 -- 0
2 | 4 --0 2 | 2 -- 0 1
The binary number is (1000)2
(10)8 = (1000)2
Convert Octal to Decimal: Examples
Question 1: Convert (5746)8 to decimal number
Solution:
The given number is (5746)8
(5746)8 = (5 * 83)+ (7 * 82) + (4 * 81) + (6 * 80)
=5 * 512 + 7 * 64 + 4 * 8 + 6 * 1 = 2560+448+32+6
= 3046
The equivalent decimal number for (5746)8 is 3046
(5746)8 = 3046
Question 2: Convert (201)8 to decimal number
Solution:
The given number is (201)8
(201)8 = (2 * 82) + (0 * 81) + (1 * 80)
= 2 * 64 + 0 * 8 + 1 * 1 = 128+0+1
The equivalent decimal number for (201)8 is 129
(201)8 = 129
Question 3: Convert (6.1)8 to decimal number
Solution:
The given number is (6.1)8
(6.1)= (6 * 80
) + (1 * 1/8) = 6 * 1 + 1/8
= 6 + 1/8 = 6 + 0.125 = 6.125
The equivalent decimal number for (6.1)8 is 6.125
(6.1)8= 6.125
Convert Octal to Hexadecimal:
Question 1: Convert (1002)8 to hexadecimal
Solution:
The given number is (1002)8
(1002)8 = (1 * 83)+ (0 * 82) + (0 * 81) + (2 * 80)
=1 * 512 + 0 * 64 + 0 * 8 + 2 * 1 = 512 + 0 + 0+ 2
Now we convert the above decimal to hexadecimal 16 | 514
16 | 32 --2 2 -- 0
The hexadecimal number is 202 (1002)8 = (202)16
Question 2: Convert (100)8 to hexadecimal
Solution:
The given number is (100)8
(100)8 = (1 * 82) + (0 * 81) + (0 * 80)
= 1 * 64 + 0 * 8 + 0 * 1 = 64 + 0 + 0
= 64(decimal number)
Now we convert the above decimal to hexadecimal 16 | 64
16 | 4 --0 16 | 0 -- 4
The hexadecimal number is 40 (100)8 = (40)16
Question 3: Convert (15)8 to hexadecimal
Solution:
The given number is (15)8
(15)8 =(1 * 8+1) + (5 * 80)
= 1 * 8 + 5 * 1 = 8 + 5
Now we convert the above decimal to hexadecimal
13 is less than 16 so the equivalent hexadecimal number is D The hexadecimal number is D
(15)8 =(D)16
Model-3:
Convert Hexadecimal to Binary
Convert hexadecimal to Decimal
Convert hexadecimal to octal
Convert Hexadecimal to Binary
Here it is not possible to convert it directly, we will convert hexadecimal to decimal then that decimal number is converted to binary.
Example:
1) A116 2) EE916 3) FD65416
Question 1: Convert (A2B)16 to a equivalent binary number
Solution:
Given hexadecimal number is (A2B)16
(A2B)16 = (A * 162) + (2 * 161) + (B * 160)
= (A * 256) + (2 * 16) + (B * 1) = (10 *256) + 32 + 11
= 2560 + 43
Now we have to convert 2603 to binary 2 | 2603
2 |1301 -- 1 2 | 650 -- 1 2 | 325 -- 0 2 | 162 -- 1 2 | 81 -- 0 2 | 40 -- 1 2 | 20 -- 0 2 | 10 -- 0 2 | 5 -- 0 2 | 2 -- 1 2 | 1 -- 0 2 | 0 -- 1
The binary number is (101000101011)2
(A2B)16 = (101000101011)2
Question 2: Convert (E)16 to a equivalent binary number
Solution:
Given hexadecimal number is E (E)16 = E * 160
= E * 1 = E
=14(Decimal number)
Now we have to convert 14 to binary 2 |14
2 | 7 -- 0 2 | 3 -- 1 2 | 1 -- 1 2 | 0 -- 1
The binary number is (1110)2
(E)16 = (1110)2
Solution:
Given hexadecimal number is (30)16
(30)16 = (3 * 161) + (0 * 160)
= 48 + 0
= 48(Decimal number)
Now we have to convert 48 to binary 2 | 48
2 | 24 -- 0 2 | 12 -- 0 2 | 6 -- 0 2 | 3 -- 0 2 | 1 -- 1 2 | 0 -- 1
The binary number is (110000)2
(30)16 = (110000)2
Convert hexadecimal to octal
Question 1: Find the equivalent octal form of (C1)16
Solution:
Given hexadecimal number is (C1)16
(C1)16 = (C * 161) + (1 * 160)
= C * 16 + 1 * 1 =12 * 16 + 1 = 192 + 1
8 | 193 8 | 24 -- 1 8 | 3 -- 0 8 | 0 --3
The octal number is (301)8
(C1)16 = (301)8
Question 2: Find the equivalent octal form of (F)16
Solution:
Given hexadecimal number is (F)16
(F)16 = (F * 160)
= F * 1 = F
= 15(Decimal form)
Now we have to convert this decimal to octal 8 | 15
8 | 1 -- 7 8 | 0 -- 1
The octal number is (17)8
(F)16 = (17)8
Question 3: Find the equivalent octal form of (105)16
Solution:
Given hexadecimal number is (105)16
(105)16 = (1 * 162) + (0 * 161) + (5 * 160)
= 261(Decimal form)
Now we have to convert this decimal to octal 8 | 261
8 | 32 -- 5 8 | 4 -- 0 8 | 0 --4
The octal number is (405)8
(105)16 = (405)8
Convert hexadecimal to Decimal:
Question 1: Convert (7B)16 into decimal number.
Solution:
Given hexadecimal number is (7B)16.
(7B)16 = 161 * 7 + 160 * B
= 16 * 7 + 1 * B = 112 + 1 * 11 = 112 + 11 = 123
Answer is 123.
Question 2: Convert (7E)16 into decimal number.
Solution:
Given hexadecimal number is (7E)16.
= 112 + 14 = 126
Answer is 126.
Question 3: Convert (CA)16 into decimal number.
Solution:
Given hexadecimal number is (CA)16.
(CA)16 = 161 * C + 160 * A
= 16 * C + 1 * A = 16 *C + A =16 * 12 +9 =192 + 9 =202
Answer is 202.
Question 4: Convert (20)16 into decimal number.
Solution:
Given hexadecimal number is (20)16.
(20)16 = 2 * 161 + 0 * 160
= 2 * 16 + 0 * 1 = 2 * 16 + 0 = 2 * 16 = 32
Question 5: Convert (24)16 into decimal number.
Solution:
Given hexadecimal number is (24)16.
(24)16 = 2 * 161 + 4 * 160
= 2 * 16 + 4 * 1 = 2 * 16 + 4 = 32+ 4 =36
Answer is 36.
Fraction conversions:
Converting Binary fraction to decimal: Example
Convert (11011.101)2 to decimal
24
23
22
21
20 .
2-1
2-2
2-3
= (1 x 24
)+ (1 x 23
)+ (0 x 22
)+ (1 x 20
)+ (1 x 2-1
)+ (0 x 2-2
)+ (1 x 2-3
) = 16+8+0+1+0.5+0+0.125
= (25.625)10
Converting Decimal fraction to Binary Example
Convert (0.75)10 to binary
Multiply the given fraction by 2. Keep the integer in the product as it is and multiply the new fraction in the product by 2. Continue the process till the required number of decimal places or till you get zero in the fraction part. Record the integers in the products from top to bottom.
Example:
Convert (105.15)10 to binary
Let us convert 105 first. (105)10 = (1101001)2
Let us convert (0.15)10
Multiply 0.15 by 2 0.30 Multiply 0.30 by 2 0.60 Multiply 0.60 by 2 1.20 Multiply 0.20 by 2 0.40 Multiply 0.40 by 2 0.80 Multiply 0.80 by 2 1.60
Reading the integers from top to bottom (0.15)10 = (0.001001)2
Final result (105.15)10 = (1101001.001001)2
Converting Decimal fraction to Octal Example:
Convert (0.75)10 to Octal
Multiply the given fraction by 8. Keep the integer in the product as it is and multiply the new fraction in the product by 8. Continue the process and read the integers in the products from top to bottom.
Given fraction 0.75 Multiply 0.75 by 8 6.00
Reading the integers from top to bottom 0.75 in decimal number system is 0.6 in octal number system.
Convert (23.25)8 to decimal
81
80
. 8-1
8-2
2 3 2 5
= (2 x 81
)+ (3 x 80
)+ (2 x 8-1
)+ (5 x 8-2
) = 16+3+0.25+0.07812
= (19.32812)10
Converting Decimal fraction to Hexadecimal Example
Convert (0.75)10 to hexadecimal
Multiply the given fraction by 16. Keep the integer in the product as it is and multiply the new fraction in the product by 16. Continue the process and read the integers in the products from top to bottom.
Given fraction 0.75 Multiply 0.75 by 16 12.00 - C
Reading the integers from top to bottom 0.75 in decimal number system is 0C in Hexadecimal number system.
Hexadecimal fraction to decimal Example
Convert (1E.8C)16 to decimal
161
160
. 16-1
16-2
1 E 8 C
= (1 x 161
)+ (14 x 160
)+ (8 x 16-1
)+ (12 x 16-2
) = 16+14+0.5+0.04688
We take a binary number in groups of 4 and use the appropriate hexadecimal digit in it’s place. We begin at the rightmost 4 bits. If we are not able to form a group of four, insert 0s to the left until we get all groups of 4 bits each. Write the hexadecimal equivalent of each group. Repeat the steps until all groups have been converted.
Example
Convert the binary number 1000101 to its Hexadecimal equivalent.
0100 0101 Note that we needed to insert a 0 to the left of 100. 4 5
Answer: (45)16
In case of a fractional binary number form groups of four bits on each side of decimal point. Then replace each group by its corresponding hexadecimal number.
Example
Convert (11100.1010)2 to hexadecimal equivalent.
0001 1100 . 1010
1 C . A
Answer : (1C.A)16
Binary Addition:
Adding Two Binary Numbers:Adding binary number is similar to the addition of decimal numbers. While performing binary addition, start by adding the bits (digits) in one column at a time, from right to left as we do in the case of adding decimal numbers.
Rules of binary addition: Rule-1: 0+0=0 Rule-2: 0+1=1 Rule-3: 1+0=1
Rule-4: 1+1=1, and carry 1 to the next most significant bit. Let us consider the following example:
0001010 + 0100100 0101110
11 ---Carry 1011
+ 0001
1100
Binary Subtraction:
Subtracting two binary numbers:
The binary subtraction will be performed on the two binary numbers. Rules of binary subtraction:
Rule-1: 0-0=0
Rule-2: 1-0=1
Rule-3: 1-1=0
Rule-4: 0-1=1, and borrow 1 from most significant bit. Consider the following examples:
Ex-1:
1011 - 1001 0010
Ex-2: 11 222
1000101 Minuend - 0011000 Subtrahend
---Two’s complement has two cases:
Carry bit case
No carry bit case No Carry bit case:
Once again take 2’s complement of sum (added value) and put minus sign and give answer. carry bit case:
Left out carry bit and note the remaining value as answer. Example-1 for carry bit case:
Calculate 1011-1001 using two’s complement method. Sol:
Find the two’s complement of the subtrahend. Subtrahend=1001
One’s complement of subtrahend = 0110.
Two’s complement of subtrahend = 0110+1=0111(here add 1 with 0110). Add the minuend and the two’s complement of the subtrahend.
1011 + 0111
---1 0010
---Now discard the carry from the last bit. Hence the result is 0010. Multiplying two Binary Numbers:
Binary numbers are multiplied in the same manner as we multiply two decimal number. The rules of multiplications of binary numbers are the same as that of decimal numbers that is:
Note that since binary operates in base 2, the multiplication rules we need to remember are those that involve 0 and 1 only. As an example of binary multiplication we have 101 times 11, 101
x11
First we multiply 101 by 1, which produces 101. Then we put a 0 as a placeholder as we would in decimal multiplication, and multiply 101 by 1, which produces 101.
101 x11 101
1010 <-- the 0 here is the placeholder
The next step, as with decimal multiplication, is to add. The results from our previous step indicates that we must add 101 and 1010, the sum of which is 1111.
101 x11 101 1010 1111
Dividing two Binary Numbers:
Binary division Binary division is similar to dividing two decimal number. Take for example the division of 1011 into 11.
11 R=10 11 )1011 -11 101 -11
10 <-- remainder, R
To check our answer, we first multiply our divisor 11 by our quotient 11. Then we add its' product to the remainder 10, and compare it to our dividend of 1011.
11 x 11 11 11
1001 <-- product of 11 and 11 1001
+ 10
1011 <-- sum of product and remainder
The sum is equal to our initial dividend, therefore our solution is correct.
Binary Representation of Integers
Binary number can be represented only by using 0’s and 1’s, but cannot use the sign (-) to denote the negative number or sign (+) to denote the positive number. So it must be either 0 or 1. There are three methods to represent binary number. They are
(i) Sign and magnitude method (ii) One’s complement method (iii) Two’s complement method
Sign and magnitude method
In this method, first bit is considered as a sign bit. Here positive number starts with 0 and negative number starts with 1.
Example 25
25/2 = 12 1
12/2 = 6 0
6/2 = 3 0
3/2 = 1 1
1/2 = 0 1
So the binary number is (11001)2. If we take the size of the word is 1 byte, then the number 25 will be represented as 00011001
Suppose, if the number is -25, and then it will be represented as 10011001
One’s Complement Method
Example
Represent 86 in one’s complement method (I byte representation)
86/2 = 43 0
43/2 = 21 1
21/2 = 10 1
10/2 = 5 0
5/2 = 2 1
2/2 = 1 0
1/2 = 0 1
The binary number is 1010110
1 byte representation of number 86 is 01010110
(ii) Represent -55 in one’s complement method (I byte representation)
55/2 = 27 1
27/2 = 13 1
13/2 = 6 1
6/2 = 3 0
3/2 = 1 1
1/2 = 0 1
The binary number is 110111 1 byte representation is 00110111
The given number is negative; hence we need to calculate one’s complement One’s complement of 00110111 is 11001000 (convert 1 into 0 and 0 into 1) Thus, the 1 byte representation of number -55 is 11001000.
Two’s Complement method
Example
(i) Represent 87 in two’s complement method (I byte representation)
87/2 = 43 1
43/2 = 21 1
21/2 = 10 1
10/2 = 5 0
5/2 = 2 1
2/2 = 1 0
1/2 = 0 1
The binary number is 1010111
Hence, the 1 byte representation of number 87 is 01010111
(ii) Represent -54 two’s complement method (I byte representation)
54/2 = 27 0
27/2 = 13 1
13/2 = 6 1
6/2 = 3 0
3/2 = 1 1
½ = 0 1
The binary number is 110110
Hence, the 1 byte representation is 00110110
The given number is negative; hence we need to calculate two’s complement. One’s complement of 00110110 is 11001001 (convert 1 into 0 and 0 into 1) Add 1 to one’s complement
1
11001001 (1+1=2, binary equivalent =11) + 1
---11001010
Thus, 1 byte representation of number -54 is 11001010
(iii) Represent -77 two’s complement method (I byte representation)
77/2 = 38 1
38/2 = 19 0
19/2 = 9 1
4/2 = 2 0
2/2 = 1 0
1/2 = 0 1
The binary number is 1001101
Hence, 1 byte representation is 01001101.
Since the given number is negative, we need to calculate two’s complement. One’s complement of 01001101 is 10110010
10110010 + 1 ---10110011
Thus, 1 byte representation of number -77 is 10110011. BCD CODE :
Binary coded decimal(BCD) is a very simple technique used for encoding decimal numbers. The main advantage of using BCD is that it allows easy conversion to decimal digits.
Now let us consider some examples, where we convert the given decimal numbers to BCD. The 8421 BCD code for 9.2 is 1001.0010.
In packed BCD representation, two decimal digits are stored in a single byte. For example, Decimal 17=0001 0111(in BCD).
Now consider another example.
Consider the unpacked BCD number, 0000 0011 0000 0110. Convert it into its equivalent decimal number.
Now, in the number, identify LSB.
So, 0000 0011 0000 0110LSB. Group th binary digits into sets of eight because the number is in
unpacked BCD format.
00000011 00000110
3 6