Ch1 - Data Representation
Ch1 - Data Representation
Omar El Safty
Introduction
Computer definition:
A computer is a device that receives input, does processing on that input and then produces
the output.
IMPORTANT
As humans we use analogue data. However, computers can’t process analogue data, they are
KEY TERMS
Analogue Data – Data that is constantly varying. For example, sound waves.
Digital Data – Discrete data that is made up from only two values 0 and 1.
Examples of data:
• Number
• Text
• Image
• Audio/Sound
• Video
1
Ch.1: Data Representation Eng. Omar El Safty
1. Numbers
1.1 Denary
Definition:
1.2 Binary
Definition:
IMPORTANT
2
Ch.1: Data Representation Eng. Omar El Safty
Solution:
This is done using the table method:
2 1s are placed in the appropriate columns so that if column headings are added, the
result is 25.
1 1 0 0 1
16 + 8 + 1 = 25
Another example:
Convert 125 to binary.
Solution:
Scan the following QR code for a more elaborate answer:
3
Ch.1: Data Representation Eng. Omar El Safty
Solution:
1 Powers of 2 (starting by 20) are placed as column headings
1 0 0 1 1 0
32 + 4 + 2 = 38
4
Ch.1: Data Representation Eng. Omar El Safty
1.3 Hexadecimal
Definition:
Hexadecimal Denary
A 10
B 11
C 12
D 13
E 14
F 15
5
Ch.1: Data Representation Eng. Omar El Safty
Solution:
Each hexadecimal digit is represented in 4 binary digits.
This means that the number 9A is represented in 8 binary digits.
9 A
9
Table method is then used to convert each hexadecimal digit to its equivalent 4 binary digits:
8 4 2 1 8 4 2 1
1 0 0 1 1 0 1 0
9 A
9
The answer is: 1001 1010
Another example:
Convert F0C to binary.
Solution:
Scan the following QR code for a more elaborate answer:
6
Ch.1: Data Representation Eng. Omar El Safty
Solution:
We recall that one hexadecimal digit is represented in 4 binary digits.
For this problem, we need to group every 4 digits and represent them as one hexadecimal
digit.
0001 0 0 1 1 0 1 1 0
After grouping each 4 binary digits, we know that we have 3 hexadecimal digits.
We use the table method to figure them out:
8 4 2 1 8 4 2 1 8 4 2 1
0 0 0 1 0 0 1 1 0 1 1 0
1 3 6
9
7
Ch.1: Data Representation Eng. Omar El Safty
Hexadecimal
Binary
Denary
Example:
Convert the hexadecimal number FB to denary.
Solution:
1 Convert FB to binary:
8 4 2 1 8 4 2 1
1 1 1 1 1 0 1 1
F B
FB = 1111 1011
1 1 1 1 1 0 1 1
128 + 64 + 32 + 16 + 8 + 2 + 1 = 251
8
Ch.1: Data Representation Eng. Omar El Safty
Denary
Binary
Hexadecimal
Example:
Convert the denary number 150 to hexadecimal.
Solution:
1 Convert 150 to binary:
1 0 0 1 0 1 1 0
128 + 16 + 4 + 2 = 150
8 4 2 1 8 4 2 1
1 0 0 1 0 1 1 0
9 6
9
3 The result is 96 in hexadecimal
9
Ch.1: Data Representation Eng. Omar El Safty
Uses of hexadecimal:
• HTML colour codes
• Error messages
• MAC addresses
• IPv6 addresses
• ASCII // Unicode
• Locations in memory
• Debugging
10
Ch.1: Data Representation Eng. Omar El Safty
Memory Units
Bit
Definition:
Refers to one binary digit.
Example:
For example the following binary number is composed of 5 bits:
11010
Nibble
Definition:
A group of 4 bits.
Byte
Definition:
A group of 8 bits.
11
Ch.1: Data Representation Eng. Omar El Safty
2. Text
Text is represented using one of the following character sets:
• ASCII (American Standard Code for Information Exchange)
• Unicode
• A list of all characters and symbols that can be represented by a computer system
• Each character/symbol is assigned a unique value.
2.1 ASCII
Definition:
The following is the ASCII table in which each character is represented with a unique value:
12
Ch.1: Data Representation Eng. Omar El Safty
Example:
Represent the word Computer in memory.
Solution:
After checking the ASCII table, the word Computer is represented as follows:
In denary:
67 111 109 112 117 116 101 114
In hexadecimal:
43 6F 6D 70 75 74 65 72
In binary:
01000011 01101111 01101101 01110000 01110101 01110100 01100101 01110010
2.2 Unicode
Definition:
• A character set which can represent all the languages of the world
• Each character is represented using up to 32 bits (4 bytes)
Main advantage of using Unicode character set compared to ASCII character set:
• Unicode allows for a greater range of characters and symbols than ASCII (including
different languages and emojis)
Main disadvantage of using Unicode character set compared to ASCII character set:
• Text stored using Unicode takes up more storage space as Unicode requires more
bits per character than ASCII
13
Ch.1: Data Representation Eng. Omar El Safty
3. Images
Definition:
An image is a series of pixels, each representing one colour, that are converted to binary and
processed by the computer.
Important note:
The file size and quality of the image depends on the image resolution and the colour depth.
KEY TERMS
IMPORTANT
• The higher the image resolution, the more the details that can be shown in the
image
• The higher the colour depth, the more colours that can be represented in the
image
14
Ch.1: Data Representation Eng. Omar El Safty
15
Ch.1: Data Representation Eng. Omar El Safty
Example:
An image is 2048 x 2048 pixels and uses a colour depth of 16 bits.
Find the size of an image taken by this camera in MiB.
Solution:
16
Colour depth in bytes = = 2 bytes
8
2048 ×2048×2
Size of image in MiB =
1024 ×1024
= 2 × 2 × 2 = 8 MiB
16
Ch.1: Data Representation Eng. Omar El Safty
4. Audio
• An audio file is converted to binary using sampling process to be processed by the
computer
• The quality of an audio file depends on the sampling rate and the sampling resolution
KEY TERMS
Sampling Process – The process of determining the amplitude of the sound wave at set time intervals.
Sampling Rate – The number of samples taken in a second.
Sampling Resolution – The number of bits representing each sample.
IMPORTANT
The accuracy of the recording and the file size increases as the sample rate
and sampling resolution increase.
17
Ch.1: Data Representation Eng. Omar El Safty
Sampling rate (in Hz) x Length of audio file (in seconds) x Sampling resolution (in bits)
Example:
An audio file has a length of 80 seconds and sampling rate of 32 samples/sec. Each sample is
represented in 32 bits. Calculate the size of the file in Kibibyte.
Solution:
Size of audio file in bits = 80 × 32 × 32
80 ×32×32 80 ×1024 80
Size of image in KiB = = =
1024 × 8 1024 × 8 8
= 10 KiB
18
Ch.1: Data Representation Eng. Omar El Safty
5. Binary Operations
5.1 Register
Definition:
A small fast memory location within the CPU that stores data and instructions during
processing.
Example:
In this example a 4-bit register is used to store the binary value 1000.
▪ Bit 1 is called the least significant bit as it has the least effect on the value of the
register
▪ Bit 4 is called the most significant bit as it has the highest effect on the value of the
register
KEY TERMS
19
Ch.1: Data Representation Eng. Omar El Safty
0+0 0 0 0+0+0 0 0
0+1 1 0 0+0+1 1 0
1+0 1 0 0+1+0 1 0
1+1 0 1 0+1+1 0 1
1+0+0 1 0
1+0+1 0 1
1+1+0 0 1
1+1+1 1 1
Example 1:
The addition of two 8-bit binary numbers can be done as follows:
Carry 0 0 0 0 1 1 1 0 0
Number 1 0 0 1 0 0 1 1 1
Number 2 0 1 0 0 1 0 1 0 +
Result 0 1 1 1 0 0 0 1
20
Ch.1: Data Representation Eng. Omar El Safty
Example 2:
The addition of two 8-bit binary numbers can be done as follows:
Carry 1 1 1 1 1 1 1 0 0
Number 1 0 1 1 0 1 1 1 0
Number 2 1 1 0 1 1 1 1 0 +
Result 1 0 1 0 0 1 1 0 0
Note:
The addition of 0110 1110 & 1101 1110 resulted in a number that is the value is greater
than 255. A number greater than 255 cannot be stored in an 8-bit register. This is called an
overflow error
26
Ch.1: Data Representation Eng. Omar El Safty
Example 1:
Represent the negative number -54 in an 8-bit register
1 To convert a negative number, we first convert it as if it was positive:
128 64 32 16 8 4 2 1
0 0 1 1 0 1 1 0
2 Then we flip all the values (each 1 is converted into 0, and each 0 is converted to 1)
128 64 32 16 8 4 2 1
1 1 0 0 1 0 0 1
3 Then add 1 (in the least significant bit) to the binary number
Carry 0 0 0 0 0 0 1 0
Flipped
1 1 0 0 1 0 0 1 +
number
Add 1 0 0 0 0 0 0 0 1
Result 1 1 0 0 1 0 1 0
IMPORTANT
The most significant bit in two’s complement represents the sign of the number.
Negative numbers have a most significant bit of 1, while positive numbers have a most
significant bit of 0.
27
Ch.1: Data Representation Eng. Omar El Safty
Example 2:
Convert the two’s complement 8-bit binary number 00111011 to a denary number.
1 Place the binary number in the two’s complement table
128 64 32 16 8 4 2 1
0 0 1 1 1 0 1 1
Note that this number had a most significant bit of 0, so the number is a positive number.
Example 3:
Convert the two’s complement 8-bit binary number 10101001 to a denary number.
Steps:
1 Check the most significant bit of the binary number.
This number has a most significant bit of 1, so the number is a negative number
converting negative numbers:
2 Take the two’s complement of the number, by flipping the number and adding 1
Carry 0 0 0 0 0 0 0 0
Flipped
0 1 0 1 0 1 1 0 +
number
Add 1 0 0 0 0 0 0 0 1
Result 0 1 0 1 0 1 1 1
28
Ch.1: Data Representation Eng. Omar El Safty
128 64 32 16 8 4 2 1
0 1 0 1 0 1 1 1
64 + 16 + 4 + 2 + 1 = 87
29
Ch.1: Data Representation Eng. Omar El Safty
In logical left shift the bits are moved some places to the left.
Example 1:
The register below is to be shifted one place to the left
This means that 1-bit logical left shift causes the denary number to be multiplied by 2
30
Ch.1: Data Representation Eng. Omar El Safty
Example 2:
The register below is to be shifted two places to the left.
This means that 2-bit logical left shift causes the denary number to be multiplied by 4 (22 )
IMPORTANT
31
Ch.1: Data Representation Eng. Omar El Safty
In logical right shift the bits are moved some places to the right.
Example 1:
The register below is to be shifted one place to the right.
This means that 1-bit logical right shift causes the denary number to be divided by 2
32
Ch.1: Data Representation Eng. Omar El Safty
Example 2:
The register below is to be shifted two places to the right.
This means that 2-bit logical right shift causes the denary number to be divided by 4 (22 )
IMPORTANT
33
Ch.1: Data Representation Eng. Omar El Safty
In the previous example Bit 8 (most significant bit) has been lost, this leads to loss of
accuracy and precision.
Example 2:
The register below is to be shifted one place to the right.
In the previous example Bit 1 (least significant bit) has been lost, this leads to loss of
accuracy and precision.
34