Chapter 3 Data Representation
Chapter 3 Data Representation
l
ae
BIT 113 – Fundamental of Computing
Data Representation
ph
Lecturer Contact (name, telephone, e-mail)
Name: Dr. Raphael Angulu
Cell Number: 0729 260 641
Email: [email protected]
Introduction
Ra
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
u
schemes. These are the techniques (systems) used by computers to represent data in memory.
l
Objectives
gu
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
Learning Activities
The learning activities (Exercise questions) are presented after every sub-topic
Chapter 3
l
Data Representation
ae
ph
3.1 Number Systems
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
An
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
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.
Dr
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, 2022: 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
l
= 2 × 100 + 5 × 10 + 8 × 1
ae
= 200 + 50 + 8
ph
. . . thats why we read this number as ”two hundred and fifty eight. . . ” and write it as
= 25810
Am sure you still remember that any number n raised to power 0 is equal to 1. As in, n0 = 1. The powers
Ra
follow the same thing you learnt in class 2, do you remember ones, tens, hundreds, thousands etc? You can
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
u
0.72510 = 7 × 10−1 + 2 × 10−2 + 8 × 10−3
l
1 1 1 1
=7× +2× 2 +5× 3 remember a−b =
gu
101 10 10 ab
7 2 5
= + +
10 100 1000
An
= 0.72510
Putting these two numbers together, the number 258.72510 will look as shown in Figure 3.2.
2 5 8 · 7 2 5
.
2 1 0 −1 −2 −3
Dr
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, 2022: 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
l
Numbers are represented to the base of two (X2 ). This number system uses two digits to represent any
ae
number. These digits are 0 and 1. The computer commonly use this number system to represent numbers
(as well as other characters).
ph
0, 1, 10, 11, 100, 101, 110, 111, 1000, 1001, 1010, 1011, 1100, 1101, 1110, 1111, 10000 and so on
(read as zero, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16)
From the counting above, you can see to represent number 510 in binary is 1012 . How do we get the
next number after 1012 ? We just add 1 to 1012 and we get (six) which is 1102 . This is done as
101
+1
110
Ra
[5 in decimal]
[1 in decimal]
[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
u
with 9 in decimal number system.
l
Just like in the decimal number system, every digit has a power. The digit in the far right (just before
gu
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.
1 0 1
2 1 0
An
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, 2022: Data Representation Notes
2. Divide the quotient by 2 and keep the remainder
3. Repeat step 2 above until the remainder is 0 (Remainder will only be 0 if quotient is less than 2)
Examples
l
ae
8 ÷ 2 = 4 rem 0 → 0
4 ÷ 2 = 2 rem 0 → 0
2 ÷ 2 = 1 rem 0 → 0
ph
1 ÷ 2 = 0 rem 1 → 1
Reading these remainders from down upwards, we find that 810 = 10002
Ra
11 ÷ 2 = 5 rem 1 → 1
5 ÷ 2 = 2 rem 1 → 1
2 ÷ 2 = 1 rem 0 → 0
1 ÷ 2 = 0 rem 1 → 1
Reading these remainders from down upwards, we find that 1110 = 10112
u
3. Convert decimal number 2710 to binary
l
27 ÷ 2 = 13 rem 1 → 1
gu
13 ÷ 2 = 6 rem 1 → 1
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
1. Multiply the number by 2 and keep the whole number. 0.25 × 2 = 0.50 so you pick 0
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)
Example
Dr. Raphael Angulu, BIT 113 SCI, MMUST, 2022: Data Representation Notes 5
1. Convert the 0.2510 to binary
0.25 × 2 = 0.50 → 0
0.50 × 2 = 1.00 → 1
l
0.725 × 2 = 1.450 → 1
ae
0.450 × 2 = 0.900 → 0
0.900 × 2 = 1.800 → 1
0.800 × 2 = 1.600 → 1
ph
0.600 × 2 = 1.200 → 1
0.200 × 2 = 0.400 → 0
0.400 × 2 = 0.800 → 0
0.800 × 2 = 1.600 → 1
0.125 × 2 = 0.250 → 0
u
0.250 × 2 = 0.500 → 0
0.500 × 2 = 1.000 → 1
l
Exercise
Convert the following numbers from decimal to binary: 0.62510 , 0.712510 , 0.812510 , 82.42510
An
1. Starting from the rightmost digit, assign powers to each digit, with the rightmost digit having power
0
Examples
1 0 1 1
3 2 1 0
6 Dr. Raphael Angulu, BIT 113 SCI, MMUST, 2022: Data Representation Notes
= (1 × 23 ) + (0 × 22 ) + (1 × 21 ) + (1 × 20 )
= (1 × 8) + (0 × 4) + (1 × 2) + (1 × 1)
= 1110
l
ae
2. Convert 11100112 to decimal
1 1 1 0 0 1 1
6 5 4 3 2 1 0
ph
Figure 3.5: Powers of each digit in a binary number 11100112
= (1 × 26 ) + (1 × 25 ) + (1 × 24 ) + (0 × 23 ) + (0 × 22 ) + (1 × 21 ) + (1 × 20 )
Ra
= (1 × 64) + (1 × 32) + (1 × 16) + (0 × 8) + (0 × 4) + (1 × 2) + (1 × 1)
= 64 + 32 + 16 + 0 + 0 + 2 + 1
u
= 11510
l
Exercise
gu
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
0 · 0 0 1
−1 −2 −3
Dr. Raphael Angulu, BIT 113 SCI, MMUST, 2022: Data Representation Notes 7
= 0 × 2−1 + 0 × 2−2 + 1 × 2−3
1 1 1 1
=0× +0× 2 +1× 3 remember a−b =
21 2 2 ab
1 1 1
=0× +0× +1×
2 4 8
l
1
= = 0.12510
ae
8
0 · 1 0 1 1 1 0 0 1
ph
−1 −2 −3 −4 −5 −6 −7 −8
1
2
1
2
1
2
1
2
1
2
1
2
1
2
1
= 1× 1 + 0× 2 + 1× 3 + 1× 4 + 1× 5 + 0× 6 + 0× 7 + 1× 8
2
Ra
= (1 × 2−1 ) + (0 × 2−2 ) + (1 × 2−3 ) + (1 × 2−4 ) + (1 × 2−5 ) + (0 × 2−6 ) + (0 × 2−7 ) + (1 × 2−8 )
1 1 1 1 1 1 1 1
u
= 1× + 0× + 1× + 1× + 1× + 0× + 0× + 1×
2 4 8 16 32 64 128 256
l
1 1 1 1 1
= + 0 + + + + 0 + 0 +
gu
2 8 16 32 256
= 0.7226562510
An
Exercise
Convert the following fractions to decimal: 0.110012 , 0.1102 , 0.01012
0, 1, 2, 3, 4, 5, 6, 7, 10, 11, 12, 13, 14, 15, 16, 17, 20, 21, 22, 23, 24, 25, 26, 27, 30 and so on
Dr
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
8 Dr. Raphael Angulu, BIT 113 SCI, MMUST, 2022: Data Representation Notes
= (1 × 81 ) + (0 × 80 )
= (1 × 8) + (0 × 1)
= 810
l
1 2
ae
1 0
ph
= (1 × 81 ) + (2 × 80 )
= (1 × 8) + (2 × 1)
= 1010 Ra
u
To convert a number from decimal to octal
Example
10 ÷ 8 = 1 rem 2 → 2
1 ÷ 8 = 0 rem 1 → 1
.
19 ÷ 8 = 2 rem 3 → 3
2 ÷ 8 = 0 rem 2 → 2
Exercise
Convert the following numbers to octal: 6710 , 12310 and 24510
Dr. Raphael Angulu, BIT 113 SCI, MMUST, 2022: Data Representation Notes 9
Converting from octal to decimal
1. Starting with the rightmost digit, assign powers, with the rightmost digit taking power 0
l
ae
Examples
ph
2 4 6
2 1 0
Ra
= (2 × 82 ) + (4 × 81 ) + (6 × 80 )
= (2 × 64) + (4 × 8) + (6 × 1)
= 128 + 32 + 6
u
= 16610
l
gu
1 5 8
2 1 0
An
= (1 × 82 ) + (5 × 81 ) + (8 × 80 )
= (1 × 64) + (5 × 8) + (8 × 1)
.
Dr
= 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.
10 Dr. Raphael Angulu, BIT 113 SCI, MMUST, 2022: 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
l
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)
ae
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
ph
Example
1. Convert the number 111001012 to octal
= 011 100 101
001
Ra
4
011
6
100 111
= 1 1 3 4 7
u
= 113478
Exercise
l
1. Check the lookup table in Table 3.1, assign corresponding three bit set for each octal digit
2. Join the sets to form a binary number
Examples
1. Convert 3468 to binary
= 3 4 6
.
= 0111001102
Exercise
Convert the following numbers to binary: 34768 , 21348 and 6528
Dr. Raphael Angulu, BIT 113 SCI, MMUST, 2022: Data Representation Notes 11
3.1.4 Hexadecimal Number System
It uses 16 digits to represent numbers. These digits are 0 - 9 and A - F. In hexadecimal, A represents 10,
B represents 11 and so on until F represents 15
l
with respective alphabet between A and F)
ae
2. Divide the quotient by 16 and keep the remainder
3. Repeat step 2 above until the quotient is 0
4. Read the remainders from down upwards.
ph
Example
1. Convert the number 4310 to hexadecimal
43 ÷ 16 = 2 rem 11(B) → B
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
3. Add the multiplications above
Examples
1. Convert 2016 to decimal
.
Dr
2 0
1 0
= (2 × 161 ) + (0 × 160 )
= (2 × 16) + (0 × 1)
= 3210
12 Dr. Raphael Angulu, BIT 113 SCI, MMUST, 2022: Data Representation Notes
E(14) A(10)
1 0
= (E × 161 ) + (A × 160 )
l
= (14 × 161 ) + (10 × 160 )
ae
= (14 × 16) + (10 × 1)
= 224 + 10
= 23410
ph
Exercise
Convert the following numbers to decimal: A8B16 , 3CD16 and 4F B16
Ra
Converting 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
u
Binary 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111
l
Hexa 0 1 2 3 4 5 6 7 8 9 A B C D E F
gu
Table 3.2: Lookup Table for Converting from binary to hexadecimal and hexadecimal to binary
Examples
= B7B16
Dr
Exercise
Convert the following numbers to hexadecimal: 101112 , 111110111102 and 1011011111012
Dr. Raphael Angulu, BIT 113 SCI, MMUST, 2022: 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
l
1. Convert B7B16 to binary
ae
= B 7 B
= 1011 0111 1011
= 1011011110112
ph
2. Convert A4F16 to binary
= A 4 F
= 1010 0100 1111
Exercise
Ra
= 1010010011112
1. Add 101 + 11
0101 [5 in decimal]
+ 0011 [3 in decimal]
1000 [8 in decimal]
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
Dr
Exercise
Perform the following binary arithmetic: 10111001112 + 100012 and 100111102 + 101101111012
14 Dr. Raphael Angulu, BIT 113 SCI, MMUST, 2022: Data Representation Notes
3.2.2 Binary subtraction
There are two ways we can subtract binary numbers
Direct Subtraction
l
This follows the same approach as subtraction in decimal number system. Important point to remember is,
ae
When we borrow from a right digit to the left digit, we borrow 2 (the base).
ph
42
+ 15
27
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.
u
To perform the above subtraction in binary we do 101010 − 1111 which is
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, 2022: 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
l
ae
Examples
1. Perform 1000 - 11
1000 [8 in decimal]
ph
− 11 [3 in decimal]
Ensure subtrahend has same bits with minuend. Subtrahend becomes 0011
1000
Ra
1’s compliment of subtrahend
2’s compliment of subtrahend
1100
1100 + 1 = 1101
[8 in decimal]
+ 1101 [-3 in decimal]
u
0101 [5 in decimal]
l
2. Perform 1100 - 111
gu
Ensure subtrahend has same bits with minuend. Subtrahend becomes 0111
1’s compliment of subtrahend 1000
2’s compliment of subtrahend 1000 + 1 = 1001
0101 [5 in decimal]
Exercise
Using 2’s compliment, perform 100011100 − 1011111 and 1111011 − 1100001
16 Dr. Raphael Angulu, BIT 113 SCI, MMUST, 2022: 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
l
ae
Table 3.3: Binary Coded Decimal (BCD) Data Representation codes
to represent the number 253, the computer would store 0010 0101 0011.
ph
3.3.2 Extended Binary Coded Decimal Information Code (EBCDIC)
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
Ra
was called EBCDIC (Extended Binary Coded Decimal Information Code).
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
l u
gu
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, 2022: 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.
l
ae
ASCII was developed by the American National Standards Institute (ANSI)
ph
Ra
l u
gu
. An
Dr
18 Dr. Raphael Angulu, BIT 113 SCI, MMUST, 2022: Data Representation Notes