Data Representation

advertisement
Computing Higher - Unit 1… Computer Systems
Higher Computing
Unit 1 – Topic 1
Data Representation
1
Computing Higher - Unit 1… Computer Systems
Data Representation – Binary Number System
The binary number system has a huge advantage in that only two
values need to be generated and detected, 0 and 1. These can
easily be represented in a computer system by a switch or
transistor being on or off, or by a high or low voltage level, eg 0
volts and 5 volts to represent 0 and 1.
Imagine how difficult it would be to represent 10 discrete logic
values, as required in the base 10 number system.
Any degradation of the signal would cause the data to change value
eg 4v could represent the value 4, if it degraded to 3.1v that could
change the 4 to a 3 – not very reliable!
2
Computing Higher - Unit 1… Computer Systems
Data Representation – Binary Number System
Binary representation also simplifies the number of arithmetic rules
that need to be applied in calculations. Binary arithmetic has fewer
rules. In binary there are only 4 rules, in decimal there are 100
rules.
3
Computing Higher - Unit 1… Computer Systems
Data Representation – Binary Number System
When numbers are represented electronically, the most convenient
base is 2, where each column, reading from the right is a power of
two (ie, 128 64 32 16 8 4 2 1). The base 2 number system
uses 2 symbols, 0 and 1 to represent a value.
In Summary:
• Binary only has to generate and detect two values
• Any degradation of the signal does not change the value (a
degraded 5v is still recognisable as a logic 1)
• Can be easily represented in a computer with a switch/transistor
being ON or OFF
• or by a HIGH or LOW voltage level
4
Computing Higher - Unit 1… Computer Systems
Data Representation – Converting Decimal to Binary
Write down the column headings, then place a one in each column
to add up to the decimal value. Example:
128 64
45
=
0
0
32
16
8
4
2
1
1
0
1
1
0
1
= 32 + 8 + 4 + 1 = 45
5
Computing Higher - Unit 1… Computer Systems
Data Representation – Converting Binary to Decimal
Write down the column headings. Write down the binary number
under the correct column. Add up the value of the column heading
when a 1 appears in the column. Example:
128 64
1
0
32
16
8
4
2
1
0
0
0
1
0
1
= 128 + 4 + 1
= 133
6
Computing Higher - Unit 1… Computer Systems
Data Representation – Representing Positive Numbers
The range of positive integers that can be represented using n bits
is 0 to 2n -1. Example:
8 bits gives the range 0-255. How?
The lowest positive value is 0.
To calculate the highest positive value use the formula:
=2 n-1,
=28-1
=(256-1)
= 255
So the range of positive values represented in 8 bits is 0-255
What range of positive values can be represented in 16 bits? 0-65535
Do Questions 1-5 from Topic 1 questions.
7
Computing Higher - Unit 1… Computer Systems
Data Representation – Representing +ve and -ve Nos
Sign and Magnitude
Sign and magnitude is one way of representing both
positive and negative integers.
The MSB (most significant bit) only tells us
whether the number is positive or negative.
The rest of the bits tell us the magnitude (size) of the
number.
If MSB is 0 number is positive.
If MSB is 1 number is negative.
Using 3 bits we can represent the following range of
numbers:
8
Computing Higher - Unit 1… Computer Systems
Data Representation – Representing +ve and -ve Nos
Sign and Magnitude - Problems
Carrying out addition and subtraction requires consideration of the
signs of the numbers and their relative magnitudes;
There are two representations of zero, testing for a zero result is
more complex than it needs to be.
(0000 = positive zero and 1000 = negative zero)
There is discontinuity in the number system, adding 1 to the
number 7 should give 8 but it gives the value -0. 7 + 1 should give
8 but it gives -0
0
1
1
1
1
1
0
0
0
For these reasons, sign and magnitude representation is not used for
the representation of negative integers in modern computers. 9
Computing Higher - Unit 1… Computer Systems
Data Representation – Representing +ve and -ve Nos
Sign and Magnitude - Problems
Range of numbers in Sign and Magnitude
8 bits: 2 n-1 -1
-(2 n-1 -1)
= 128 -1
=-128 – 1
01111111
11111111
=
=
127
-127
10
Computing Higher - Unit 1… Computer Systems
Data Representation – Representing +ve and -ve Nos
Two’s Complement
Two’s complement is a method of representing both negative and
positive numbers where arithmetic works, and where there is only 1
value for zero The major difference with two’s complement is that
the MSB gives the sign of the number but also contributes to its
size. Example: Change 55 to -55
Write down
column Headings
128 64
32
16
8
4
2
1
Write down
number (55)
0
0
1
1
0
1
1
1
Invert Numbers
1
1
0
0
1
0
1
1
0
0
1
0
0
+
0
0
1
1
Add 1
Number is calculated as
= -128 + 64 + 8 + 1 = -55
11
Computing Higher - Unit 1… Computer Systems
Data Representation – Representing +ve and -ve Nos
Two’s Complement
Range of numbers in Two’s Complement Notation:
The largest negative number using n bits is: When n is 8:
= –2 n-1
= -27
= -128
The largest positive number using n bits:
= +2 n-1 –1
= 27 -1
= 127
So the formulae is: –2 n-1 to +(2 n-1 –1)
So the range of numbers in two’s complement is -128 to 127
12
Computing Higher - Unit 1… Computer Systems
Data Representation – Floating Point Numbers
Floating Point – To represent real numbers
Floating-point representation represents decimal fractions (real
numbers) in scientific notation. This notation represents numbers as
a base number and an exponent. For example, 234.567 in decimal
could be represented as 2.34567 x 10 2, with base = 10 and exponent
= 2.
Mantissa * base exponent
To represent a decimal fraction in a computer, floating point
representation uses a non-zero fractional part, the mantissa, and an
integer part, the exponent, to represent a decimal number.
The mantissa can be represented in two’s complement while the
exponent uses sign and magnitude.
13
Computing Higher - Unit 1… Computer Systems
Data Representation – Floating Point Numbers
Floating Point
An example of floating point representation in decimal is shown
below;
The exponent represents the range of the number, while the
precision/accuracy is represented by the mantissa.
14
Computing Higher - Unit 1… Computer Systems
Data Representation – Floating Point Numbers
Floating Point
Given a fixed number of bits in a register, there are decisions that
need to be made as to how many bits will be allocated to the
mantissa and how many bits allocated to the exponent.
Applications that deal with very large numbers, for example
astronomy, are more interested in representing a big enough range of
numbers. Whereas, at the other end of the spectrum, scientists
dealing with molecular data (where distances can be measured in
10-9 of a metre) place greater importance on precision.
15
Computing Higher - Unit 1… Computer Systems
Data Representation – Floating Point Numbers
Floating Point
If we take the value 1011.0101, we float the point to the far left, note
the number of places it floated and this becomes the exponent (in
sign and magnitude notation). All the bits of the number are then
stored as the mantissa (in two’s complement notation). So, to
represent 1011.0101 as a mantissa exponent it becomes.
Mantissa
10110101
Exponent (Sign and Magnitude)
0100
The mantissa holds all the bits of the number, the exponent holds the
number of places the point floated to the left hand side (The power
the base is raised to!)
16
Computing Higher - Unit 1… Computer Systems
Memory used for different number representations
It is important to understand that in questions in your Higher you
need to look for the clue in the question regarding how numbers
are to be stored.
If a question states that positive integers are to be stored then you
can assume that binary (and not Sign and Magnitude or Twos
Complement) can be used as there is no need to store negatives.
So, for example, 8 bits can store from 0-255 or 256 numbers.
17
Computing Higher - Unit 1… Computer Systems
Memory used for different number representations
Storing Positive and Negative Integers in Twos Complement
Notation - it is likely that 3/4/5 bytes are used to store the value.
Floating Point Numbers - it is likely that 4 bytes is used for the
mantissa and 1 byte for the exponent, or a variation on these 5
bytes, allocating differing amounts to the mantissa/exponent which
will affect the accuracy and range of the number stored.
ASCII is a data representation to store text. Even when a number
is stored eg 8, it is stored as text and so cannot be used in a
calculation. When a number is stored as text it only uses up 7/8
bits (normal/extended ASCII) so in some situations it is favourable
to store a number as text.
18
Computing Higher - Unit 1… Computer Systems
Data Representation – Text
Text - ASCII
The set of characters that can be represented by the computer is
known as the character set. Many computers have the flexibility
of using several character sets, but we will restrict our discussions
to ASCII and Unicode.
ASCII
ASCII uses 7 bits per character, giving a possible 128 different
characters. It has 96 displayable characters, enough to represent a
letter or symbol of the English alphabet and numerical symbols.
There are 32 special character codes known as control characters.
These use ASCII values 0-31.
19
Computing Higher - Unit 1… Computer Systems
Data Representation – Text
Text - ASCII
Non-printing codes =
0-31
a-z = 97-122
A-Z=65-90
0-9=48-57
6 - ack
7 – bell
8 – backspace
9 – Horizontal Tab
13 – carriage return
24- cancel
27 - escape
20
Computing Higher - Unit 1… Computer Systems
Data Representation – Text
Extended ASCII
21
Computing Higher - Unit 1… Computer Systems
Data Representation – Text
Unicode
An increase in worldwide communications brought a need to
exchange information internationally. Languages such as Japanese
and Arabic, for example, have entirely different symbol shapes. A
Japanese symbol is shown below;
A solution to the problem of supporting multilingual text was to
encode characters using 16 bits, thus providing 65,536 possible
symbols. This is Unicode which is capable of including the characters
from all known languages and alphabets in the world.
22
Computing Higher - Unit 1… Computer Systems
Data Representation – Graphics
Bit-Mapped Graphics
A bit mapped image is stored as a file of pixel data and is produced on
screen at the same resolution at which it was created. Resolution
refers to the total number of pixels in the width and height of the
image. A bit map is a term that is applied to a 1-bit per pixel graphic
system, i.e. where images can have only two possible colour values, 0
and 1, usually black and white,
ie 1:1 mapping of pixel : bit in memory.
23
Computing Higher - Unit 1… Computer Systems
Data Representation – Graphics
Bit-Mapped Graphics
A simple graphic produced in a painting package, using 2 colours, and
its corresponding 9 x 9 x 1-bit representation is shown below.
24
Computing Higher - Unit 1… Computer Systems
Data Representation – Graphics
Bit-Mapped Graphics
As the number of bits per pixel increases, the number of colours that
can be represented increases. For instance, using 2 bits per pixel
allows 4 colours to be represented, while 8 bits per pixel can represent
256 possible colours. The number of bits used to represent each pixel
is called the bit-depth.
Number of Bits
Calculation Number of Colours
1
21
2
2
22
4
3
23
8
8
28
256
16
216
65536
24
224
16+ million colours
25
Computing Higher - Unit 1… Computer Systems
Data Representation – Graphics
Bit-Mapped Graphics
Advantages of bit mapped graphic representation
• a bit mapped image can be manipulated at the pixel level. Thus a
designer may apply particular colour values to a selected pixel area
to produce shading or textured effects;
• it is possible to create a wider range of irregular shapes and
patterns by simply deleting pixels anywhere on the image.
Disadvantages of bit mapped representation
• requires large amounts of storage space;
• image becomes course (jagged) when scaled;
• does not take advantage of resolutions that are higher than the
resolution of the image. Resolution dependence means graphic
takes it resolution at the time of creation.
26
Computing Higher - Unit 1… Computer Systems
Data Representation – Graphics
Bit-Mapped Graphics – Calculating Memory Requirements
How much memory would be required to store an image made up of
960 pixels by 800 pixels using 256 colours?
Formulae = pixels * pixels* colour
No. bits for 256 colours =
8 bits = 1 byte (28 = 256)
Calculation of storage for the image =
pixels * pixels* colour
=
960 x 800 x 1 byte
=
768000 bytes / 1024
=
750 Kb
27
Computing Higher - Unit 1… Computer Systems
Data Representation – Graphics
Vector Graphics
Vector graphic representation does not represent the image pixel by
pixel. Instead, it stores a description of the objects that make up the
image, ie the objects attributes. For example, if an image contains
a coloured circle and a pattern filled square then these object
descriptions could take the form shown on next slide.
28
Computing Higher - Unit 1… Computer Systems
Data Representation – Graphics
Vector Graphics
29
Computing Higher - Unit 1… Computer Systems
Data Representation – Graphics
Vector Graphics
Advantages of vector graphic representation
• they do not lose their image quality on scaling.
• requires less storage space than a bit mapped image;
• they can be edited at the "object" level, thus allowing the user to
reposition, scale and delete entire objects, or groups of objects,
with ease;
• objects can be grouped to form larger objects that can then be
manipulated as a single image;
• images are resolution independent.
Disadvantages of vector graphic representation
• See advantages of bit mapped graphics.
30
Download