0% found this document useful (0 votes)
19 views

Chapter 05

This document discusses various methods of representing data in a computer system. It covers numeric systems like binary and decimal, and how integers and fractions are represented in binary. It also examines floating point number representation and how different data types like integers, strings and floating point numbers are stored in memory.

Uploaded by

NMHH
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Chapter 05

This document discusses various methods of representing data in a computer system. It covers numeric systems like binary and decimal, and how integers and fractions are represented in binary. It also examines floating point number representation and how different data types like integers, strings and floating point numbers are stored in memory.

Uploaded by

NMHH
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

1

Data representation
CHAPTER 05
2
Contents

 Positional Numbering System


 Binary numbers
 Static-point and floating-point numbers
 Data representation
 Data storage and program memory layout
3
Number System

Number system
Decimal Base 10. Digits used: 0 to 9
Binary Base 2. Digits used: 0,1
Octal Base 7. Digits used: 0 to 7
Hexa Base 16. Digits used: 0 to 9, Letters used A to F
4
Decimal system

4536 = (4x103) + (5x102) + (3x101) + 6x100


Base or radix = 10
4: MSD - Most significant digit
6: LSD - Least significant digit
23.76 = (2x10) + 3 + 7x10-1+ 6x10-2
5
Binary system

1101.11 = 1x23 + 1x22 + 0x21 + 1x20 + 2-1 + 2-2 = 13.75


Base or radix = 2
1: MSB - Most significant bit (Left most bit)
1: LSB - Least significant digit (Right most bit)
6
Converting Binary and Decimal
Operation Result Remainder
29/2 14 1
14/2 7 0
7/2 3 1
3/2 1 1
1/2 0 1

2910= 111012
7
Binary fractions

.1012= 1x2-1 +0x2-2 + 1x2-3 =


1x0.5 + 0x0.25 + 1x0.125 = 0.625
8
Convert decimal fraction to binary
Convert 13.625 to binary

1. Whole part: 13=11012

2. Fraction decimal part : 0.625

Multiply by 2 Whole part Fraction

 0.625 x 2 = 1.25 1 0.25

 0.25 x 2 = 0.5 0 0.5

 0.5 x 2 = 1.0 1 0 (stop)

 Fraction binary : 1012

 Combine both parts : 1101.101


9
Signed Binary
 Employs a sign bit is the signed-magnitude representation.

 In an n-bit word, the rightmost n - 1 bits hold the magnitude


of the integer. Decimal Binary
+3 011
+2 010
+1 001
0 000
-0 100
-1 101
-2 110
-3 111
10
Complement Arithmetic

 1’s Complement

 2’s Complement
Two’s complement
11
0000
1111 0001
-1 0
1 0010
1110 15
-2 2
14
1101 -3 0011
13 3

1100 -4 12 positive 4 0100


negative
11
-5 5
1011 0101
10
-6 9 6
1010 8 0110
-7 7
-8
1001 0111
1000

(let’s restrict to 4 bits)


Two’s complement
12
0000
1111 0001
0
-1
1 3+2=5
1110 15 0010
-2 2
14
1101 -3 0011
13 3

1100 -4 12 positive 4 0100


negative
11
-5 5
1011 0101
10
-6 9 6
1010 8 0110
-7 7
-8
1001 0111
1000
Two’s complement
13
0000
1111 0001 3+ (-5) = -2
-1 0
1 0010
1110 15
-2 2
14
1101 -3 0011
13 3

1100 -4 12 positive 4 0100


negative
11
-5 5
1011 0101
10
-6 9 6
1010 8 0110
-7 7
-8
1001 0111
1000
18
Range extension

 Take an n-bit integer and store it in m bits, where m > n

 The rule for 2s complement integers:

move the sign bit to the new leftmost position and fill in with copies
of the sign bit. For positive numbers, fill in with zeros, and for
negative numbers, fill in with ones.

+18 (8 bit) = 0010010 ➔ 00000000 0010010 (16 bit)

-18 (8 bit) = 11101110 ➔ 11111111 11101110 (16 bit)


25

Integer
representation
26
Integer types

http://tinyurl.com/y6ymre56
27
Two complement

http://tinyurl.com/y6ymre56
28
Signed & Unsigned Addition

http://tinyurl.com/y6ymre56
29
String representation
 “Hello”

http://tinyurl.com/y6ymre56
 “19653”

“19653” takes up 6 bytes in memory, whereas 19653 takes only 2


bytes
30

Floating point numbers

Common representation X=M x RE


 M : Mantissa
 R : Radix
 E : Exponent
Example (in decimal)
 13.2567 = 1.32567 x 101 = 1.32567 E+01
 0.0015908 = 1.5908 x 10-3 = 1.5908 E-03
31

Intel floating-point binary format

 The two most common floating-point binary storage formats


used by Intel processors (standardized by IEEE 754):

 Short real 32 bits (single precision) :

1 sign + 8 bits exponent + 23 bits mantissa

 Long real 64 bits (double precision) :

1 sign + 11 bits exponent + 52 bits mantissa


32

Floating-point binary fields

 Signed bit (1): 0 for positive, 1 for negative

 Biased exponent (characteristic): c = e + 2t-1 -1

e: exponent needed to store, t: bit size of this field

Ex: need to store the exponent 4 in 6-bit field. c = 4+25-1= 35 = 100011

t 2t-1 -1
3 23-1 -1 = 3
8 28-1 -1 = 127
11 211-1 -1 = 1023
33

Converting decimal to floating-point


1. Convert the absolute value of the number to binary,

2. Append × 20 to the end of the binary number (which does not


change its value), (e.g. 1101 → 11.01 x 20 )

3. Normalize the number. Move the binary point so that it is one bit from
the left. Adjust the exponent of two so that the value does not
change, (e.g. 11.01 x 20 → 1.01 x 21 )

4. Place the mantissa into the mantissa field of the number. Omit the
leading one, and fill with zeros on the right,

5. Add the bias to the exponent of two, and place it in the exponent
field,

6. Set the sign bit


34

Examples
Convert 2.625 to 8-bit binary floating-point

1-bit sign 3-bit exponent 4-bit mantissa

Convert to binary: 2.62510 = 10.1012

1. Add exponent part: 10.101 = 10.101 x 20

2. Normalize: 10.101 x 20 = 1.0101 x 21

3. Mantissa: 0101

4. Exponent: 1+3 = 4 = 1002

5. Sign bit is 0

The resulting number is 0100 01012 = 4516


35

Examples
Convert -4.75 to 8-bit binary floating-point

1-bit sign 3-bit exponent 4-bit mantissa

Convert to binary: 4.7510 = 100.112

1. Add exponent part: 100.11 = 100.11 x 20

2. Normalize: 100.11 x 20 = 1.0011 x 22

3. Mantissa: 0011

4. Exponent: 2+3 = 5 = 1012

5. Sign bit is 1

The resulting number is 1101 00112 = D316


36

Examples
Convert -1313.3125 to IEEE 32-bit binary floating-point

1-bit sign 8-bit exponent 23-bit mantissa

Convert to binary: 1313.312510 = 10100100001.01012

1. Add exponent part: 10100100001.01012 = 10100100001.01012 x 20

2. Normalize: 10100100001.01012 x 20 = 1.010010000101012 x 210

3. Mantissa: 01001000010101000000000

4. Exponent: 10+127 = 137 = 100010012

5. Sign bit is 1

The resulting number: 11000100101001000010101000000000 = C4A42A0016


37

Examples
Convert e7 binary floating-point (8 bits) to decimal

1-bit sign 8-bit exponent 22-bit mantissa

1. Convert to binary: e716 = 1 110 01112

2. Mantissa: 1.0111

3. Exponent: 1102 = 6; c = 6 – (2t-1 – 1) = 6 -3 = 3

4. De-normalize: 1.0111 x 23 = 1011.1

5. Convert: 10112 = 1110 ; 0.12 = 0.510

6. Sign bit is 1 → negative

Result: -11.5
38

Memory Word Storage


Little-endian/Big-endian architecture

 Little-endian (LSB is stored in the lowest memory address):

 e.g. Word value: 65530 = 0xFFF0 stored at 100h

Address 100 101


Value F0 FF

 e.g. Double-Word value: 5,000,000 = 0x004C4B40 stored at 100h

Address 100 101 103 104


Value 40 4B 4C 00
39

Program Memory Layout

High memory Stack

Heap
BSS – uninitialized data
Data – initialized data
Low memory Text (code)

You might also like