data representation summer 2014 comp 2130 intro computer systems computing science thompson rivers...

54
Data Representation Summer 2014 COMP 2130 Intro Computer Systems Computing Science Thompson Rivers University

Upload: kelley-gray

Post on 17-Dec-2015

213 views

Category:

Documents


1 download

TRANSCRIPT

Data Representation

Summer 2014

COMP 2130 Intro Computer Systems

Computing ScienceThompson Rivers University

TRU-COMP2130 Data Representation 2

Course Contents

Introduction to computer systems: B&O 1 Data representations: B&O 2.1 – 2.4 Introduction to C programming: K&R 1 – 4 C: advanced topics: K&R 5.1 – 5.10, 6 – 7 Introduction to IA32 (Intel Architecture 32): B&O 3.1 – 3.8, 3.13 Compiling, linking, loading, and executing: B&O 7 (except 7.12) Dynamic memory management – Heap: B&O 9.9.1 – 9.9.2, 9.9.4 –

9.9.5, 9.11 Code optimization: B&O 5.1 – 5.6, 5.13 Memory hierarchy, locality, caching: B&O 5.12, 6.1 – 6.3, 6.4.1 –

6.4.2, 6.5, 6.6.2 – 6.6.3, 6.7 Virtual memory (if time permits): B&O 9.4 – 9.5

TRU-COMP2130 Data Representation 3

Unit Learning Objectives

Convert a decimal number to binary, Octal, Hexadecimal. Convert a hexadecimal number to binary number, or Octal and vice

versa Compute binary addition. Compute binary subtraction using 2’s complement. Determine the 2’s complement representation of a signed integer. Understand the overflow of unsigned integers and signed integers. Understand the 2’s complement representation for negative integers. Subtract a binary number by using the 2’s complement addition. Multiply two binary numbers. Distinguish little endian byte order and big endian byte order Use of left shift and right shift. Binary division

TRU-COMP2130 Data Representation 4

1. Information Storage

memory, address Memory space is partitioned into more manageable units to store the

different program objects, i.e., instructions, program data, and control information.

The actual machine level program simply treats each program object as a block of bytes, and the program itself as a sequence of bytes.

Example int number = 28; 4 bytes, i.e., 32 bits, will be allocated to the variable number. The decimal number 28 will be stored in the 32 bits? How? Binary number, not the decimal, 00000000 00000000 00000000 00011100

will be stored in the 32 bits. Do programmers have to convert 28 to it’s binary number?

The Decimal System

TRU-COMP2130 Number Systems 5

Base (also called radix) = 10 10 digits { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }

Digit Position Integer & fraction

Digit Weight Weight = (Base) Position

Magnitude Sum of “Digit x Weight”

Formal Notation

1 0 -12 -2

5 1 2 7 4

10 1 0.1100 0.01

500 10 2 0.7 0.04

d2*B2+d1*B

1+d0*B0+d-1*B

-1+d-2*B-2

(512.74)10

The Binary System

In computer systems, the most basic memory unit is a bit that contains 0 and 1.

The data unit of 8 bits is referred as a byte that is the basic memory unit used in main memories and hard disks.

All data are represented by using binary numbers. Data types such as text, voice, image and video have no meaning in the data representation.

8 bits are usually used to express English alphabets. A collection of n bits has 2n possible states. Is it true? E.g.,

How many different numbers can you express using 2 bits? How many different numbers can you express using 4 bits? How many different numbers can you express using 8 bits? How many different numbers can you express using 32 bits?

TRU-COMP2130 Number Systems 6

Binary Number System

Base = 2 2 digits { 0, 1 }, called binary digits or “bits”

Weights Weight = (Base) Position

Magnitude Sum of “Bit x Weight”

Formal Notation Groups of bits 4 bits = Nibble

8 bits = Byte

Kilobyte (KB) = 1,024 Bytes Megabyte (MB) = 1,024 Kilobytes Gigabyte (GB) =1,024 Megabytes Terabyte (TB) = 1,024 Gigabytes Petabyte (PB) = 1,024 Terabytes Exabyte (EB) = 1,024 Petabytes

TRU-COMP2130 Number Systems 7

1 0 -12 -2

2 1 1/24 1/4

1 0 1 0 1

1 *22+0 *21+1 *20+0 *2-1+1 *2-2

=(5.25)10

(101.01)2

1 0 1 1

1 1 0 0 0 1 0 1

Octal Number System Base = 8

8 digits { 0, 1, 2, 3, 4, 5, 6, 7 } Weights

Weight = (Base) Position

Magnitude Sum of “Digit x Weight”

Formal Notation 1 0 -12 -2

8 1 1/864 1/64

5 1 2 7 4

5 *82+1 *81+2 *80+7 *8-1+4 *8-2

=(330.9375)10

(512.74)8

Hexadecimal Number System Base = 16

16 digits { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F } Weights

Weight = (Base) Position

Magnitude Sum of “Digit x Weight”

Formal Notation 1 0 -12 -2

16 1 1/16256 1/256

1 E 5 7 A

1 *162+14 *161+5 *160+7 *16-1+10 *16-2

=(485.4765625)10

(1E5.7A)16

The Power of 2

n 2n

0 20=1

1 21=2

2 22=4

3 23=8

4 24=16

5 25=32

6 26=64

7 27=128

n 2n

8 28=256

9 29=512

10 210=1024

11 211=2048

12 212=4096

20 220=1M

30 230=1G

40 240=1T

Mega

Giga

Tera

Kilo

Conversion Chart

Uses two digits 0 and 1. How to expand binary numbers?

02 = 0×20 = 010

12 = 1×20 = 110

102 = 1×21 + 0×20 = 210

112 = 1×21 + 1×20 = 310

1002 = 1×22 + 0×21 + 0×20 = 410

1012 = 1×22 + 0×21 + 1×20 = 510

1102 = 1×22 + 1×21 + 0×20 = 610

1112 = 1×22 + 1×21 + 1×20 = 710

10002 = 1×23 + 0×22 + 0×21 + 0×20 = 810

10012 = 1×23 + 0×22 + 0×21 + 1×20 = 910

...TRU-COMP2130 Number Systems 11

Powers of 2

12 = 1×20 = 110

102 = 1×21 + 0×20 = 210

1002 = 1×22 + 0×21 + 0×20 = 410

10002 = 1×23 + 0×22 + 0×21 + 0×20 = 810

1 00002 = 1610

10 00002 = 3210

100 00002 = 6410

TRU-COMP2130 Number Systems 12

Number Base Conversions

Decimal(Base 10)

Octal(Base 8)

Binary(Base 2)

Hexadecimal(Base 16)

Evaluate Magnitude

Evaluate Magnitude

Evaluate Magnitude

Decimal to Binary Conversion

Divide the number by the ‘Base’ (=2) Take the remainder (either 0 or 1) as a coefficient Take the quotient and repeat the division

Example: (13)10

Quotient Remainder Coefficient

Answer: (13)10 = (a3 a2 a1 a0)2 = (1101)2

MSB LSB

13/ 2 = 6 1 a0 = 1 6/ 2 = 3 0 a1 = 0 3/ 2 = 1 1 a2 = 1 1/ 2 = 0 1 a3 = 1

Decimal to Binary Conversion

Multiply the number by the ‘Base’ (=2) Take the integer (either 0 or 1) as a coefficient Take the resultant fraction and repeat the division

Example: (0.625)10

Integer Fraction Coefficient

Answer: (0.625)10 = (0.a-1 a-2 a-3)2 = (0.101)2

MSB LSB

0.625* 2 = 1 . 250.25* 2 = 0 . 5 a-2 = 00.5 * 2 = 1 . 0 a-3 = 1

a-1 = 1

Decimal to Octal ConversionExample: (175)10

Quotient Remainder Coefficient

Answer: (175)10 = (a2 a1 a0)8 = (257)8

175/ 8 = 21 7 a0 = 7 21/ 8 = 2 5 a1 = 5 2 / 8 = 0 2 a2 = 2

Example: (0.3125)10

Integer Fraction Coefficient

Answer: (0.3125)10 = (0.a-1 a-2 a-3)8 = (0.24)8

0.3125* 8 = 2 . 50.5 * 8 = 4 . 0 a-2 = 4

a-1 = 2

Binary − Octal Conversion

8 = 23

Each group of 3 bits represents an octal digit

Octal Binary

0 0 0 0

1 0 0 1

2 0 1 0

3 0 1 1

4 1 0 0

5 1 0 1

6 1 1 0

7 1 1 1

Example:

( 1 0 1 1 0 . 0 1 )2

( 2 6 . 2 )8

Assume Zeros

Works both ways (Binary to Octal & Octal to Binary)

Binary − Hexadecimal Conversion

16 = 24

Each group of 4 bits represents a hexadecimal digit

Hex Binary0 0 0 0 01 0 0 0 12 0 0 1 03 0 0 1 14 0 1 0 05 0 1 0 16 0 1 1 07 0 1 1 18 1 0 0 09 1 0 0 1A 1 0 1 0B 1 0 1 1C 1 1 0 0D 1 1 0 1E 1 1 1 0F 1 1 1 1

Example:

( 1 0 1 1 0 . 0 1 )2

( 1 6 . 4 )16

Assume Zeros

Works both ways (Binary to Hex & Hex to Binary)

Octal − Hexadecimal Conversion

Convert to Binary as an intermediate step

Example:

( 0 1 0 1 1 0 . 0 1 0 )2

( 1 6 . 4 )16

Assume Zeros

Works both ways (Octal to Hex & Hex to Octal)

( 2 6 . 2 )8

Assume Zeros

Decimal, Binary, Octal and Hexadecimal

Decimal Binary Octal Hex00 0000 00 001 0001 01 102 0010 02 203 0011 03 304 0100 04 405 0101 05 506 0110 06 607 0111 07 708 1000 10 809 1001 11 910 1010 12 A11 1011 13 B12 1100 14 C13 1101 15 D14 1110 16 E15 1111 17 F

Decimal Addition

Decimal Addition

5 5

55+

011

= Ten ≥ Base

Subtract a Base

11 Carry

Binary Addition

Column Addition

1 0 1111

1111 0+

0000 1 11

≥ (2)10

111111

= 61

= 23

= 84

Binary Subtraction

Borrow a “Base” when needed

0 0 1110

1111 0−

0101 1 10

= (10)2

2

22 2

1

000

1

= 77

= 23

= 54

Binary Multiplication

Bit by bit

01 1 1 1

01 1 0

00 0 0 0

01 1 1 1

01 1 1 1

0 0 000

0110111 0

x

Hexadecimal Number System

010 = 00002 = 016 = 0x0 110 = 00012 = 116 = 0x1 210 = 00102 = 216 = 0x2 310 = 00112 = 316 = 0x3 410 = 01002 = 416 = 0x4 510 = 01012 = 516 = 0x5 610 = 01102 = 616 = 0x6 710 = 01112 = 716 = 0x7 810 = 10002 = 816 = 0x8 910 = 10012 = 916 = 0x9 1010 = 10102 = A16 = 0xA 1110 = 10112 = B16 = 0xB 1210 = 11002 = C16 = 0xC 1310 = 11012 = D16 = 0xD 1410 = 11102 = E16 = 0xE 1510 = 11112 = F16 = 0xF

14816 = ???10 14816 = ???2

23c9d6ef = ???10 23c9d6ef = ???2

TRU-COMP2130 Number Systems 25

4 bits can be used for a hexadecimal number, 0, ..., F.

Please memorize it!

Binary to/from Hexadecimal conversion

4DA916 = ???2

1001101101010012 = ???16

= 100 1101 1010 1001

= 4DA9

10 11102 = 0x??? = ???10

0100 1110 1011 1001 01002 = 0x??? = ???10

TRU-COMP2130 Number Systems 26

TRU-COMP2130 Data Representation 27

Words

A word size – the nominal size of integer and pointer data. A pointer variable contains an address in the virtual address space. We will discuss pointer variable in the next unit.

The word size determines the maximum size of the virtual address space.

32bit operating systems? 64bit operating systems?

TRU-COMP2130 Data Representation 28

Addressing and Byte Ordering

A variable x of type int (allocates 4 bytes) If the address of x: 0x100 (means it starts storing from 0x100) This means the 4 bytes of x would be stored in memory locations 0x100,

0x101, 0x102, and 0x103. Let’s assume x has the value 0x1234567, which needs to be stored in four

bytes. There are two conventions to store the values in the 4 consecutive byte

memory locations. 0x01, 0x23, 0x45, and 0x67, or 0x67, 0x45, 0x23, and 0x01, depending on CPU architecture.

Little endian byte order – Intel-compatible machines0x103 0x102 0x101 0x100 address

0x01 0x23 0x45 0x67 value Big endian byte order – machines from IBM and Sun Microsystems

0x103 0x102 0x101 0x100 0x67 0x45 0x23 0x01

TRU-COMP2130 Data Representation 29

The byte orderings are totally invisible for most application programmers.

Why are the byte orderings important?1. Think of data exchange between two machines through a network.

2. Assembly programming

3. When type casting is used

TRU-COMP2130 Data Representation 30

2. Integer Representations

C Java Size

char, unsigned char byte 1B

short, unsigned short short 2Bs

char in Java uses 2Bs.

int, unsigned int int 4Bs

long, unsigned long long 8Bs

// there is no unsigned in Java

float float 4Bs

double double 8Bs

Unsigned Encodings

unsigned char All 8 bits are used. No sign bit. The smallest number is 0 The maximum number is 0xff.

unsigned short 16 bits The smallest number is ___ The maximum number is ____

unsigned int 32 bits The smallest number is ___ The maximum number is ____

unsigned long 64 bits The smallest number is ____ The maximum number is _____

TRU-COMP2130 Data Representations 31

Representation of Unsigned Integers

8-bit representation of unsigned char255 11111111254 11111110 ... ... 128 10000000127 01111111126 01111110 ... ... 2 00000010 1 00000001 0 00000000

The maximum number is ? The minimum number is ? What if we add the maximum number by 1 ??? What if we subtract the minimum number by 1 ???

TRU-COMP2130 Data Representations 32

+1

+1+1

overflow

Binary Addition

We will discuss binary addition and binary subtraction, before we discuss the representation of signed integers.

How to add two binary numbers? Let’s consider only unsigned integers (i.e., positive numbers only) for a while.

Just like the addition of two decimal numbers. E.g.,

10010 10010 1111 + 1001 + 1011 + 1

11011 11101 ???

10111 + 111

???

carry

TRU-COMP2130 Data Representations 33

Binary Subtraction

How to subtract a binary number? Just like the subtraction of decimal numbers. E.g.,

0112 02 02 1000 10 10 10010 10010 10010 -1 -1 -11 -11 -11 1 ?1 ?11 1111

Try: 101010 How to do? 1 -101 -10

TRU-COMP2130 Data Representations 34

In the previous slide, 10010 – 11 = 1111 What if we add

00010010+ 111111001 00001110+ 1 00001111

Is there any relationship between 112 and 111002?

The 1’s complement of 112 is ??? Switching 0 1 This type of addition is called 1’s complement addition. Find the 8-bit one’s complements of the followings.

11011 -> 00011011 -> 10 ->00000010 -> 101 ->00000101 ->

TRU-COMP2130 Data Representations 35

In the previous slide, 10010 – 11 = 1111 What if we add

00010010+ 111111011 00001111

Is there any relationship between 11 and 11101? The 2’s complement of 11 is ???

2’s complement ≡ 1’s complement + 1 -> 11100 + 1 = 11101 This type of addition is called 2’s complement addition. Find the 16-bit two’s complements of the followings.

11011 -> 0000000000011011 -> 10 101

TRU-COMP2130 Data Representations 36

Another example 101010- 101

??? What if we use 1’s complement addition or 2’s complement addition

instead as follow? Let’s use 8-bit representation. 00101010 00101010+ 11111010 + 111110111 00100100 1 00100101

+ 1 00100101

What does this mean? A – B = A + (–B), where A and B are positive Is the 1’s complement or the 2’s complement of B sort of equal to –B?

TRU-COMP2130 Data Representations 37

Can we use 8-bit 1’s complement addition for 12 – 102 = –12 ? 1 00000001- 10 + 11111101 <- 8-bit 1’s complement of

10 11111110 <- Is this correct?

(1’s complement of 1?) Let’s use 8-bit 2’s complement addition for 12 – 102.

00000001+ 11111110 <- 2’s complement of 10 11111111 <- Correct? (2’s complement of 1?)

12 – 102 = 12 + (–102) = –12

How to represent negative binary numbers, i.e., signed integers?

TRU-COMP2130 Data Representations 38

Representation of Negative Binaries

Representation of signed integers 8 or 16 or 32 bits are usually used for integers. Let’s use 8 bits for examples.

The left most bit (called most significant bit) is used as sign. When the MSB is 0, non-negative integers. When the MSB is 1, negative integers. The other 7 bits are used for integers.

How to represent positive integer 9? 00001001

How about -9? 10001001 is really okay? 00001001 (9) + 10001001 (-9) = 10010010 (-18) It is wrong! We need a different representation for negative integers.

TRU-COMP2130 Data Representations 39

How about -9? 10001001 is really okay? 00001001 (9) + 10001001 (-9) = 10010010 (-18) It is wrong! We need a different representation for negative integers.

What is the 8-bit 1’s complement of 9? 11110110 <- 8-bit 1’s complement of 9 00001001 + 11110110 <- 9 + 8-bit 1’s complement of 9

= 11111111 <- Is it zero? (1’s complement of 0?) What is the 2’s complement of 9?

11110111 <- 8-bit 2’s complement of 9 00001001 + 11110111 <- 9 + 8-bit 2’s complement of 9

= 1 00000000 <- It looks like zero.

2’s complement representation is used for negative integers.

TRU-COMP2130 Data Representations 40

12 – 102 = 12 + (–102) ??? What is the result in decimal?

00000001+ 11111110 <- 2’s complement of 10, i.e., -102

11111111 <- 2’s complement of 1, i.e., -1 (= 1 – 2) 1010102 – 1012 = 1010102 + (–1012) ???

100102 – 112 ???

102 – 12 ???

-102 – 12 ???

Is the two’s complement of the two’s complement of an integer the same integer?

What is x when the 8-bit 2’s complement of x is 11111111 11110011 10000001

TRU-COMP2130 Data Representations 41

8-bit representation of signed char with 2’s complement 127 01111111 126 01111110 ... ... 2 00000010 1 00000001 0 00000000 -1 11111111 -2 11111110 -3 11111101 ... ...-127 10000001-128 10000000

The maximum number is ? The minimum number is ? What if we add the maximum number by 1 ??? What if we subtract the minimum number by 1 ???

TRU-COMP2130 Data Representations 42

+1

+1+1

overflow

overflow

-1-1-1

16-bit representation signed short with 2’s complement... 01111111 11111111... 01111111 11111110... ... 3 00000000 00000011 2 00000000 00000010 1 00000000 00000001 0 00000000 00000000-1 11111111 11111111-2 11111111 11111110-3 11111111 11111101... ...... 10000000 00000001... 10000000 00000000

The maximum number is ? What if we add the maximum number by 1 ??? The minimum number is ? What if we subtract the minimum number by 1 ???

TRU-COMP2130 Data Representations 43

+1

overflow

overflow

-1

+1+1

-1-1

Note that computers use the 8-bit representation, the 16-bit representation, the 32-bit representation and the 64-bit representation with 2’complement for negative integers.

In programming languages char, unsigned char 8-bits short, unsigned short 16-bits int, unsigned int 32-bits long, unsigned long 64-bits

When we use the 32-bit representation with 2’s complement, The maximum number is ? What if we add the maximum number by 1 ??? The minimum number is ? What if we subtract the minimum number by 1 ???

TRU-COMP2130 Data Representations 44

TRU-COMP2130 Data Representation 45

Now we know how to represent negative integers. 2’ complement addtion A + (–B) is computed for subtraction A – B. Let’s suppose B is negative. Then –B is really a positive integer? For

example, let’s consider 1 byte signed integer. 127 01111111 126 01111110 ... ... 2 00000010 1 00000001 0 00000000 -1 11111111 -2 11111110 -3 11111101 2’s complement of -3 is 00000011, i.e., 3. ... ...-127 10000001-128 10000000 2’s complement of -128 is 10000000 again.

For any -127 < x < 127, x – x = 0. But (-128) – (-128) = ???

TRU-COMP2130 Data Representation 46

Binary Multiplication

Bit by bit

01 1 1 1

01 1 0

00 0 0 0

01 1 1 1

01 1 1 1

0 0 000

0110111 0

x

Binary Division

Binary division? 1111 <- quotient

101 1001101 -101 1001 -101 1000 -101 111 -101 10 <- remainder Try 1101011 / 110 Dividing negative binary numbers: division without sign, and then put

the sign.

TRU-COMP2130 Number Systems 47

This is why division is an expensive operation.

Binary division by a power of 2?

1001101 / 10 = 1001101001101 / 100 = 100111001101 / 1000 = 1001

What if we shift 1001101 right by 1 bit? What if we shift 1001101 right by 2 bits? What if we shift 1001101 right by 3 bits?

1001101 / 101 ??? Complicated implementation required

TRU-COMP2130 Number Systems 48

TRU-COMP2130 Data Representation 49

2.4 Floating Point

Fractional binary numbers IEEE floating-point representation

Fractions: Fixed-Point

How can we represent fractions? Use “binary point” to separate positive from negative powers of two --

like “decimal point.” 2’s comp addition and subtraction still work. (Assuming binary points are

aligned)

TRU-COMP2130 Number Systems 50

00101000.101 (40.625)

+ 11111110.110 (-1.25) (2’s complement)

00100111.011 (39.375)

2-1 = 0.5

2-2 = 0.25

2-3 = 0.125

No new operations -- same as integer arithmetic.

Just for your curiosity: How to convert decimal fractions to binary? 0.62510 = ???2

0.625 * 2 = 1.25 -> 1 (0.625 = x 2-1 + y 2-2 + z 2-3 + ...) 0.25 * 2 = 0.5 -> 0 (1.25 = x + y 2-1 + z 2-2 + ... => x = 1) 0.5 * 2 = 1.0 -> 1 (0.25 = y 2-1 + z 2-2 + ... ) Therefore 0.101

0.710 = ???2

0.7 * 2 = 1.4 -> 1 0.4 * 2 = 0.8 -> 0 0.8 * 2 = 1.6 -> 1 0.6 * 2 = 1.2 -> 1 0.2 * 2 = 0.4 -> 0 0.4 * 2 = 0.8 -> 0 0.8 * 2 = 1.6 -> 1 ... Therefore 0.1011001...

TRU-COMP2130 Number Systems 51

How to deal with big numbers and small numbers?

Introduction 52

Binary Logic

Truth table

TRU-COMP2130

x y z

0 0 0

0 1 0

1 0 0

1 1 1

x y z

0 0 0

0 1 1

1 0 1

1 1 1

x z

0 1

1 0

AND OR NOT

xy z x

y z

z = x • y = x y

z = x + y

x z

ASCII Character Codes

American Standard Code for Information Interchange (Refer to Table 1.7)

A popular code used to represent information sent as character-based data.

It uses 7-bits to represent: 94 Graphic printing characters. 34 Non-printing characters.

Some non-printing characters are used for text format (e.g. BS = Backspace, CR = carriage return).

Other non-printing characters are used for record marking and flow control (e.g. STX and ETX start and end text areas).

ASCII Properties

ASCII has some interesting properties: Digits 0 to 9 span Hexadecimal values 3016 to 3916

Upper case A-Z span 4116 to 5A16

Lower case a-z span 6116 to 7A16

Lower to upper case translation (and vice versa) occurs by flipping bit 6.