CH 01

Download as pdf or txt
Download as pdf or txt
You are on page 1of 44

IGCSE Computer Science 0984

Chapter 01
Data Representation

Steal This CS Notes


WHAT THEY WON’T TELL YOU ABOUT CS

Created by:
Mr. Mahmoud Moussa
01098569912
www.mahmoudmoussa.com

Outline
How and why computers use binary to represent all forms of data?

Convert between
Positive denary and positive binary

Positive denary and positive hexadecimal


Positive hexadecimal and positive binary

How and why hexadecimal is used as a beneficial method of data representation?


Add two positive 8-bit binary integers

Why overflow occurs in binary addition?


Perform a logical binary shift left and right on a positive 8-bit binary integer and
the effect this has on the positive binary integer
Use two’s complement to represent positive and negative 8-bit binary integers

3
Outline
How and why a computer represents text and the use of character sets, including
American standard code for information interchange (ASCII) and Unicode?
How and why a computer represents sound, including the effects of the sample
rate and sample resolution?
How and why a computer represents an image, including the effects of the
resolution and colour depth?
How data storage is measured?
Calculate the file size of an image file and a sound file, using information given
The purpose of and need for data compression
How files are compressed using lossy and lossless compression methods?

Binary System
Why we use binary to represent data in computers?

Binary System Consists of 1s and 0s only.

Computers contain millions of tiny ‘switches’ which


must be in the ‘ON’ or ‘OFF’ position.
A switch in the ON position can be represented by 1

A switch in the OFF position can be represented by 0

Binary Signifies voltage


Binary System
Converting from denary (base 10) to binary (base 2)
Example 1: Convert 2510 to binary
Repeated Division-by-2 Method
Solution: Quotient Remainder
25/2 = 12 1 LSB (least significant bit)
12/2 = 6 0
6/2 = 3 0
3/2 = 1 1
1/2 = 0 1 MSB (most significant bit)
Therefore, 2510 = 110012

Divide the decimal number by 2 repeatedly.


Keep track of remainder.

This process continues until the quotient becomes 0.


Remainders are written in reverse order to obtain the binary number.

Converting from Denary (Base 10) to Binary (Base 2)


Example 1: Convert 2510 to binary
25 - This method involves placing
Sum-of-Weights Method 1s in the appropriate position
24 23 22 21 20 so that the total equates to 25.
Solution:
16 8 4 2 1
- If weight > 25, place a 0,
otherwise a 1.
1 1 0 0 1
16 + 8 + 0 + 0 + 1= 25 Therefore, 2510 = 110012
----------------------------------------------------------------------------------------------------
Example 2: Convert 10510 to binary
Solution: Quotient Remainder
105/2 = 52 1 LSB (least significant bit)
52/2 = 26 0
26/2 = 13 0
13/2 = 6 1
6/2 = 3 0
3/2 = 1 1
1/2 = 0 1 MSB (most significant bit)
Therefore, 10510 = 11010012
Converting from Denary (Base 10) to Binary (Base 2)
Example 3: Convert 10710 to 8 bit binary value
Solution: Quotient Remainder
107/2 = 53 1 LSB (least significant bit)
53/2 = 26 1
26/2 = 13 0
13/2 = 6 1
6/2 = 3 0
3/2 = 1 1
1/2 = 0 1
0/2 = 0 0 MSB (most significant bit)
Therefore, 10710 = 011010112
----------------------------------------------------------------------------------------------------
Sum-of-Weights Method 107
27 26 25 24 23 22 21 20
Solution:
128 64 32 16 8 4 2 1

0 1 1 0 1 0 1 1
0 + 64 + 32 + 0 + 8 + 2 + 1 = 107

Converting from Binary (Base 2) to Denary (Base 10)


Example 1: Convert 11011012 to denary
Solution:

- Determine the weight of each bit that is a 1.


- Find the sum of the weights to get the decimal number.

Weight: 26 25 24 23 22 21 20

Binary number: 1 1 0 1 1 0 1

11011012 = 64+ 32 + 0+ 8+ 4+ 0+ 1 = 109


Converting from Denary (Base 10) to Binary (Base 2)

The process of converting the binary number 1101101 into a denary number

Write column headings (Powers of 2 ) (26, 25, 24,23,22,21,20)

Place a 1 or a 0 for each column in correct position

Identify the columns to be added


Add together the denary values identified this will give a
total which is the denary number
Answer is 109

By looking at the binary number, how can you determine if the


binary number is odd or even number in denary?

If the number to the far right in the binary number is 1


The denary number should be odd
Otherwise it should be even

Measurement of the Size of Computer Memories

A binary digit is commonly referred to as a BIT (0).


A bit is abbreviated with a lower-case‘b’

1 NIBBLE = 4 bits (0000)


1 BYTE = 8 bits (0000 0000)

1 BYTE represents a character (e.g letter, number or symbole)


A byte is abbreviated with the capital letter ‘B’

Name of Memory Size Number of bytes Equivalent denary value


1 kilobyte (KB) = 1000B 210 1 kibibyte (KiB) = 1024 bytes
1 megabyte (MB) = 1000KB 220 1 mebibyte (MiB) = 1024KiB
1 gigabyte (GB) = 1000MB 230 1 gibibyte (GiB) = 1024MiB
1 terabyte (TB) = 1000GB 240 1 tebibyte (TiB) = 1024GiB
1 petabyte (PB) = 1000TB 250 1 pebibyte (PiB) = 1024TiB
1 Exabyte (EB) = 1000 PB 260 1 exbibyte (EiB) = 1024 PiB 11
Measurement of the Size of Computer Memories

Example 1
Q: If the data transfer rate using the internet is 32 megabits per second, how long
it will take to download a 40 MB file?

40MB = 320megabits
320megabits / 32megabits per second
It will take 10 seconds to download a 40MB file.
Example 2
Q: Each image taken requires 1MB of storage. If the camera captures an image
every 30 seconds over a 10 hour period, how much storage is required? Give your
answer in gigabytes and show all your working.

Convert hours to seconds = 10 * 60 * 60 = 36000 seconds


Number of photos = 36000 S / 30 S = 1200 photos
Memory requirement = 1MB * 1200 = 1200MB
Convert to GB = 1200 / 1000 = 1.2GB

Binary VS Denary
The differences between the binary number system and the denary number system

A denary number system is a base-10 system


A binary number system uses 0 and 1 values

A denary number system uses 0 to 9 values

A binary number system has column headings that


increase by the power of 2

A denary number system has column headings that increase by


the power of 10

Binary has more digit for the same value


Applications of Binary System

When computers (or microprocessors) are used to control devices (such as robots)
Registers and sensors are used as part of the control system.
What is meant by the term register?

Small piece of fast memory (eg: 16bits, 32bits, 64bits)


Part of the processor
Temporary storage of data
Data is about to be or has been processed

What is meant by the term sensor?

It is an input device
It measures/takes physical readings of the surrounding environment properties
Such as Temperature sensor, sound sensor, and proximity sensor

Applications of Binary System


1 Mobile Trolley
Controlled devices usually contain registers which are made up of binary digits (bits). The
following example shows how these registers can be used to control a device.
The device on the left is a mobile trolley with 3 wheels. All three wheels
can turn left or right and each wheel has its own electric driving motor.
Sensors at the front and rear of the trolley detect an object in its path
which would subsequently stop all movement. An 8-bit register is used to
control the device. These represents the sensors
These represents the wheels

Front Backward Motor


Front Rear Error (object
Wheel Rear
Wheel Wheel Forward Direction (on/off) in the way
Turns right Wheel
Turns left Turns right Direction (on/off) =1, clear
(on/off) Turns left
(on/off) (on/off) (on/off) path = 0
(on/off)
(1 = ON and 0 = OFF)
Applications of Binary System
1 0 1 0 1 0 1 0

Front Backward Motor


Front Rear Error (object
Wheel Rear
Wheel Wheel Forward Direction (on/off) in the way
Turns right Wheel
Turns left Turns right Direction (on/off) =1, clear
(on/off) Turns left
(on/off) (on/off) (on/off) path = 0
(on/off)

What does this register mean? 10101010


- Front wheel turns left
- Back wheels turn left
- The direction is forward
- The motors are on
- Therefore the trolley is moving forward and turning left.

Applications of Binary System


0 0 0 1 0 1 1 1

Front Backward Motor


Front Rear Error (object
Wheel Rear
Wheel Wheel Forward Direction (on/off) in the way
Turns right Wheel
Turns left Turns right Direction (on/off) =1, clear
(on/off) Turns left
(on/off) (on/off) (on/off) path = 0
(on/off)

What does this register mean? 00010111


- Front wheel not turning left or right
- Rear wheels turning right
- Going in backward direction
- Motors on
- Error – object in path
- So the vehicle is going nowhere.

17
Applications of Binary System
0 1 1 0 1 0 1 0

Front Backward Motor


Front Rear Error (object
Wheel Rear
Wheel Wheel Forward Direction (on/off) in the way
Turns right Wheel
Turns left Turns right Direction (on/off) =1, clear
(on/off) Turns left
(on/off) (on/off) (on/off) path = 0
(on/off)

How would the following be represented using the above register?

- Front wheel turning right


- Back wheels turning left
- Moving in a forward direction
- Motors on
- No object in its path

18

Applications of Binary System


2 A digital alarm clock
A digital alarm clock is controlled by a microprocessor. It uses the 24-hour clock
system (i.e. 6 pm is 18:00). Each digit in a typical display is represented by a 4-
digit binary code. For example:

is represented by:
(clock display)
(a) What time is shown on the clock display if the 4-digit binary codes are:
(b) What would be stored in the 4-digit binary codes if the clock display time was:

0 0 0 1
0 1 1 1
0 0 1 0
(clock display)
1 0 0 1

(clock display)
Applications of Binary System
3 Elevators
A large hotel uses eight lifts (elevators) to carry passengers between its sixty floors. A
computer is used to control and monitor the lifts. Each lift has three registers to represent
its state. 1
Register 1 UP/DOWN 1 = lift going up and 0 = lift going down
4 2 1
Register 2 Lift ID number 0 to 7

32 16 8 4 2 1
Register 3 Floor number 0 to 60
Thus:
1 4 2 1 32 16 8 4 2 1
1 1 0 0 0 1 1 0 1 1

lift going up lift ID number 4 lift presently on 27th floor

Applications of Binary System


1
Register 1 UP/DOWN 1 = lift going up and 0 = lift going down
4 2 1
Register 2 Lift ID number 0 to 7

32 16 8 4 2 1
Register 3 Floor number 0 to 60
-----------------------------------------------------------------------------------------------------
(a) If the three registers contain the following data:
1 4 2 1 32 16 8 4 2 1
0 1 1 1 1 1 1 0 0 0

What information about the lift is shown?


- Lift 7
- Presently on 56th floor
- Going down
Applications of Binary System
1
Register 1 UP/DOWN 1 = lift going up and 0 = lift going down
4 2 1
Register 2 Lift ID number 0 to 7

32 16 8 4 2 1
Register 3 Floor number 0 to 60
-----------------------------------------------------------------------------------------------------
(b) How would the following information be shown in the three registers:
lift 6 presently on the 45th floor and going down

1 4 2 1 32 16 8 4 2 1
0 1 1 0 1 0 1 1 0 1

22

Applications of Binary System


(c) A customer is on the 14th floor and wants to go to the 50th floor. She presses
the button to call the lift. What two pieces of information would the computer
check to identify which of the eight lifts should be made available?
- is lift in service? - is a lift already on the 14th floor?
- which lift is nearest 14th floor? - which lifts are going up?
- which floor is the lift on? - which lifts are below 14th floor
(d) Using your answers to part (c), which of the following lifts would be made
available to the customer?
32 16 8 4 2 1
A

D B

D
Example Use of Binary
Uses for a binary number stored in a register
Data
ASCII value or Unicode value

Number

Part of image or small image


A sound sample or small sound track
Instruction
An address in main memory

24

The Hexadecimal System (Base 16)


Denary Binary Hex Based on 16 different digits [0-9] and [A-F]
0 0000 0
1 0001 1
A hexadecimal number system has column
2 0010 2
headings that increase by the power of 16
3 0011 3
4 0100 4 The typical headings for a hexadecimal
5 0101 5 number with five digits would be:
6 0110 6
65536 4096 256 16 1
7 0111 7
164 163 162 161 160
8 1000 8
9 1001 9 Each hexadecimal digit is equal to four
10 1010 A binary digits.
11 1011 B
Computers convert hexadecimal data into
12 1100 C
binary before processing it
13 1101 D
14 1110 E Table summarises the link between
15 1111 F denary, binary and hexadecimal.
Converting from Binary to Hexadecimal
Example 1: Represent binary 100111110101 in hex.
Solution:
- First the number is grouped into sets of 4 bits.
- Each group of 4 bits is replaced with its hex equivalent.
1001 1111 0101

9 F 5
-----------------------------------------------------------------------------------------------------
Example 2: Represent binary 10000111111101 in hex.
Solution:
- First the number is grouped into sets of 4 bits.
- The left group contains only 2 bits, so add in 2 0s.
- Each group of 4 bits is replaced with its hex equivalent.

0010 0001 1111 1101

2 1 F D

Converting from Hexadecimal to Binary


Example 1: Convert hex 29B to binary
Solution:
2 9 B

0010 1001 1011


Dropping the leading zeros gives 1010011011
-----------------------------------------------------------------------------------------------------
Example 2: Convert hex 45A to binary
Solution:
4 5 A

0100 0101 1010


Dropping the leading zero gives 10001011010
27
Converting from Hexadecimal to Denary
Example 1: Convert hex 45A to denary
Solution:
4 5 A
162 161 160
(4x256) + (5x16)+ (10x1) Note: A=10
Denary Number = 1114
-----------------------------------------------------------------------------------------------------
Example 2: Convert hex C8F to denary
Solution:
C 8 F
162 161 160
(12x256) + (8x16)+ (15x1) Note: C=12 F=15
Denary Number = 3215
28

Converting from Denary to Hexadecimal


Example 1: Convert 4510 to hex
Solution: Quotient Remainder
45/16 = 2 13 (hex D) LSB (least significant bit)
2/16 = 0 2 MSB (most significant bit)
Therefore, 4510 = 2D16
-----------------------------------------------------------------------------------------------------
Example 2: Convert 62910 to hex
Solution: Quotient Remainder
629/16 = 39 5 LSB (least significant bit)
39/16 = 2 7
2/16 = 0 2 MSB (most significant bit)

Therefore, 62910 = 27516

29
Use of Hexadecimal System

Memory Dumps
Use of Notation for Colour in HyperText
Hexadecimal Mark-up Language (HTML)
System
Media Access Control (MAC) Address

Web Addresses
Assembly Code and Machine Code

Display Error Codes

Use of Hexadecimal System


1 Memory Dumps

Much easier to work with: B 9 A 4 1 A F C rather than:


1011 | 1001 | 1010 | 0100 | 0001 | 1010 | 1111 | 1100
Therefore, Hexadecimal is often used in developing new software or when trying
to trace errors in programs.
When the memory contents are output to a printer or monitor,
this is known as a MEMORY DUMP
A program developer can look at each of the hexadecimal codes and determine
where the error lies

Memory location
Use of Hexadecimal System
2 Notation for Colour in HyperText Mark-up Language (HTML)

Hex colour notations are normally six digits


Each hex notation represents a different colour
The first two digits are the red component
The second two the green component
The last two the blue component.
All three together create the colour
The different intensity of the three primary colours (red, green, blue) is
determined by its hexadecimal values.
Example 01:
#FF 00 00 represent primary colour red
#00 FF 00 represent primary colout green
#00 00 FF represent primary colout blue
#FF 80 00 represent primary colout orange

Use of Hexadecimal System


Example 02:
The following code shows HTML ‘tag’ pairs on either side of the text stating the colour that
each creates.
<font color “#FF0000“> RED </font>
<font color “#00FF00“> GREEN </font>
<font color “#0000FF“> BLUE </font>
<font color “#X“> YELLOW </font>
<font color “#Y“> MAGENTA </font>
<font color “#Z“> CYAN </font>
Yellow is a combination of red and green, magenta a combination of red and blue and
cyan a combination of green and blue. State what 6-digit hexadecimal values should
replace X, Y and Z in the above code.
X FF FF 00 Y FF 00 FF Z 00 FF FF

Describe how other colours, such as a darker shade of blue, are created.

Hex values between 00 to FF are combined together to create a hex code


33
Different combinations in hex codes will create different colours
Use of Hexadecimal System
3 Media Access Control (MAC) Address

Hardware or physical address


MAC address refers to a number which uniquely identifies
each device on a network
Usually 48 or 64 bits (12 or 16 hex digits)
First 6 or 8 digits = manufacturer code of device (NIC)
Last 6 or 8 digits = serial number of device (NIC)
It is manufactured into every network card and rarely changed

Example:
00 – 1C – B3 – 4F – 25 - EE

Manufacturer Serial Number


Code Of the device

Use of Hexadecimal System


Types of MAC address

Universally Administered MAC Address (UAA)


Most common type of MAC address
Set by the manufacturer at the factory
It is rare for a user to want to change this MAC address
It will cause big problems if the changed address isn’t unique

Locally Administered MAC Address (LAA)


Reasons why the MAC address needs to be changed using LAA
Certain software used on mainframe systems needs all the MAC
addresses of devices to fall into a strict format
It may be necessary to bypass a MAC address filter on a router or a firewall
Only MAC addresses with a certain format are allowed through, otherwise the
devices will be blocked
Use of Hexadecimal System
4 Web Addresses

There are two systems commonly


used for character (text, numbers
and symbols) representation
ASCII
(AMERICAN STANDARD CODE
FOR INFORMATION
INTERCHANGE)

Unicode

ASCII TABLE

Use of Hexadecimal System


Differences between the ASCII and Unicode character sets

UNICODE has greater range of characters than ASCII


UNICODE gives 65,536 possible characters
ASCII gives 256 possible characters
UNICODE represents most written languages in the world
ASCII used for English only
ASCII uses 7bits per character (0-127)
Extended ASCII uses 8bits per character (0-255)

UNICODE uses up to 4 bytes per character


UNICODE uses 16 or 32 bits
Use of Hexadecimal System
How characters are represented using the ASCII character set

Each character is represented by a unique corresponding binary code

Example
When a key is pressed on the keyboard, the computer stores the ASCII representation of
the character typed into main memory. The ASCII representation for A is 65 (denary), for B
is 66 (denary), etc. There are two letters stored in the following memory locations:

Show the contents of Location 1 and Location 2 Location 1 A


as binary using 8 bits. Location 2 C

Location 1 0 1 0 0 0 0 0 1

Location 2 0 1 0 0 0 0 1 1

Use of Hexadecimal System


Representation of a web address:
Use of ASCII codes to represent a web address /Uniform Resource Locator (URL)
The % sign is used to denote that hexadecimal is being used
Sometimes the URL contains the characters %20 WHY??
Because <space> not allowed in a URL, %20 is the coding for a space (32 in denary)

www.hodder.co.uk
w w w .
%77 %77 %77 %2E
h o d d e r .
%68 %6F %64 %64 %65 %72 %2E
c o . u k
%63 %6F %2E %75 %6B
Use of Hexadecimal System
5 Assembly Code and Machine Code

Benefits of using hexadecimal numbers in computer science:


Can represent 16 bit words as only 4 hexadecimal digits/ Less digits to use
It is easy to convert hex digits back to binary if necessary
More can be displayed on a screen for addresses
Smaller amount of memory needed to store the hex error messages than text based
Easier to find errors
Easier for programmers to read and understand
Faster than binary for entering numbers

Less likely to make error

Use of Hexadecimal System


Example:
STO FFA4 (Assembly code assigns mnemonic codes)
A5E4 FFA4 (Machine code using hexadecimal values)
1010 0101 1110 0100 1111 1111 1010 0100 (Machine code using binary)
Machine code and assembly code are examples of low level languages

Why software developers use hexadecimal system rather than


binary at the development stage of software writing?
Help software developers to locate errors in the code
Easier for software developers to identify values in hexadecimal than binary
Drawbacks of using true machine code (which uses binary)
Very cumbersome and it takes a long time to key in the values
It is also very easy to mistype the digits in a ‘sea of 1s and 0s’
Programs are hard to maintain and debug
Use of Hexadecimal System
6 Display Error Codes

One of the most common errors you can come across while browsing the
Internet is a #404 or Page Not Found error.

This error often occurs when you follow a broken link or if you type in a website
address that doesn't exist.
A #404 page is a page that explains that a 404 error has occurred.

This error code #404 is written in hexadecimal

42

Addition of Binary Numbers


Facts when carrying out addition of two binary digits

Binary Addition Carry Sum


0+0= 0 0
0+1= 0 1
1+0= 0 1
1+1= 1 0
1+1+1= 1 1

43
Addition of Binary Numbers
Example 1 Add 00100111 + 01001010
111 Carry Values
00100111
+
01001010

01110001 Sum Values

44

Addition of Binary Numbers


Example 2 Add 0 1 1 0 1 1 1 0 and 1 1 0 1 1 1 1 0 (using 8 bits)

1 1 11 1 1 1 Carry Values
01101110
+
11011110
9th bit
1 01001100 Sum Values
This addition has generated a 9th bit.
The 8 bits of the answer are 0 1 0 0 1 1 0 0 – this gives the denary value of 76
which is incorrect because the denary value of the addition is 110 + 222 = 332.
The maximum denary value of an 8-bit binary number is 255 (28 -1)
The generation of a 9th bit is a clear indication that the sum has exceeded this value.
This is known as an overflow error
45
Addition of Binary Numbers
Why overflow error occur in binary addition?

Will occur if the value is greater than 255 in an 8-bit register


A computer or a device has a predefined limit that it
can represent or store, for example 16-bit
Occurs when a value outside this limit should be returned

Note
The larger the number of bits which can be used to represent a number
then the larger the number that can be stored.

Example
A 16-bit register would allow a maximum denary value of 65,535
(216 – 1) to be stored

46

Logical Binary Shifts


Logical Shift
Means moving the binary number to the left or to the right
Each shift left is equivalent to multiplying the binary number by 2
Each shift right is equivalent to dividing the binary number by 2
Logical Binary Shifts
Example 1

The denary number 21 is 00010101 in binary. If we put this into an 8-bit register:

128 64 32 16 8 4 2 1

0 0 0 1 0 1 0 1

If we now shift the bits in this register one place to the left, we obtain:

Note how the empty


128 64 32 16 8 4 2 1
right-most bit position
is now filled with a 0
0 0 1 0 1 0 1 0

The value of the binary bits is now 21 × 21 = 42.

Logical Binary Shifts


Example 2

The denary number 21 is 00010101 in binary. If we put this into an 8-bit register:

128 64 32 16 8 4 2 1

0 0 0 1 0 1 0 1

If we now shift the bits in this register two places to the left, we obtain:

Note how the empty


128 64 32 16 8 4 2 1
right-most bit position
is now filled with a 0
0 1 0 1 0 1 0 0

The value of the binary bits is now 21 × 22 = 84.


Logical Binary Shifts
Example 3
Let us consider what happens if we shift the binary number 00010101 four places
left:

128 64 32 16 8 4 2 1 Losing 1 bit following


a shift operation will
0 1 0 1 0 0 0 0 cause an error

The left-most 1-bit has been lost


In our 8-bit register the result of 21 × 24 is 80 which is clearly incorrect

This error is because we have exceeded the maximum number of left shifts
possible using this register

Logical Binary Shifts


Example 4
A computer uses an 8-bit register. The 8-bit register contains binary integers.
Write the denary (base 10) value represented by:

128 64 32 16 8 4 2 1

0 1 1 1 0 0 0 0

112
The original number is shifted three places to the right. Show the new binary number
00001110

Write the equivalent denary number.


14
State the effect the shift to the right had on the original denary number
51
Divided by 8
Logical Binary Shifts

128 64 32 16 8 4 2 1

0 1 1 1 0 0 0 0

Describe the problems that could be caused if the original binary number is shifted
five places to the
Run out of places to the right of register
Right-most 1 would be lost

Number would become 3 instead of 3.5


Loss of precision

Two’s Complement (Binary Numbers)


Two’s complement

Used to allow the possibility of representing negative integers


In two’s complement to a binary number that the left-most bit always determines
the sign of the binary number

A 1-value in the left-most bit indicates a negative number and a 0-value in the left
most bit indicates a positive number.

For example
00110011 Positive = 51 in denary

11001111 Negative = -49 in denary

53
Two’s Complement (Binary Numbers)
Example 1
Using two’s complement, show how 114 in base 10 could be stored in an 8-bit
register:

0 1 1 1 0 0 1 0

114/2 57 0
57/2 28 1
28/2 14 0
14/2 7 0
7/2 3 1
3/2 1 1
1/2 0 1

Two’s Complement (Binary Numbers)


Example 2

Using two’s complement, show how -93 in base 10 could be stored in an 8-bit
register:

93/2 46 1 Complement the bits


46/2 23 0
01011101
23/2 11 1
11/2 5 1 101000 10
5/2 2 1 Add one to the result
2/2 1 0 101000 10
1/2 0 1 0 0 0 0 0 0 0 1+
1010001 1

1 0 1 0 0 0 1 1
Two’s Complement (Binary Numbers)
Example 3

Using two’s complement, show how -77 could be stored in an 8-bit register

77/2 38 1 Complement the bits


38/2 19 0
01001101
19/2 9 1
9/2 4 1 101100 10
4/2 2 0 Add one to the result
2/2 1 0 101100 10
1/2 0 1 0 0 0 0 0 0 0 1+
1011001 1

1 0 1 1 0 0 1 1

Two’s Complement (Binary Numbers)


Example 4

Using two’s complement, show how to convert the binary value 1001 0011 to denary

-128 64 32 16 8 4 2 1

1 0 0 1 0 0 1 1

-128 + 16 + 2 + 1 = - 109

57
Representation of sound
Difference between analogue and digital data

Analogue data
Continuous data

Non-discrete data

e.g. data such as a sound wave

Digital data

Discrete data that has only two values

e.g. binary data (1’s and 0’s)

Representation of Sound
Soundwaves are vibrations in the air
The human ear senses these vibrations and interprets them as sound

High Frequency Wave

The amplitude specifies


the loudness of the sound
Low Frequency Wave

59
Representation of Sound
Sound waves vary continuously
This means that sound is analogue.
Computers cannot work with analogue data.

So sound waves need to be sampled in order to be stored in a computer

This is done using an analogue to digital converter (ADC)

Sampling

Measurement of value of analogue signal at regular time intervals

Representation of Sound
Sampling Rate
The number of samples taken per unit time

Effect of Sampling Rate on Sound Quality


Increasing the sampling rate will increase the accuracy of the digitised sound

Sampling Resolution

Number of distinct values available to encode each sample


Number of bits used to store each sample
Sometimes referred to as bit depth

The higher the sampling resolution, the smaller the quantization error

A higher sampling resolution results in less distortion of the sound

61
Usually 8 bit, 16 bit, 24 bit or 32 bit
Representation of Sound
The benefits and drawbacks of using a larger sampling resolution when recording
sound

Benefits
Larger dynamic range

Better sound quality

Less sound distortion

Drawbacks
Produces larger file size

Takes longer to transmit

Requires greater processing power


62

Representation of Sound
How sampling is used to record the sound clips?

The height of the sound wave is determined

At set time intervals

To get an approximation of the sound wave

And encoded as a sequence of binary numbers

Increasing the sampling rate will improve the accuracy of the recording

63
Representation of Bitmap Images
Bitmap Image
Made up of pixels
Made up of a two-dimensional matrix of pixels
Each pixel has one colour
Colour of each pixel stored as a binary number

Colour depth

The number of bits per pixel


With 1 bit we can have just 2 colours, black or white, for example

With 2 bits we can have 4 colours

With 3 bits we can have 8 colours

With 8 bits will give us 256 colours


So the more the bits per pixel, the greater range of colours in the image, we achieve

Representation of Bitmap Images


Image Resolution
The number of pixels in an image
The number of pixels wide by the number of pixels high

File Header

Data about the bitmap image


Examples of data stored in a file header
Dimensions of the image in pixels (Image Resolution)
Colour Depth (Number of bits in each pixel)
Type of compression used, if any

Note

The file size and quality of the image increases as the resolution and colour depth increase
65
Calculation of File Size
The file size of an image is calculated as:
Image Resolution (in pixels) × Colour Depth (in bits)

The size of a mono sound file is calculated as:


Sample rate (in Hz) × Sample Resolution (in bits) × Length of Sample (in seconds)

66

Calculation of File Size


Example 1

The image is scanned with an image resolution of 1024 × 512 pixels, and a colour
depth of 8 bits per pixel. Calculate an estimate for the file size, giving your answer in
mebibytes. Show your working.

1024 x 512 x 1B / 1024 = 512KiB

512 / 1024 = 0.5MiB

67
Calculation of File Size
Example 2

The icon in below is represented in a computer's memory as a bitmap image.


Four different colours have been used in the icon. Row 1 of the icon is represented in the
computer's memory as the bit pattern:

What are the bit patterns that have been used to


represent a grey pixel and a white pixel?

Grey pixel: 00 White pixel: 11

68

Calculation of File Size


State one possible 20-bit representation for Row 4 of the icon

1 1 1 1 0 0 1 0 0 1 1 0 0 1 1 0 0 0 1 1

Calculate the number of bytes required to represent


all the pixel data in the icon as a bitmap. Show your
working.

10 x 10 x 2 = 200bits

200 / 8 = 25Bytes
OR
Each row contains 20bit which is equal 2.5B

We have 10 rows so 10 x 2.5B = 25B

69
Calculation of File Size
Example 3
A camera detector has an array of 2048 by 2048 pixels and uses a colour depth of
16 bits per pixel. Find the size of an image taken by this camera in MiB.

2048 x 2048 x 2 / 1024

2 x 2048 x 2 / 1024

2 x 2x 2 = 8MiB

70

Calculation of File Size


Example 4
An audio CD has a sample rate of 44100 Hz and a sample resolution of 8bits. The
music being sampled uses two channels to allow for stereo recording. Calculate the file
size for a 10 seconds recording. Give your answer in MiB

Sample Rate (in Hz) × Sample Resolution (in bits) × Length of Sample (in seconds)

44100 × 8 × 10

44100 × 1 × 10

441000 B

441000 / 1024 / 1024 = .4MiB

Multiply by 2 since there are two channels being used .4 X 2 = .8MiB


71
File Formats
MPEG-3 (MP3)

MPEG-3 (MP3) uses technology known as AUDIO COMPRESSION to


convert music and other sounds into an MP3 file format.
This compression technology will reduce the size of a normal
music file by about 90 %

For example, 80MB file will become 8MB using MB3)

How can the original music file be reduced by 90%


whilst still retaining most of the music quality?

This is done using file compression algorithms


which use PERCEPTUAL MUSIC SHAPING
This essentially removes sounds that the
human ear can’t hear properly.

File Formats
MPEG-4 (MP4)

This format allows the storage of multimedia files rather than just sound

Music, videos, photos and animation can all be stored in the MP4 format

MP3 and MP4 files use what is known as a LOSSY FORMAT. WHY??

Since part of the original file is lost following the compression algorithm
File Formats
MP3 file Features

MP3
Digital recording of sound
Produced by recording software and a microphone
Used when distributing sound files
Compressed file format

File Formats
Joint Photographic Experts Group (jpeg) files

Resolution of the photographs is reduced from A to E


Photographs A and B are very sharp D is very fuzzy and E is almost unrecognisable
This is the result of reducing picture resolution.
JPEG is an example of lossy file compression

TIFF (Tagged Image File Format ) and BMP (bitmap) are the highest image
quality because, unlike jpeg, they are not in a compressed format

TIFF Lossy Compression Algorithm JPEG


File Formats
Example The following 2 pictures are images of the letter ‘R’ stored as bitmap files:
Why is picture X fuzzy?

Low resolution

Low bit map image

Insufficient pixel density (picture has less pixels)

Even the sharp image in picture Y would become fuzzy if enlarged. Why would this happen?
As picture is enlarged covers larger area

So pixel density gets smaller and sharpness of image is lost

Pixels become too big


State a drawback of storing sharp quality bitmap image files.
Uses up large amount of Download or upload takes longer time
memory storage space

File Formats
Text and number file formats
Examples of text file formats

.doc File extension for Word Document

.docx File extension for Word Document

.odt File extension for Word Document

.txt File extension for Text file

.rtf File extension for Rich Text Format

.csv File extension for Comma Separated Values

.pdf File extension for Portable Document Format

Note ASCII format is used to store text on a file


File Compression
What is meant by data compression?
Reducing the file size

Types of File Compression

Lossless file compression

Lossy file compression

File Compression
Lossless file compression
File size is reduced
No information is lost
The process can be reversed to re-create the original file
Used to compress text files
.bmp (bitmap) and .png (Portable Network Graphics) use lossless encoding
Lossy file compression
With this technique, the file compression algorithm eliminates unnecessary
bits of data as seen in MP3 and jpeg formats

It is impossible to get the original file back once it is compressed.

This is why it is chosen for files where removing certain bits doesn’t detract
from the quality
File Compression
Benefits of compressing a large file to send it over the internet
Smaller file to transmit

The file is transmitted quicker

Uses less bandwidth

Benefits of using lossy rather than lossless to compress sound file.

The file size will be smaller than lossless


Requires less storage space

Requires less time to transmit

File Compression
How a text file is compressed using lossless compression.

A compression algorithm is used

No data is lost in the process

Repeated words can be indexed

The indexed words can be replaced with numerical values

How lossless compression reduces the size of a sound file.

Uses a compression algorithm


Does not permanently remove any data

Repeated patterns of notes are identified and are grouped, with an index
File Compression
Example of using lossless compression in a text file
Consider the phrase ‘THIS SECTION SHOWS YOU HOW THIS WOULD
WORK’ which consists of 35 memory units (ignoring spaces)
Repeated words, such as ‘THIS’ could be put into Word Value
a data dictionary and be replaced by ‘1’. THIS 1
Repeated word sections, such as ‘HOW’ and ‘OU’ HOW 2
could be replaced by the numbers ‘2’ and ‘3’. OU 3

Our phrase then becomes ‘1 SECTION S2S Y3 2 1 W3LD WORK’.

Ignoring spaces, this compressed form now uses only 23 memory units.

This is about a 33% saving in file size.

File Compression
How to reduce the file size of a video as much as possible.
Best compression would be lossy
Use compression algorithm
This would remove all the unnecessary data from the file, removes the pixels
the human eye may not see and sound that the human ear may not hear

Reduce colour palette so each pixel requires fewer bits

Reduce resolution Reduce sample rate

Reduce sample resolution

Only store what changes between frames (temporal redundancy)


File Compression
Run Length Encoding (RLE) Algorithm
Lossless method of compression

Reduces the physical size of a string of adjacent, identical characters or pixels

The repeating string (a run) is encoded into two values

One value represents the number of identical characters in the run (the run count)

The other value is the code of the character or colour code of pixel in the run (the
run value)

The run value and run count combination may be preceded by a control character

Note
RLE is only effective where there is a long run of repeated bits.
84

File Compression
RLE on Text Data Example 1

Consider the following text string: aaaaabbbbccddddd


ASCII code is being used a = 97
Assuming each character requires 1byte then this string needs 16 bytes

Show how RLE will compress this string.

a a a a a b b b b c c d d d d d
5 97 4 98 2 99 5 100

Note

Assuming each number in the second row requires 1 byte of memory, the
RLE code will need 8 bytes. This is half the original file size

85
File Compression
RLE on Text Data Example 2

Consider the following text string:


aaaaaaaabbbbbbbbbbcdcdcdeeeeeeee
ASCII code is being used a = 97
Assuming each character requires 1byte then this string needs 32 bytes

Show how RLE will compress this string.

86

File Compression
String aaaaaaaa bbbbbbbbbb c d c d c d eeeeeeee
Code 8 97 10 98 1 99 1 100 1 99 1 100 1 99 1 100 8 101

The coded version contains 18 values and would require 18 bytes of storage
Introducing a flag (255 in this case) produces
255 08 97 || 255 10 98 || 99 100 99 100 99 100 || 255 08 101
This has 15 values and would, therefore, require 15 bytes of storage.
Note
This means that 255 is the flag and the other two numbers
255 08 97 indicate that there are eight items with ASCII code 97

1 99 This means one character with ASCII code 99 follows

Using the flag reduced the file size from 18 bytes to 15bytes

87
File Compression
RLE on Image Data Example 2
Colour Colour
The following diagrams show: Symbol (denary)
 The denary colour code that represents each colour B 153
 The first three rows of a bitmap image
W 255

Show how RLE will compress the first three rows of this image.
Row 1: 10 153 3 255 3 153 Row 3: 7 153 9 255
Row 2: 9 153 6 255 1 153 88

You might also like