0% found this document useful (0 votes)
27 views67 pages

2 Data Representation

Uploaded by

nightcore AMV
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views67 pages

2 Data Representation

Uploaded by

nightcore AMV
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 67

DATA

REPRESENTATIO
N
CONTENTS
Integers are represented in computers
using:
🢝Unsigned, signed magnitude, excess, and two’s
complement notations
Fractional numbers are represented in
computers
🢝Floating point notation (IEEE 754 single format)
Characters are represented in
computers
Colours, images, sound and movies are
represented
2
BINARY NUMBERS
Binary number is simply a number comprised of only
0's and 1's.
Computers use binary numbers because it's easy for
them to communicate using electrical current -- 0 is
off, 1 is on.
You can express any base 10 (our number system --
where each digit is between 0 and 9) with binary
numbers.
The need to translate decimal number to binary and
binary to decimal.
There are many ways in representing decimal number
in binary numbers.
3
HOW DOES THE BINARY
SYSTEM WORK?
It is just like any other system
In decimal system the base is 10
🢝We have 10 possible values 0-9

In binary system the base is 2


🢝We have only two possible values 0 or 1.
🢝The same as in any base, 0 digit has non
contribution, where 1’s has contribution
which depends at there position.

4
BINARY TO DECIMAL

Technique
1. Multiply each bit by 2n, where n is the “weight” of
the bit
2. The weight is the position of the bit, starting from 0
on the right
3. Add the results

🢝For binary number with n digits:


🢝dn-1 ... d3 d2 d1 d0
🢝decimal = d0×20 + d1×21 + d2×22 + ... dn-12n-1

5
EXAMPLE
Bit “0”

1010112 => 1 x 20 = 1
1 x 21 =
2
0 x 22 =
0
1 x 23 =
8
0 x 24 =
0
1 x 25 =
DECIMAL TO BINARY

Technique
1. Divide the number by 2.
2. Get the integer quotient for the next
iteration.
3. Get the remainder for the binary digit.
4. Repeat the steps until the quotient is
equal to 0.

7
EXAMPLE
12510 = ?2 2 125
62 1
2
2 31 0
15 1
2
7 1
2
2 3 1

2 1 1
0 1

12510 = 11111012
FRACTIONS

Binary to decimal
10.1011 =>
1 x 2-4 = 0.0625
1 x 2-3 = 0.125
0 x 2-2 = 0.0
1 x 2-1 = 0.5
0 x 20 = 0.0
1 x 21 = 2.0
2.6875

9
FRACTIONS

Decimal to binary x
.14579
2
3.14579 0.29158
x 2
0.58316
x 2
1.16632
x 2
0.33264
x 2
0.66528
x 2
1.33056
11.001001... etc.
10
EXAMPLES: DECIMAL --
BINARY
Find the binary representation of
🢝12910

Find the decimal value


represented by the following
binary representations:
🢝1000 00112
🢝1010 10102

11
EXAMPLES: DECIMAL --
BINARY
Find the binary representation of
🢝12910 = 1000 00012

Find the decimal value


represented by the following
binary representations:
🢝1000 00112 = 13110
🢝1010 10102 = 17010

12
EXAMPLES: REPRESENTING
FRACTIONAL NUMBERS
Find the binary representations of:
🢝0.510 = 0.1
🢝0.7510 = 0.11

Using only 8 binary digits find the


binary representations of:
🢝0.210
🢝0.810

13
NUMBER
REPRESENTATION
Representing whole numbers
Representing fractional numbers

14
INTEGER
REPRESENTATIONS
Unsigned notation
Signed magnitude notation
Excess notation
Two’s complement notation.

15
UNSIGNED
REPRESENTATION
Represents positive integers.
Unsigned representation of 157:

7 6 5 4 3 2 1 0
position
Bit pattern 1 0 0 1 1 1 0 1
contribution 27 24 23 22 20

Addition is simple:
1 0 0 1 + 0 1 0 1 = 1 1 1 0.

16
REPRESENTATION OF
NEGATIVE NUMBERS
Is a representation of negative numbers
possible?
Unfortunately:
🢝you can not just stick a negative sign in front of a
binary number.

There are three methods used to


represent negative numbers.
1. Signed magnitude notation
2. Excess notation
3. Two’s complement notation
17
SIGNED MAGNITUDE
REPRESENTATION
Unsigned: - and + are the same.
In signed magnitude
🢝 the left-most bit represents the sign of the integer.
🢝0 for positive numbers.
🢝1 for negative numbers.

The remaining bits represent to


magnitude of the numbers.

18
EXAMPLE
Suppose 10011101 is a signed magnitude representation.
The sign bit is 1, then the number represented is negative

position 7 6 5 4 3 2 1 0
Bit pattern 1 0 0 1 1 1 0 1
contribution 24 23 22 20
-
The magnitude is 0011101 with a value 24+23+22+20= 29
Then the number represented by 10011101 is –29.

19
EXERCISE 1
1. 3710 has 0010 0101 in signed magnitude notation.
Find the signed magnitude of –3710 ?
2. Using the signed magnitude notation find the 8-
bit binary representation of the decimal value 2410
and -2410.
3. Find the signed magnitude of –63 using 8-bit
binary sequence?

20
DISADVANTAGE OF
SIGNED MAGNITUDE
Addition and subtractions are difficult.
Signs and magnitude, both have to carry
out the required operation.
They are two representations of 0
🢝00000000 = + 010
🢝10000000 = - 010
🢝To test if a number is 0 or not, the CPU will need to
see whether it is 00000000 or 10000000.
🢝0 is always performed in programs.
🢝Therefore, having two representations of 0 is inconvenient.

21
SIGNED MAGNITUDE-
SUMMARY
In signed magnitude notation,
🢝The most significant bit is used to represent the sign.
🢝1 represents negative numbers
🢝0 represents positive numbers.
🢝The unsigned value of the remaining bits represent The
magnitude.

Advantages:
🢝Represents positive and negative numbers

Disadvantages:
🢝two representations of zero,
🢝Arithmetic operations are difficult.
22
EXCESS NOTATION
In excess notation:
🢝The value represented is the unsigned value with a
fixed value subtracted from it.
🢝For n-bit binary sequences the value subtracted fixed
value is 2(n-1).
🢝Most significant bit:
🢝0 for negative numbers
🢝1 for positive numbers

23
EXCESS NOTATION WITH N
BITS
1000…0 represent 2n-1 is the decimal value
in unsigned notation.

Decimal Decimal
value - 2n-1 = value
(In unsigned (In excess
notation) Notation)

Therefore, in excess notation:


🢝 1000…0 will represent 0.

24
EXAMPLE (1) - EXCESS
TO DECIMAL
Find the decimal number
represented by 10011001 in
excess notation.
🢝Unsigned value
🢝1001 10002 = 27 + 24 + 23 + 20 = 128 + 16 +8 +1 = 15310
🢝Excess value:
🢝excess value = 153 – 27 = 152 – 128 = 25.

25
EXAMPLE (2) - DECIMAL
TO EXCESS
Represent the decimal value 24 in
8-bit excess notation.
We first add, 28-1, the fixed value
🢝 24 + 28-1 = 24 + 128= 152

then, find the unsigned value of


152
🢝15210 = 10011000 (unsigned notation).
🢝2410 = 10011000 (excess notation)
26
EXAMPLE (3)

Represent the decimal value -24 in 8-bit


excess notation.
We first add, 28-1, the fixed value
🢝 -24 + 28-1 = -24 + 128= 104

then, find the unsigned value of 104


🢝10410 = 01101000 (unsigned notation).
🢝-2410 = 01101000 (excess notation)

27
ADVANTAGES OF EXCESS
NOTATION
It can represent positive and negative
integers.
There is only one representation for 0.
It is easy to compare two numbers.
🢝When comparing the bits can be treated as unsigned
integers.

Excess notation is not normally used


to represent integers.
🢝It is mainly used in floating point representation for
representing fractions (later floating point rep.).
28
EXCESS NOTATION -
SUMMARY
In excess notation, the value represented is the unsigned
value with a fixed value subtracted from it.
🢝i.e. for n-bit binary sequences the value subtracted is 2(n-1).

Most significant bit:


🢝0 for negative numbers .
🢝1 positive numbers.

Advantages:
🢝Only one representation of zero.
🢝Easy for comparison.

Disadvantages :
🢝Always need convert from presentation number to value for
both positive and negative numbers
🢝0000…0 is not zero value
29
TWO’S COMPLEMENT
NOTATION
The most used representation for
integers.
🢝All positive numbers begin with 0.
🢝All negative numbers begin with 1.
🢝One representation of zero
🢝i.e. 0 is represented as 0000 using 4-bit binary sequence.

30
TWO’S COMPLEMENT
NOTATION WITH 4-BITS
Binary pattern Value in 2’s complement.
0 1 1 1 7
0 1 1 0 6
0 1 0 1 5
0 1 0 0 4
0 0 1 1 3
0 0 1 0 2
0 0 0 1 1
0 0 0 0 0
1 1 1 1 -1
1 1 1 0 -2
1 1 0 1 -3
1 1 0 0 -4
1 0 1 1 -5
1 0 1 0 -6
1 0 0 1 -7
1 0 0 0 -8

31
PROPERTIES OF TWO’S
COMPLEMENT NOTATION

Positive numbers begin with 0


Negative numbers begin with 1
Only one representation of 0, i.e.
0000
Relationship between +n and –n.
🢝0 1 0 0 +4 00010010 +18
🢝1 1 0 0 -4 11101110 -18

32
ADVANTAGES OF TWO’S
COMPLEMENT NOTATION

It is easy to add two numbers.


0 0 0 1 +1 1 0 0 0 -8
+ +
0 1 0 1 +5 0 1 0 1 +5

0110 +6 1 1 0 1 -3

• Subtraction can be easily performed.


• Multiplication is just a repeated addition.
• Division is just a repeated subtraction
• Two’s complement is widely used in ALU

33
EVALUATING NUMBERS IN
TWO’S COMPLEMENT
NOTATION
1. Sign bit = 0, the number is positive. The value
is determined in the usual way.
2. Sign bit = 1, the number is negative. Three
methods can be used:
🢝Method 1: decimal value of (n-1) bits, then subtract 2n-1
🢝Method 2: -2n-1 is the contribution of the sign bit.
🢝Method 3: Invert bits and add one: e.g. –6
🢝000…0110 => 111…1001 + 1 => 111…1010

34
EXAMPLE: 10101 IN
TWO’S COMPLEMENT
The most significant bit is 1, hence it is a
negative number.
Method 1
🢝0101 = +5 (+5 – 25-1 = 5 – 24 = 5-16 = -11)

Method 2
4 3 2 1 0
1 0 1 0 1
-24 22 20 =
-11

Method 3
🢝Invert(10101) + 1 => 01010 + 1 => 01011 = 8 + 2+1 = 11
🢝the result is then –11.

35
SIGNED INTEGERS

32-bit type
🢝2’s complement
f(b31…b0) = -b31 x 231 + … b1 x 21 + b0 x 20
🢝Max f(0111…11) = 231 – 1 = 2147483647
🢝Min f(100…00) = -231 = -2147483648
(asymmetric)
🢝Range[-231,231-1] => # values(231-1 to -231)
= 232
🢝Invert bits and add one: e.g. –6
🢝000…0110 => 111…1001 + 1 => 111…1010

36
TWO’S COMPLEMENT-
SUMMARY
Advantages:
🢝In two’s complement the most significant for an n-bit
number has a contribution of –2(n-1).
🢝One representation of zero (0…0)
🢝All arithmetic operations can be performed by using
addition and inversion.
🢝The most significant bit: 0 for positive and 1 for
negative.

Disadvantages:
🢝Need convert from presentation negative number to
value
37
FRACTION
REPRESENTATION
To represent fraction we need
other representations:
1. Fixed point representation
2. Floating point representation.

38
FIXED-POINT
REPRESENTATION
old position 7 6 5 4 3 2 1 0
New position 4 3 2 1 0 -1 -2 -3
Bit pattern 1 0 0 1 1. 1 0 1
Contribution 24 21 20 2-1 2-3 =19.625

Radix-point

39
LIMITATION OF FIXED-
POINT REPRESENTATION
To represent large numbers or
very small numbers we need a
very long sequences of bits.
This is because we have to give
bits to both the integer part and
the fraction part.

40
FLOATING POINT
REPRESENTATION
In decimal notation we can get around this
problem using scientific notation or
floating point notation.

Number Scientific notation Floating-point notation

1,245,000,000,000 1.245 1012 0.1245 1013

0.0000001245 1.245 10-7 0.1245 10-6

-0.0000001245 -1.245 10-7 -0.1245 10-6

41
FLOATING POINT

-15900000000000000
could be represented as
Mant
Base
issa
Expo
Sign - 159 * 1014 nent
- 15.9 * 1015
- 1.59 * 1016
A calculator might display 159 E14
42
FLOATING POINT
FORMAT
± M × B ±E

Sign mantissa or base


exponent
significand

Sign Exponent Mantissa

43
FLOATING POINT
REPRESENTATION FORMAT

Sign Exponent Mantissa

The exponent is biased by a fixed


value b, called the bias.
The mantissa should be normalised,
🢝e.g. if the real mantissa if of the form 1.f then
the normalised mantissa should be f, where f
is a binary sequence.

44
IEEE 745 SINGLE
PRECISION
The number will occupy 32 bits

The first bit represents the sign of the number;


1= negative 0= positive.
The next 8 bits will specify the exponent stored
in biased 127 form.

The remaining 23 bits will carry the mantissa


normalised to be between 1 and 2.
i.e. 1<= mantissa < 2
45
REPRESENTATION IN IEEE 754
SINGLE PRECISION

sign bit:
🢝0 for positive and,
🢝1 for negative numbers

8 biased exponent by 127 (Excess


notation)
23 bit normalised mantissa
Sign Exponent Mantissa

46
BASIC CONVERSION
Converting a decimal number to a floating
point number.
🢝1.Take the integer part of the number and
generate the binary equivalent.
🢝2.Take the fractional part and generate a binary
fraction
🢝3.Then place the two parts together and
normalise.

47
IEEE – EXAMPLE 1
Convert 6.75 to 32 bit IEEE format.
1. The Mantissa. The Integer first.
6/2 =3r0
3/2 =1r1
1/2 =0r1 =
2. Fraction next. 1102
.75 * 2 = 1.5
.5 * 2 = 1.0 =
0.11 110.11
3. put the two parts together…
2
Now normalise 1.1011 * 22

48
IEEE – EXAMPLE 1

Normalise: 1.1011 * 2 2
🢝Signed: 0 for positive
🢝Mantissa: 1011
🢝Exponent: 2 + 127 = 129 = 1000 0001
sign(1) exponent(8)
3
mantissa(23)
3 2 2 1 0
1 0 3 2

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

Sig
Exponent Mantissa
n
49
POSSIBLE REPRESENTATIONS
OF AN EXPONENT
Binary Sign 2’s Biased 127 Exponent.
Magnitude Complement
0000 0000 0 0 -127 {reserved}
0000 0001 1 1 -126
0000 0010 2 2 -125

0111 1110 126 126 -1


0111 1111 127 127 0
1000 0000 -0 -128 1

1111 1110 -126 -2 127


1111 1111 -127 -1 128 {reserved}

50
WHY BIASED ?
The smallest exponent 00000000
Only one exponent zero 01111111
The highest exponent is 11111111
To increase the exponent by one simply add
1 to the present pattern.
The zero value: 1.0*2-127 ~ 0
3 3 2 2 1 0
1 0 3 2

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

51
EXAMPLE (2)

which number does the following IEEE


single precision notation represent?
1 1000 0000 0100 0000 0000 0000 0000 000
The sign bit is 1, hence it is a negative number.
The exponent is 1000 0000 = 12810
It is biased by 127, hence the real exponent is
128 –127 = 1.
The mantissa: 0100 0000 0000 0000 0000 000.
It is normalised, hence the true mantissa is
1.01 = 1.2510
Finally, the number represented is: -1.25 x 21 = -2.50
52
RANGE OF NUMBERS

Normalized (positive range;


negative is symmetric)
smalles +2-126× (1+0) = 2-126
t
0 00000001 00000000000000000000000

larges +2127× (2-2-23)


t
0 11111110 11111111111111111111111

2127(2-2-23)
0 2
-126

Positive underflow Positive overflow

53
REPRESENTATION IN IEEE
754 DOUBLE PRECISION
FORMAT
It uses 64 bits
🢝1 bit sign
🢝11 bit biased exponent (Biased = 1023)
🢝52 bit mantissa

Sign Exponent Mantissa

54
IEEE 754 ENCODING
Represented
Single Precision Double Precision
Object
Exponent Fraction Exponent Fraction
0 0 0 0 0
+/- denormalized
0 non-zero 0 non-zero
number
+/- floating-point
1~254 anything 1~2046 anything
numbers
255 0 2047 0 +/- infinity
NaN (Not a
255 non-zero 2047 non-zero
Number)

55
FP ROUNDING

Rounding is important
🢝Small errors accumulate over billions of ops
FP rounding hardware helps
🢝Compute extra guard bit beyond 23/52 bits
🢝Further, compute additional round bit beyond
that
🢝Multiply may result in leading 0 bit, normalize shifts
guard bit into product, leaving round bit for rounding
🢝Finally, keep sticky bit that is set whenever ‘1’
bits are “lost” to the right
🢝Differentiates between 0.5 and 0.500000000001
56
FLOATING POINT
REPRESENTATION FORMAT
(SUMMARY)
Sign Exponent Mantissa

the sign bit represents the sign


🢝0 for positive numbers
🢝1 for negative numbers
The exponent is biased by a fixed value b, called
the bias.
The mantissa should be normalised, e.g. if the
real mantissa if of the form 1.f then the
normalised mantissa should be f, where f is a
binary sequence.
57
CHARACTER
REPRESENTATION- ASCII
ASCII (American Standard Code for Information
Interchange)
It is the scheme used to represent characters.
Each character is represented using 7-bit binary code.
If 8-bits are used, the first bit is always set to 0

58
ASCII – EXAMPLE
Symbol decimal Binary

7 55 00110111
8 56 00111000
9 57 00111001
: 58 00111010
; 59 00111011
< 60 00111100
= 61 00111101
> 62 00111110
? 63 00111111
@ 64 01000000
A 65 01000001
B 66 01000010
C 67 01000011

59
EXERCISE

Use the ASCII table to write the


ASCII code for the following:
🢝CIS110
🢝6=2*3

60
UNICODE -
REPRESENTATION
ASCII code can represent only 128 = 27 characters.
It only represents the English Alphabet plus some
control characters.
Unicode is designed to represent the worldwide
interchange.
It uses 16 bits and can represents 32,768
characters.
For compatibility, the first 128 Unicode are the same
as the one of the ASCII.

61
COLOUR
REPRESENTATION
Colours can represented using a sequence of bits.
256 colors – how many bits?
🢝Hint for calculating
🢝To figure out how many bits are needed to represent a
range of values, figure out the smallest power of 2 that is
equal to or bigger than the size of the range.
🢝That is, find x for 2 x => 256

24-bit colour – how many possible colors can be


represented?
🢝Hints
🢝16 million possible colors (why 16 millions?)

62
24-BITS -- THE TRUE
COLOUR
🢝24-bit color is often referred to as the true
colour.
🢝Any real-life shade, detected by the naked
eye, will be among the 16 million possible
colours.

63
EXAMPLE: 2-BIT PER
PIXEL
= (whit
4=2 choices
2
0
0
e)
🢝00 (off, off)=white
🢝01 (off, on)=light = (light
grey 0 grey)
1
🢝10 (on, off)=dark
grey = (dark
grey)
🢝11 (on, on)=black 1
0

= (blac
1 k)
1

64
IMAGE REPRESENTATION
An image can be divided into many tiny squares,
called pixels.
Each pixel has a particular colour.
The quality of the picture depends on two factors:
🢝the density of pixels.
🢝The length of the word representing colours.
The resolution of an image is the density of pixels.
The higher the resolution the more information
the image contains.

65
BITMAP IMAGES
Each individual pixel (pi(x)cture element)
in a graphic stored as a binary number
🢝Pixel: A small area with associated coordinate
location
🢝Example: each point below is represented by a 4-bit
code corresponding to 1 of 16 shades of gray

66
SUMMARY
Integer representation
🢝Unsigned,
🢝Signed notation,
🢝Excess notation, and
🢝Two’s complement.

Fraction representation
🢝Floating point (IEEE 754 format )
🢝Single and double precision

Character representation
Colour representation

67

You might also like