Chapter 4 Data Representation
Chapter 4 Data Representation
R
Data Representation
U
Cell Number: 0729 260 641
Email: rangulu@mmust.ac.ke
Introduction
UL
In this topic, we will learn concepts of data representation. We will start by discussing various number
systems used by computers to represent numbers. This will be followed by a brief introduction to coding
schemes. These are the techniques (systems) used by computers to represent data in memory.
G
Objectives
By the end of this topic, you should be able to
AN
5. Convert decimal numbers to octal and back from octal to decimal and binary
.
6. Convert decimal numbers to hexadecimal and back from hexadecimal to decimal, binary and octal
DR
Learning Activities
The learning activities (Exercise questions) are presented after every sub-topic
Chapter 3
Data Representation
R
3.1 Number Systems
U
There are infinite ways to represent a number. The four commonly associated with modern computers and
digital electronics are: decimal, binary, octal, and hexadecimal.
You can see from above, to represent number 15 in decimal, we use a 1 and a 5 from the 10 digits used to
represent numbers in decimal, i.e 0 - 9.
Now, now do you represent two hundred and fifty eight in decimal number system? Sounds like a grade
four question, but it is very important as it will form a firm basis to understanding the other number systems.
The answer is straight forward, two hundred and fifty eight in decimal is represented as 25810 . Just as
you did in mathematics, the base is written as a subscript.
.
Now, read this number out loudly: 258. I hope you did not say Two Fifty Eight, that is wrong. The
DR
correct way to read this number is Two hundred and fifty eight. This tells us that, the 2 in the number
represents 200, the 5 represents 50 and the 8 represents 8. That is why we read the 2 as two hundred,
the 5 as fifty and the 8 as eight.
You can see clearly that 200 + 50 + 8 = 258. Now lets look at the next concept.
Everything that has a base has a power. Decimal number system represents numbers to a base of 10.
What does this mean? This means, every digit in a decimal number has a base of 10 and a power. The
power of the digit corresponds to the position of the digit, with the right-most digit having a power of 0
and the powers increase as you move towards the left. For instance,
In our number 25810 , 8 has power 0, 5 has power 1 and 2 has power 2 as shown in Figure 3.1.
2 Dr. Raphael Angulu, BIT 113 SCI, MMUST, 2020: Data Representation Notes
2 5 8
2 1 0
The base of these digits is the same, 10. Therefore, the number 25810 is actually representation of
25810 = 2 × 102 + 5 × 101 + 8 × 100
= 2 × 100 + 5 × 10 + 8 × 1
R
= 200 + 50 + 8
. . . thats why we read this number as ”two hundred and fifty eight. . . ” and write it as
U
= 25810
Am sure you still remember that any number n raised to power 0 is equal to 1. As in, n0 = 1. The powers
follow the same thing you learnt in class 2, do you remember ones, tens, hundreds, thousands etc? You can
UL
see we also start from right-left when assigning power to whole decimal numbers.
For fractional decimal numbers, we assign powers to the digits from left-to-right starting with the first
digit after the decimal point taking power -1. For example, a fraction like 0.72510 , 7 will have power -1, 2
will have power -2 while 5 will have power -3. This fraction is actually represented as
0.72510 = 7 × 10−1 + 2 × 10−2 + 8 × 10−3
G
1 1 1 1
=7× +2× 2 +5× 3 remember a−b =
101 10 10 ab
AN
7 2 5
= + +
10 100 1000
= 0.72510
.
Putting these two numbers together, the number 258.72510 will look as shown in Figure 3.2.
DR
2 5 8 · 7 2 5
2 1 0 −1 −2 −3
Do you notice the powers look like reversed number line we did in form one? Positives are on the left,
negatives on the right.
Do you notice something with this counting? You must have noticed two things
Dr. Raphael Angulu, BIT 113 SCI, MMUST, 2020: Data Representation Notes 3
• That, to get the next number, we add one to the current number
• That, when you add one to the highest number in the set (for this case 9), you get 0 carry 1. Just
as you did in class 1 a decade or so ago. This is how we move from 9 to 10, from 19 to 20, from 29
to 30, from 39 to 40 etc. The same concept applies to other number systems as we will see
shortly
R
Binary counting goes (read as zero, one, two, three. . . fifteen, sixteen)
0, 1, 10, 11, 100, 101, 110, 111, 1000, 1001, 1010, 1011, 1100, 1101, 1110, 1111, 10000 and so on
From the counting above, you can see to represent number 510 in binary is 1012 . How do we get the
U
next number after 1012 ? We just add 1 to 1012 and we get (six) which is 1102 . This is done as
101 [5 in decimal]
+1 [1 in decimal]
UL 110 [6 in decimal]
You can notice from this example that, when we add 1 to 1 in binary, we get 0 carry 1. Remember, 1 is
the highest digit in the set 0, 1 used by binary number systems to represent numbers. Same thing we saw
with 9 in decimal number system.
G
Just like in the decimal number system, every digit has a power. The digit in the far right (just before
the dot in the case of fractional parts) has a power of 0 and the powers increase towards the left. In binary,
the number five is represented as shown in Figure 3.3.
AN
1 0 1
2 1 0
The base of each digit is 2. Therefore, number five in binary is actually a representation of
1012 = 1 × 22 + 0 × 21 + 1 × 20
.
=1×4+0×2+1×1 remember n0 = 1
DR
= 510
4 Dr. Raphael Angulu, BIT 113 SCI, MMUST, 2020: Data Representation Notes
3. Repeat step 2 above until the remainder is 0 (Remainder will only be 0 if quotient is less than 2)
4. Read the remainders from down upwards.
Examples
R
Reading these remainders from down upwards, we find that 810 = 10002
2. Convert the decimal number 1110 to binary
11 ÷ 2 = 5 rem 1 → 1
U
5 ÷ 2 = 2 rem 1 → 1
2 ÷ 2 = 1 rem 0 → 0
1 ÷ 2 = 0 rem 1 → 1
UL
Reading these remainders from down upwards, we find that 1110 = 10112
3. Convert decimal number 2710 to binary
27 ÷ 2 = 13 rem 1 → 1
13 ÷ 2 = 6 rem 1 → 1
G
6 ÷ 2 = 3 rem 0 → 0
3 ÷ 2 = 1 rem 1 → 1
1 ÷ 2 = 0 rem 1 → 1
AN
Reading these remainders from down upwards, we find that 2710 = 110112
Exercise
Convert the following numbers from decimal to binary: 45, 189, 23, 87, 64
2. Multiply the resulting fractional part by 2 and keep the whole number 0.50 × 2 = 1.00 so you pick 1.
3. Repeat step 2 above until the fractional part becomes 0 (or until you reach satisfactory accuracy)
4. Read the whole numbers from up downwards.
Example
Dr. Raphael Angulu, BIT 113 SCI, MMUST, 2020: Data Representation Notes 5
2. Convert the number 0.72510 to binary
0.725 × 2 = 1.450 → 1
0.450 × 2 = 0.900 → 0
0.900 × 2 = 1.800 → 1
0.800 × 2 = 1.600 → 1
0.600 × 2 = 1.200 → 1
0.200 × 2 = 0.400 → 0
0.400 × 2 = 0.800 → 0
0.800 × 2 = 1.600 → 1
R
This fraction is recurring, so we can stop there and say, 0.72510 = 0.101110012 by reading whole
numbers from up-downwards.
U
0.125 × 2 = 0.250 → 0
0.250 × 2 = 0.500 → 0
0.500 × 2 = 1.000 → 1
Exercise
UL
Reading whole numbers from up-downwards we get 0.12510 = 0.0012
Convert the following numbers from decimal to binary: 0.62510 , 0.712510 , 0.812510 , 82.42510
1. Starting from the rightmost digit, assign powers to each digit, with the rightmost digit having power
0
AN
Examples
1 0 1 1
.
3 2 1 0
DR
= (1 × 23 ) + (0 × 22 ) + (1 × 21 ) + (1 × 20 )
= (1 × 8) + (0 × 4) + (1 × 2) + (1 × 1)
= 1110
6 Dr. Raphael Angulu, BIT 113 SCI, MMUST, 2020: Data Representation Notes
2. Convert 11100112 to decimal
1 1 1 0 0 1 1
6 5 4 3 2 1 0
= (1 × 26 ) + (1 × 25 ) + (1 × 24 ) + (0 × 23 ) + (0 × 22 ) + (1 × 21 ) + (1 × 20 )
R
= 64 + 32 + 16 + 0 + 0 + 2 + 1
= 11510
U
Exercise
Convert the following numbers to decimal: 1011111012 , 101112 , 100001112
UL
Converting fractions numbers from Binary to Decimal
To convert fractions from binary to decimal
1. Starting with first digit after the decimal point (dot), assign powers as you move to the right, with
first digit after decimal point having power -1.
Examples
AN
0 · 0 0 1
−1 −2 −3
1 1 1 1
=0× 1
+0× 2 +1× 3 remember a−b =
2 2 2 ab
1 1 1
=0× +0× +1×
2 4 8
1
= = 0.12510
8
Dr. Raphael Angulu, BIT 113 SCI, MMUST, 2020: Data Representation Notes 7
2. Convert 0.101110012 to decimal
0 · 1 0 1 1 1 0 0 1
−1 −2 −3 −4 −5 −6 −7 −8
1 1 1 1 1 1 1 1
= 1× 1 + 0× 2 + 1× 3 + 1× 4 + 1× 5 + 0× 6 + 0× 7 + 1× 8
R
2 2 2 2 2 2 2 2
1 1 1 1 1 1 1 1
= 1× + 0× + 1× + 1× + 1× + 0× + 0× + 1×
2 4 8 16 32 64 128 256
U
1 1 1 1 1
= + 0 + + + + 0 + 0 +
2 8 16 32 256
Exercise
= 0.7226562510
UL
Convert the following fractions to decimal: 0.110012 , 0.1102 , 0.01012
G
3.1.3 Octal Number System
This is a base 8 number system. It uses 8 digits to represent any number. These digits are 0 t0 7.
AN
Remember, when you add one to the highest number in the set (for this case 7), you get 0 carry 1.
Every digit in an octal number has a base of 8 and a power that corresponds to the digits position. For
example, in the counting above 108 = 810 . Lets prove this.
.
1 0
1 0
DR
= (1 × 81 ) + (0 × 80 )
= (1 × 8) + (0 × 1)
= 810
8 Dr. Raphael Angulu, BIT 113 SCI, MMUST, 2020: Data Representation Notes
1 2
1 0
= (1 × 81 ) + (2 × 80 )
= (1 × 8) + (2 × 1)
=8+2
R
= 1010
U
1. Divide the number by 8 and keep the remainder. 10 ÷ 8 = 1 rem 2
UL
3. Repeat step 2 above until the quotient becomes 0
Example
19 ÷ 8 = 2 rem 3 → 3
2 ÷ 8 = 0 rem 2 → 2
Exercise
DR
1. Starting with the rightmost digit, assign powers, with the rightmost digit taking power 0
Dr. Raphael Angulu, BIT 113 SCI, MMUST, 2020: Data Representation Notes 9
Examples
2 4 6
2 1 0
= (2 × 82 ) + (4 × 81 ) + (6 × 80 )
R
= (2 × 64) + (4 × 8) + (6 × 1)
= 128 + 32 + 6
U
= 16610
UL 1
2
5
1
8
0
= (1 × 82 ) + (5 × 81 ) + (8 × 80 )
G
= (1 × 64) + (5 × 8) + (8 × 1)
AN
= 64 + 40 + 8
= 11210
Exercise
Convert the following numbers to decimal: 3458 , 7418 and 2148
.
NOTE: Converting fractional numbers from decimal to octal and vice versa follows the same procedure as
in binary.
DR
1. Starting with the least significant digit (rightmost digit), group the binary digits into sets of three
(put leading zeros if last group has less than three bits)
2. For each set, assign the octal digit from the lookup table shown in Table 3.1
3. Join the octal digits to form the octal representation of a binary number
10 Dr. Raphael Angulu, BIT 113 SCI, MMUST, 2020: Data Representation Notes
Binary 000 001 010 011 100 101 110 111
Octal 0 1 2 3 4 5 6 7
Table 3.1: Lookup Table for Converting from binary to octal and octal to binary
Example
R
= 3468
U
= 1 1 3 4 7
= 113478
Exercise
UL
Convert the following numbers to octal: 1001001111012 , 101111012 and 10111012
Examples
= 3 4 6
= 011 100 110
= 0111001102
.
= 2 4 5 3
= 010 100 101 011
= 0101001010112
Exercise
Convert the following numbers to binary: 34768 , 21348 and 6528
Dr. Raphael Angulu, BIT 113 SCI, MMUST, 2020: Data Representation Notes 11
Converting from decimal to hexadecimal
To convert from decimal to hexadecimal
1. Divide the number by 16 and keep the remainder (If the remainder is between 10 and 15, replace it
with respective alphabet between A and F)
R
Example
43 ÷ 16 = 2 rem 11(B) → B
2 ÷ 16 = 0 rem 2 → 2
U
Reading the remainders from down upwards, 4310 = 2B16
Exercise
G
Convert the following numbers to hexadecimal: 5610 , 43510 , 2410
1. Starting with the rightmost digit, assign each digit a power with the rightmost digit having power 0
2. Multiply each digit by 16 raised to the digits power. If the hexa symbol is an alphabet, replace it with
corresponding value between 10 and 15
Examples
DR
2 0
1 0
= (2 × 161 ) + (0 × 160 )
= (2 × 16) + (0 × 1)
= 3210
12 Dr. Raphael Angulu, BIT 113 SCI, MMUST, 2020: Data Representation Notes
E(14) A(10)
1 0
= (E × 161 ) + (A × 160 )
= (14 × 161 ) + (10 × 160 )
= (14 × 16) + (10 × 1)
R
= 224 + 10
= 23410
Exercise
Convert the following numbers to decimal: A8B16 , 3CD16 and 4F B16
U
Converting from binary to hexadecimal
UL
To convert from binary to hexadecimal
1. Starting from the rightmost digit, group the binary digits in sets of 4 bits (add leading zeros if last set
has less than 4 bits)
2. Check the corresponding hexadecimal digit in the lookup table shown in Table 3.2
G
Binary 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111
Hexa 0 1 2 3 4 5 6 7 8 9 A B C D E F
Table 3.2: Lookup Table for Converting from binary to hexadecimal and hexadecimal to binary
AN
Examples
= B 7 B
= B7B16
Exercise
Convert the following numbers to hexadecimal: 101112 , 111110111102 and 1011011111012
Dr. Raphael Angulu, BIT 113 SCI, MMUST, 2020: Data Representation Notes 13
Converting from hexadecimal to binary
To convert from hexadecimal to binary
1. For each hexadecimal digit, look for corresponding 4 digit binary in the lookup table in Table 3.2.
Examples
= B 7 B
R
= 1011 0111 1011
= 1011011110112
U
= A 4 F
= 1010 0100 1111
= 1010010011112
Exercise
UL
Convert the following numbers to binary: ABCD16 , D9C16 and 45616
When adding binary numbers, always ensure the digits are right-aligned. Remember, 1 + 1 in binary is equal
to 0 carry 1. A carried bit is added to the next bit on the left. Lets look at examples
1. Add 101 + 11
0101 [5 in decimal]
+ 0011 [3 in decimal]
1000 [8 in decimal]
.
DR
We added the right-most digits (1 + 1) and we got 0 carry 1. We added the carried bit to the next
left bit and added 1 + 0 + 1 and got 0 carry 1. The carried bit is added to the next left bit 1 + 1 and
get 0 carry 1. The carry is added to the left bit and we get 1. So, 101 + 11 = 1000
Exercise
Perform the following binary arithmetic: 10111001112 + 100012 and 100111102 + 101101111012
14 Dr. Raphael Angulu, BIT 113 SCI, MMUST, 2020: Data Representation Notes
3.2.2 Binary subtraction
There are two ways we can subtract binary numbers
Direct Subtraction
This follows the same approach as subtraction in decimal number system. Important point to remember is,
When we borrow from a right digit to the left digit, we borrow 2 (the base).
R
Wait, you did the following subtraction in primary school
42
+ 15
U
27
Your mathematics teacher in primary must have told you to solve this, since 2 - 5 is impossible, you borrow
1 from 4 add it to 2 to get 12 then you do 12 - 5 = 7. As much as you believed them, you did not notice
that 1 + 2 6= 12 but 10 + 2 = 12.
UL
This tells you that, when you borrow from a most significant digit to a less significant digit, what you
are borrowing is the base. For decimal, we borrow 10 and for binary we borrow 2.
1. Ensure the minuend (the number from which another number is to subtracted) and the subtrahend
(number to be subtracted from another number) have same number of bits, add leading zeros where
necessary. For example (8 - 3), 1000 - 11 you need to make it look like 1000 - 0011
Dr. Raphael Angulu, BIT 113 SCI, MMUST, 2020: Data Representation Notes 15
2. Convert the subtrahend into 1’s compliment by flip-flopping the bits. (Change 0 to 1 and 1 to 0). 1’s
compliment of 1001101 is 0110010
3. Add 1 to the 1’s compliment to get the 2’s compliment. For 1’s compliment above, 2;s compliment is
0
4. Add the 2’compliment to the minuend (2’s compliment is the negative of the subtrahend)
5. The final answer should have same number of bits as the minuend (or subtrahend). Any overflow bit
is discarded
Examples
1. Perform 1000 - 11
R
1000 [8 in decimal]
− 11 [3 in decimal]
U
Ensure subtrahend has same bits with minuend. Subtrahend becomes 0011
1’s compliment of subtrahend 1100
UL
2’s compliment of subtrahend
1000
+ 1101
1100 + 1 = 1101
[8 in decimal]
[-3 in decimal]
0101 [5 in decimal]
G
2. Perform 1100 - 111
AN
Ensure subtrahend has same bits with minuend. Subtrahend becomes 0111
1’s compliment of subtrahend 1000
2’s compliment of subtrahend 1000 + 1 = 1001
.
Exercise
Using 2’s compliment, perform 100011100 − 1011111 and 1111011 − 1100001
16 Dr. Raphael Angulu, BIT 113 SCI, MMUST, 2020: Data Representation Notes
3.3.1 Binary Coded Decimal (BCD)
Early mainframe computers in the 1950s were programmed using numeric codes, not text. As a result, the
first coding system used in these computers only needed to represent the ten digits in decimal number system.
The BCD(Binary Coded Decimal) system used 4 bits to represent numbers as shown in Table 3.3. Therefore,
BCD 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001
Decimal 0 1 2 3 4 5 6 7 8 9
R
to represent the number 253, the computer would store 0010 0101 0011.
U
In the early 1960s, computers were increasing in speed and storage capacity. It was now possible to program
these computers (and store data) in text format as well as number format. IBM developed an extension to
the BCD code which would be able to represent all English characters and punctuation marks. This code
was called EBCDIC (Extended Binary Coded Decimal Information Code).
UL
EBCDIC used 8 bits to represent characters, therefore it can represent 28 = 256 possible symbols.
Figure 3.15 shows a portion of the original EBCDIC table. The grey areas represent non-printable con-
trol keys (Esc, Del, Backspace, etc.), as well as special characters used in data transmission To find the
G
. AN
2. Write down the four ”Low Order” bits on the left side of the table directly across from the character
(for ”J”, they are 1101).
3. Write down the four ”High Order” bits at the top of the table, directly above the character (for ”J”,
they are 0001).
Therefore, the 8-bit EBCDIC code for an upper-case ”J” is 1101 0001.
Dr. Raphael Angulu, BIT 113 SCI, MMUST, 2020: Data Representation Notes 17
3.3.3 American Standard Code for Information Interchange (ASCII)
This is the most common format for text files in computers and on the Internet. In an ASCII file, each
alphabetic, numeric, or special character is represented with a 7-bit binary number (a string of seven 0s or
1s). 128 possible characters are defined.
UNIX and DOS-based operating systems use ASCII for text files. Windows NT and 2000 uses a newer
code, Unicode. IBM’s S/390 systems use a proprietary 8-bit code called EBCDIC. Conversion programs
allow different operating systems to change a file from one code to another.
R
U
UL
G
. AN
DR
18 Dr. Raphael Angulu, BIT 113 SCI, MMUST, 2020: Data Representation Notes