chapter 4 bits and bytes · 2012-01-31 · page 1 chapter 4 bits and bytes • why bits? •...

59
Page 1 Chapter 4 Bits and Bytes Why bits? Representing information as bits Binary/Hexadecimal Byte representations Representing Expressing in C Simple Gates and Circuits Why Don’t Computers Use Base 10? Base 10 Number Representation That’s why fingers are known as “digits” Natural representation for financial transactions Floating point number cannot exactly represent $1 20 Floating point number cannot exactly represent $1.20 Even carries through in scientific notation 1.5213 X 10 4 Implementing Electronically Hard to store ENIAC (First electronic computer) used 10 vacuum tubes / digit Hard to transmit Need high precision to encode 10 signal levels on single wire Messy to implement digital logic functions Addition, multiplication, etc.

Upload: others

Post on 11-Aug-2020

9 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Chapter 4 Bits and Bytes · 2012-01-31 · Page 1 Chapter 4 Bits and Bytes • Why bits? • Representing information as bits –Binary/Hexadecimal –Byte representations • Representing

Page 1

Chapter 4Bits and Bytes• Why bits?• Representing information as bits

–Binary/Hexadecimal–Byte representations

• Representing Expressing in C• Simple Gates and Circuits

Why Don’t Computers Use Base 10?

Base 10 Number Representation• That’s why fingers are known as “digits”

• Natural representation for financial transactions

Floating point number cannot exactly represent $1 20– Floating point number cannot exactly represent $1.20

• Even carries through in scientific notation

– 1.5213 X 104

Implementing Electronically• Hard to store

– ENIAC (First electronic computer) used 10 vacuum tubes / digitC ( st e ect o c co pute ) used 0 acuu tubes / d g t

• Hard to transmit

– Need high precision to encode 10 signal levels on single wire

• Messy to implement digital logic functions

– Addition, multiplication, etc.

Page 2: Chapter 4 Bits and Bytes · 2012-01-31 · Page 1 Chapter 4 Bits and Bytes • Why bits? • Representing information as bits –Binary/Hexadecimal –Byte representations • Representing

Page 2

Binary Representations

Base 2 Number Representation• Represent 1521310 as 111011011011012

• Represent 1.2010 as 1.0011001100110011[0011]…2

• Represent 1 5213 X 104 as 1 1101101101101 X 213• Represent 1.5213 X 104 as 1.11011011011012 X 213

Electronic Implementation• Easy to store with bistable elements

• Reliably transmitted on noisy and inaccurate wires

0 1 0

0.0V

0.5V

2.8V

3.3V

Introduction to Information Technology, Diplome FMIPA UGM

Most computers are digital

Recognize only two discrete states: on or off

Use a binary system to recognize two states

Use Number system with two unique digits: 0 and 1, called bits (short for binary digits)

Page 3: Chapter 4 Bits and Bytes · 2012-01-31 · Page 1 Chapter 4 Bits and Bytes • Why bits? • Representing information as bits –Binary/Hexadecimal –Byte representations • Representing

Page 3

Introduction to Information Technology, Diplome FMIPA UGM

What is a byte?

Eight bits grouped together as a unit Provides enough different combinations of 0s and 1s to

represent 256 individual charactersp Numbers

Uppercase and lowercase letters

Punctuation marks

(ascii 3)

(ascii 5)

marks(ascii D)

Byte-Oriented Memory Organization

Programs Refer to Virtual Addresses• Conceptually very large array of bytes

• Actually implemented with hierarchy of different memory types

SRAM DRAM disk– SRAM, DRAM, disk

– Only allocate for regions actually used by program

• In Unix and Windows NT, address space private to particular “process”

– Program being executed

– Program can clobber its own data, but not that of others

Compiler + Run-Time System Control Allocation• Where different program objects should be stored

• Multiple mechanisms: static, stack, and heap

• In any case, all allocation within single virtual address space

Page 4: Chapter 4 Bits and Bytes · 2012-01-31 · Page 1 Chapter 4 Bits and Bytes • Why bits? • Representing information as bits –Binary/Hexadecimal –Byte representations • Representing

Page 4

Machine Words

Machine Has “Word Size”• Nominal size of integer-valued dataNominal size of integer valued data

– Including addresses

• Most current machines are 32 bits (4 bytes)

– Limits addresses to 4GB

– Becoming too small for memory-intensive applications

• High-end systems are 64 bits (8 bytes)

– Potentially address 1.8 X 1019 bytes

• Machines support multiple data formats• Machines support multiple data formats

– Fractions or multiples of word size

– Always integral number of bytes

Word-Oriented Memory Organization

Addresses Specify Byte

000000010002

32-bitWords

Bytes Addr.64-bitWords

Addr =

0000Locations• Address of first byte in word

• Addresses of successive words differ by 4 (32-bit) or 8 (64-bit)

00020003000400050006000700080009

Addr =

0000

0000

Addr =

0004

Addr =

001000110012001300140015

Addr =

0008

=0008

Addr =

0012

Page 5: Chapter 4 Bits and Bytes · 2012-01-31 · Page 1 Chapter 4 Bits and Bytes • Why bits? • Representing information as bits –Binary/Hexadecimal –Byte representations • Representing

Page 5

Sizes of C Objects (in Bytes)

C Data Type Compaq Alpha Typical 32-bit Intel IA32

int 4 4 4int 4 4 4

long int 8 4 4

char 1 1 1

short 2 2 2

float 4 4 4

double 8 8 8

long double 8 8 10/12

char * 8 4 4char * 8 4 4

» Or any other pointer

Introduction to Information Technology, Diplome FMIPA UGM

Values for Different Word Sizes

W8 16 32 64

UMax 255 65,535 4,294,967,295 18,446,744,073,709,551,615TMax 127 32,767 2,147,483,647 9,223,372,036,854,775,807

C Programming• #include <limits.h>

– K&R, App. B11

• Declares constants, e.g.,

a 3 , 6 , , 83,6 9, 3,3 ,036,85 , 5,80TMin -128 -32,768 -2,147,483,648 -9,223,372,036,854,775,808

– ULONG_MAX

– LONG_MAX

– LONG_MIN

• Values platform-specific

Page 6: Chapter 4 Bits and Bytes · 2012-01-31 · Page 1 Chapter 4 Bits and Bytes • Why bits? • Representing information as bits –Binary/Hexadecimal –Byte representations • Representing

Page 6

Introduction to Information Technology, Diplome FMIPA UGM

Sign Extension Example

short int x = 15213;int ix = (int) x; short int y = -15213;int iy = (int) y;

Decimal Hex Binaryx 15213 3B 6D 00111011 01101101ix 15213 00 00 C4 92 00000000 00000000 00111011 01101101y -15213 C4 93 11000100 10010011iy -15213 FF FF C4 93 11111111 11111111 11000100 10010011

• Converting from smaller to larger integer data type

• C automatically performs sign extension

Word be ordered in memory

Conventions• Alphas, PC’s are “Little Endian” machines

– Least significant byte has lowest address• Sun’s, Mac’s are “Big Endian” machines

L t i ifi t b t h hi h t dd– Least significant byte has highest address

Example• Variable x has 4-byte representation 0x01234567• Address given by &x is 0x100

0x100 0x101 0x102 0x103Big Endian

address

01 23 45 67

0x100 0x101 0x102 0x103

67 45 23 01

Little Endiandata

Page 7: Chapter 4 Bits and Bytes · 2012-01-31 · Page 1 Chapter 4 Bits and Bytes • Why bits? • Representing information as bits –Binary/Hexadecimal –Byte representations • Representing

Page 7

Example: show_bytes in C

int a = 15213;

printf("int a = 15213;\n");

show bytes((pointer) &a sizeof(int));show_bytes((pointer) &a, sizeof(int));

Result:

int a = 15213;

0x11ffffcb8 0x6d

0x11ffffcb9 0x3b

0x11ffffcba 0x00

0x11ffffcbb 0x00

Example: Representing Integers

int A = 15213;

int B = -15213;

long int C = 15213;

Decimal: 15213

Binary: 0011 1011 0110 1101

Hex: 3 B 6 D

6D3B0000

Alpha A

3B6D

0000

Sun A

0000

6D3B0000

Alpha C

3B6D

0000

Sun C

93C4FFFF

Alpha B

C493

FFFF

Sun B000000

Two’s complement representation

Page 8: Chapter 4 Bits and Bytes · 2012-01-31 · Page 1 Chapter 4 Bits and Bytes • Why bits? • Representing information as bits –Binary/Hexadecimal –Byte representations • Representing

Page 8

Introduction to Information Technology, Diplome FMIPA UGM

Short Int Example

• One’s complement: Invert bits for negative numbers

• Sign magnitude: Invert sign bit for negative numbers

• short int exampless o t t e a p es

• ISO C does not define what encoding machines use for signed integers, but 99% (or more) use two’s complement.

15213 Unsigned 00111011 01101101-15213 Two’s complement 11000100 10010011-15213 One’s complement 11000100 10010010-15213 Sign magnitude 10111011 01101101

• For truly portable code, don’t count on it.

Example: Representing Floats

Float F = 15213.0;

IEEE Single Precision Floating Point RepresentationIEEE Single Precision Floating Point Representation

Hex: 4 6 6 D B 4 0 0 Binary: 0100 0110 0110 1101 1011 0100 0000 0000

15213: 1110 1101 1011 01

00B46D46

Alpha F

B400

466D

Sun F

Page 9: Chapter 4 Bits and Bytes · 2012-01-31 · Page 1 Chapter 4 Bits and Bytes • Why bits? • Representing information as bits –Binary/Hexadecimal –Byte representations • Representing

Page 9

Example: Representing Pointersint B = -15213;

int *P = &B;

Alpha Address

A0FCFF

Alpha P

Alpha Address

Hex: 1 F F F F F C A 0

Binary: 0001 1111 1111 1111 1111 1111 1100 1010 0000

01000000

FF

EF

Sun P

Sun Address

FB2C

FFSun Address

Hex: E F F F F B 2 C Binary: 1110 1111 1111 1111 1111 1011 0010 1100

Different compilers & machines assign different locations to objects

char S[6] = "15213";Representing StringsStrings in C

• Represented by array of characters

• Each character encoded in ASCII format

– Standard 7-bit encoding of character setAlpha S Sun S

Standard 7 bit encoding of character set

– Other encodings exist, but uncommon

– Character “0” has code 0x30

» Digit i has code 0x30+i

• String should be null-terminated

– Final character = 0

CompatibilityB t d i t i

3231

3135

3300

3231

3135

3300

• Byte ordering not an issue

– Data are single byte quantities

• Text files generally platform independent

– Except for different conventions of line termination character!

Page 10: Chapter 4 Bits and Bytes · 2012-01-31 · Page 1 Chapter 4 Bits and Bytes • Why bits? • Representing information as bits –Binary/Hexadecimal –Byte representations • Representing

Page 10

Representation Instruction Code

Encode Program as Sequence of Instructions• Each simple operation

– Arithmetic operationp

– Read or write memory

– Conditional branch

• Instructions encoded as bytes

– Alpha’s, Sun’s, Mac’s use 4 byte instructions

» Reduced Instruction Set Computer (RISC)

– PC’s use variable length instructions

» Complex Instruction Set Computer (CISC)Complex Instruction Set Computer (CISC)

• Different instruction types and encodings for different machines

– Most code not binary compatible

Example: Representing Instructions

int sum(int x, int y)

{

return x+y; 0000

Alpha sum

813

Sun sum

5589

PC sum

} 0030420180FA6B

E008

C3

90020009

• For this example, Alpha & Sun use two 4-byte instructions

– Use differing numbers of instructions in other cases

• PC uses 7 instructions with lengths 1, 2, and 3 bytes

E58B

89

450C034508

Different machines use totally different instructions and encodings

– Same for NT and for Linux

– NT / Linux not binary compatible

89EC5DC3

Page 11: Chapter 4 Bits and Bytes · 2012-01-31 · Page 1 Chapter 4 Bits and Bytes • Why bits? • Representing information as bits –Binary/Hexadecimal –Byte representations • Representing

Page 11

Boolean Algebra

Developed by George Boole in 19th Century• Algebraic representation of logic

– Encode “True” as 1 and “False” as 0

AndA&B = 1 when both A=1 and B=1

& 0 10 0 01 0 1

Not

OrA|B = 1 when either A=1 or B=1

| 0 10 0 11 1 1

Exclusive-Or (Xor)

~0 11 0

~A = 1 when A=0

^ 0 10 0 11 1 0

Exclusive-Or (Xor)A^B = 1 when either A=1 or B=1, but

not both

Gates and Symbols

Page 12: Chapter 4 Bits and Bytes · 2012-01-31 · Page 1 Chapter 4 Bits and Bytes • Why bits? • Representing information as bits –Binary/Hexadecimal –Byte representations • Representing

Page 12

Diagram of a Typical Computer Circuit

1Bit compare for Equality

Adder half

http://lpmpjogja.diknas.go.id/kc/b/boolean/boolean.htm

0 0 1 1

+ 0 + 1 + 0 + 1

00 01 01 10

The 1-ADD Circuit and Truth Table

Page 13: Chapter 4 Bits and Bytes · 2012-01-31 · Page 1 Chapter 4 Bits and Bytes • Why bits? • Representing information as bits –Binary/Hexadecimal –Byte representations • Representing

Page 13

Introduction to Information Technology, Diplome FMIPA UGM

Full Adder Circuit CI A B Q CO

0 0 0 0 0

0 0 1 1 0

0 1 0 1 0

1bit

0 1 1 0 1

1 0 0 1 0

1 0 1 0 1

1 1 0 0 1

1 1 1 1 1

4bit

1bit

http://lpmpjogja.diknas.go.id/kc/b/boolean/boolean.htm

Introduction to Information Technology, Diplome FMIPA UGM

Example

A: 0011

B: 0101

Page 14: Chapter 4 Bits and Bytes · 2012-01-31 · Page 1 Chapter 4 Bits and Bytes • Why bits? • Representing information as bits –Binary/Hexadecimal –Byte representations • Representing

©

Christian Jacob

n

Chapter Overview

Chapter 4

Binary Data Representatioand

Binary Arithmetic

4.1 Binary Data Representation

4.2 Important Number Systems for Computers

4.2.1 Number System Basics

4.2.2 Useful Number Systems for Computers

4.2.3 Decimal Number System

4.2.4 Binary Number System

4.2.5 Octal Number System

4.2.6 Hexadecimal Number System

4.2.7 Comparison of Number Systems

4.3 Powers of 2

Page 15: Chapter 4 Bits and Bytes · 2012-01-31 · Page 1 Chapter 4 Bits and Bytes • Why bits? • Representing information as bits –Binary/Hexadecimal –Byte representations • Representing

©

Christian Jacob

n

Chapter Overview

4.4 Conversions Between Number Systems

4.4.1 Conversion of Natural Numbers

4.4.2 Conversion of Rational Numbers

4.5 Binary Logic

4.6 Binary Arithmetic

4.7 Negative Numbers and Complements

4.7.1 Problems of Signed Number Representatio

4.7.2 1-Complement ((B-1)-Complement)

4.7.3 2-Complement (B-Complement)

4.8 Floating Point Numbers

4.8.1 Mantissa and Exponent

4.8.2 Floating Point Arithmetics

4.8.3 Limited Precision

4.9 Representation of Strings

4.9.1 Representation of Characters

4.9.2 Representation of Strings

Page 16: Chapter 4 Bits and Bytes · 2012-01-31 · Page 1 Chapter 4 Bits and Bytes • Why bits? • Representing information as bits –Binary/Hexadecimal –Byte representations • Representing

Page 3 Chapter 4: Binary Data Representation and Arithmetic © Christian Jacob

Prev Next Last

ation

/ 0, 5V / 0V

tates

rd length) which omputer in a single

First Back TOC

4.1 Binary Data Representation

Bit: smallest unit of inform

yes / no, on / off, L / 0, 1

Byte: group of 8 bits

--> 28

= 256 different

s

Word:

the number of bits (wocan be processed by a cstep (e.g., 32 or 64)

--> machine dependent

Representation:n-1

2n-1

3

8

2

4

1

2

0

1

. . .

Page 17: Chapter 4 Bits and Bytes · 2012-01-31 · Page 1 Chapter 4 Bits and Bytes • Why bits? • Representing information as bits –Binary/Hexadecimal –Byte representations • Representing

Page 4 Chapter 4: Binary Data Representation and Arithmetic © Christian Jacob

Prev Next Last

-bit pattern, with

word?

: 00, 01, 10, 11

First Back TOC

• The word size in any given computer is fixed.

Example: 16-bit word

⇒ every word (memory location) can hold a 16each bit either 0 or 1.

How many distinct patterns are there in a 16-bit

• Each bit has 2 possible values: 0 or 1

1 bit has 2 distinct patterns: 0, 1

• With 2 bits, each position has 2 possible values

2

2

= 4 distinct bit patterns

Page 18: Chapter 4 Bits and Bytes · 2012-01-31 · Page 1 Chapter 4 Bits and Bytes • Why bits? • Representing information as bits –Binary/Hexadecimal –Byte representations • Representing

Page 5 Chapter 4: Binary Data Representation and Arithmetic © Christian Jacob

Prev Next Last

or 1:

ave 2n distinct bit

entirely on the

First Back TOC

• With 3 bits, again each position can be either 0

000 100001 101010 110011 111

⇒ 23 = 8 distinct bit patterns

• In general, for n bits (a word of length

n

) we h

patterns.

• NOTE:

What these bit patterns mean dependscontext in which the patterns are used.

Page 19: Chapter 4 Bits and Bytes · 2012-01-31 · Page 1 Chapter 4 Bits and Bytes • Why bits? • Representing information as bits –Binary/Hexadecimal –Byte representations • Representing

Page 6 Chapter 4: Binary Data Representation and Arithmetic © Christian Jacob

Prev Next Last

r Computers

ds on its “assigned

alues.

th

0 1 2 … 9, , , ,{ }=

B Z 10= =

n

… d1B1

d0B0

+ + +

First Back TOC Important Number Systems for Computers

4.2 Important Number Systems fo

4.2.1 Number System Basics

• Number systems, as we use them, consist of

- a basic set of digits (or letters); example:

- a base

(how many digits); example:

• A

number

is a linear sequence of digits.

• The

value of a digit

at a specific position depen

meaning”

and on its position.

• The

value of a number

is the sum of these v

Number: with word leng

Value:

Z Z

B Z=

N

NB dn 1– dn 2– …d1d0=

N

B

d

i

B

i

i 0=

n

1–

d

n

1–

B

n

1–

d

n

2–

B

n

2–

+= =

Page 20: Chapter 4 Bits and Bytes · 2012-01-31 · Page 1 Chapter 4 Bits and Bytes • Why bits? • Representing information as bits –Binary/Hexadecimal –Byte representations • Representing

Page 7 Chapter 4: Binary Data Representation and Arithmetic © Christian Jacob

Prev Next Last

rs

9

9, A, B, C, D, E, F

First Back TOC Important Number Systems for Computers

4.2.2 Useful Number Systems for Compute

Name Base Digits

dualbinary

2 0, 1

octal 8 0, 1, 2, 3, 4, 5, 6, 7

decimal 10 0, 1, 2, 3, 4, 5, 6, 7, 8,

hexadecimalsedecimal

16 0, 1, 2, 3, 4, 5, 6, 7, 8,

Page 21: Chapter 4 Bits and Bytes · 2012-01-31 · Page 1 Chapter 4 Bits and Bytes • Why bits? • Representing information as bits –Binary/Hexadecimal –Byte representations • Representing

Page 8

Chapter 4: Binary Data Representation and Arithmetic

©

Christian Jacob

Prev Next Last

power of 10.

y a power of 10.

First Back TOC Important Number Systems for Computers

4.2.3 Decimal Number System

;

• Each position to the left of a digit increases by a

• Each position to the

right

of a digit

decreases

b

Example:

47692

10

= 2 · 10

0

+

9 · 10

1

+

6 · 10

2

+

7 · 10

3

+

4 · 10

4

= 2 + 90 + 600 + 7000 + 40000

Z 0 1 2 3 4 5 6 7 8 9, , , , , , , , ,{ }= B 10=

Page 22: Chapter 4 Bits and Bytes · 2012-01-31 · Page 1 Chapter 4 Bits and Bytes • Why bits? • Representing information as bits –Binary/Hexadecimal –Byte representations • Representing

Page 9 Chapter 4: Binary Data Representation and Arithmetic © Christian Jacob

Prev Next Last

a power of 2.

by a power of 2.

28 = 18510

First Back TOC Important Number Systems for Computers

4.2.4 Binary Number System

;

• Each position to the left of a digit increases by

• Each position to the

right

of a digit

decreases

Example:

10111001

2

= 1 · 2

0

+

0 · 2

1

+

0 · 2

2

+

1 · 2

3

+

1 · 2

4

+

1 · 2

5

+

0 · 2

6

+

1 · 2 7

= 1 + 8 + 16 + 32 + 1

Z 0 1,{ }= B 2=

Page 23: Chapter 4 Bits and Bytes · 2012-01-31 · Page 1 Chapter 4 Bits and Bytes • Why bits? • Representing information as bits –Binary/Hexadecimal –Byte representations • Representing

Page 10 Chapter 4: Binary Data Representation and Arithmetic © Christian Jacob

Prev Next Last

First Back TOC Important Number Systems for Computers

Counting in Binary

Decimal Dual Decimal Dual

0 00 000 16 10 000

1 00 001 17

10 001

2

00 010

18

10 010

3

00 011

19

10 011

4

00 100

20

10 100

5

00 101

21

10 101

6

00 110

22

10 110

7

00 111

23

10 111

8

01 000

24

11 000

9

01 001

25

11 001

10

01 010

26

11 010

11

01 011

27

11 011

12

01 100

28

11 100

13

01 101

29

11 101

14

01 110

30

11 110

15

01 111

31

11 111

Page 24: Chapter 4 Bits and Bytes · 2012-01-31 · Page 1 Chapter 4 Bits and Bytes • Why bits? • Representing information as bits –Binary/Hexadecimal –Byte representations • Representing

Page 11 Chapter 4: Binary Data Representation and Arithmetic © Christian Jacob

Prev Next Last

a power of 8.

by a power of 8.

1 · 4096

First Back TOC Important Number Systems for Computers

4.2.5 Octal Number System

;

• Each position to the left of a digit increases by

• Each position to the

right

of a digit

decreases

Example:

12403

8

= 3 · 8

0

+

0 · 8

1

+

4 · 8

2

+

2 · 8

3

+

1 · 8

4

= 3 · 1 + 0 · 8 + 4 · 64 + 2 · 512 +

= 5379

10

Z 0 1 2 3 4 5 6 7, , , , , , ,{ }= B 8=

Page 25: Chapter 4 Bits and Bytes · 2012-01-31 · Page 1 Chapter 4 Bits and Bytes • Why bits? • Representing information as bits –Binary/Hexadecimal –Byte representations • Representing

Page 12 Chapter 4: Binary Data Representation and Arithmetic © Christian Jacob

Prev Next Last

First Back TOC Important Number Systems for Computers

Counting in Octal

Decimal Octal Decimal Octal

0 0 0 16 2 0

1 0 1

17

2 1

2

0 2

18

2 2

3

0 3

19

2 3

4

0 4

20

2 4

5

0 5

21

2 5

6

0 6

22

2 6

7

0 7

23

2 7

8

1 0

24

3 0

9

1 1

25

3 1

10

1 2

26

3 2

11

1 3

27

3 3

12

1 4

28

3 4

13

1 5

29

3 5

14

1 6

30

3 6

15

1 7

31

3 7

Page 26: Chapter 4 Bits and Bytes · 2012-01-31 · Page 1 Chapter 4 Bits and Bytes • Why bits? • Representing information as bits –Binary/Hexadecimal –Byte representations • Representing

Page 13 Chapter 4: Binary Data Representation and Arithmetic © Christian Jacob

Prev Next Last

power of 16.

a power of 16.

096 + 15 · 65536

16

First Back TOC Important Number Systems for Computers

4.2.6 Hexadecimal Number System

;

Each position to the left of a digit increases by a

Each position to the

right

of a digit

decreases

by

Example:

FB40A

16

= 10 · 16

0

+

0 · 16

1

+

4 · 16

2

+

11 · 16

3

+

15 · 16

4

= 10 · 1 + 0 · 16 + 4 · 256 + 11 · 4

= 1,029,130 10

Z 0 1 2 3 4 5 6 7 8 9 A B C D E F, , , , , , , , , , , , , , ,{ }= B =

Page 27: Chapter 4 Bits and Bytes · 2012-01-31 · Page 1 Chapter 4 Bits and Bytes • Why bits? • Representing information as bits –Binary/Hexadecimal –Byte representations • Representing

Page 14 Chapter 4: Binary Data Representation and Arithmetic © Christian Jacob

Prev Next Last

First Back TOC Important Number Systems for Computers

Counting in Hexadecimal

Decimal Hexadecimal Decimal Hexadecimal

0 0 0 16 1 0

1 0 1

17

1 1

2

0 2

18

1 2

3

0 3

19

1 3

4

0 4

20

1 4

5

0 5

21

1 5

6

0 6

22

1 6

7

0 7

23

1 7

8

0 8

24

1 8

9

0 9

25

1 9

10

0 A

26

1 A

11

0 B

27

1 B

12

0 C

28

1 C

13

0 D

29

1 D

14

0 E

30

1 E

15

0 F

31

1 F

Page 28: Chapter 4 Bits and Bytes · 2012-01-31 · Page 1 Chapter 4 Bits and Bytes • Why bits? • Representing information as bits –Binary/Hexadecimal –Byte representations • Representing

Page 15 Chapter 4: Binary Data Representation and Arithmetic © Christian Jacob

Prev Next Last

Octal Hexadecimal

2 0 1 0

2 1 1 1

2 2 1 2

2 3 1 3

2 4 1 4

2 5 1 5

2 6 1 6

2 7 1 7

3 0 1 8

3 1 1 9

3 2 1 A

3 3 1 B

3 4 1 C

3 5 1 D

3 6 1 E

3 7 1 F

First Back TOC Important Number Systems for Computers

4.2.7 Comparison of Number Systems

Decimal Dual Octal Hexadecimal Decimal Dual

0 00 000 0 0 0 0 16 10 000

1 00 001 0 1 0 1 17 10 001

2 00 010 0 2 0 2 18 10 010

3 00 011 0 3 0 3 19 10 011

4 00 100 0 4 0 4 20 10 100

5 00 101 0 5 0 5 21 10 101

6 00 110 0 6 0 6 22 10 110

7 00 111 0 7 0 7 23 10 111

8 01 000 1 0 0 8 24 11 000

9 01 001 1 1 0 9 25 11 001

10 01 010 1 2 0 A 26 11 010

11 01 011 1 3 0 B 27 11 011

12 01 100 1 4 0 C 28 11 100

13 01 101 1 5 0 D 29 11 101

14 01 110 1 6 0 E 30 11 110

15 01 111 1 7 0 F 31 11 111

Page 29: Chapter 4 Bits and Bytes · 2012-01-31 · Page 1 Chapter 4 Bits and Bytes • Why bits? • Representing information as bits –Binary/Hexadecimal –Byte representations • Representing

Page 16

Chapter 4: Binary Data Representation and Arithmetic

©

Christian Jacob

Prev Next Last

First Back TOC Powers of 2

4.3 Powers of 2

N 2N N 2N

0 1 17 131,072

1 2 18 262,144

2 4 19 524,288

3 8 20 1,048,576

4 16 21 2,097,152

5 32 22 4,194,304

6 64 23 8,388,608

7 128 24 16,777,216

8 256 25 33,554,432

9 512 26 67,108,864

10 1,024 27 134,217,728

11 2,048 28 268,435,456

12 4,096 29 536,870,912

13 8,192 30 1,073,741,824

14 16,384 31 2,147,483,648

15 32,768 32 4,294,967,296

16 65,536 33 8,589,934,592

Page 30: Chapter 4 Bits and Bytes · 2012-01-31 · Page 1 Chapter 4 Bits and Bytes • Why bits? • Representing information as bits –Binary/Hexadecimal –Byte representations • Representing

Page 17

Chapter 4: Binary Data Representation and Arithmetic

©

Christian Jacob

Prev Next Last

Systems

a number of

& remainder .

.

number .

o to step 1.

N10

R

B

NB

First Back TOC Conversions Between Number Systems

4.4 Conversions Between Number

4.4.1 Conversion of Natural Numbers

Base 10 ⇒ Base B

Conversion

We use the metod of

iterated division

to convert

base 10 into a number of base .

1. Divide the number by : whole number

- Take as the next number to be divided by

- Keep as the next left-most digit of the new

2. If is zero then STOP, else set and g

NB B

N10 B N

N

R

N N10 N=

Page 31: Chapter 4 Bits and Bytes · 2012-01-31 · Page 1 Chapter 4 Bits and Bytes • Why bits? • Representing information as bits –Binary/Hexadecimal –Byte representations • Representing

Page 18 Chapter 4: Binary Data Representation and Arithmetic © Christian Jacob

Prev Next Last

e

127 · 8 = 1016

15 · 8 = 120

1 · 8 = 8

B 8=

First Back TOC Conversions Between Number Systems

Example: 102010 = ? 8

Number to be converted: ; target bas

Step 1: 1020 : 8 = 127 Remainder: 4

Step 2: 127 : 8 = 15 Remainder:

7

Step 3: 15 : 8 = 1 Remainder:

7

Step 4: 1 : 8 = 0 Remainder:

1

1020

10

= 1774

8

N10 1020=

Page 32: Chapter 4 Bits and Bytes · 2012-01-31 · Page 1 Chapter 4 Bits and Bytes • Why bits? • Representing information as bits –Binary/Hexadecimal –Byte representations • Representing

Page 19 Chapter 4: Binary Data Representation and Arithmetic © Christian Jacob

Prev Next Last

nvert a number

NB

B 10=

First Back TOC Conversions Between Number Systems

Base B ⇒ Base 10 Conversion

We use the metod of iterated multiplication

to co

of base into a number of base 10.

We simply add the “contribution” of each digit.

Example:

1F2

16

= ?

10

Number to be converted: ; target base

1F2

16

= 2 · 16

0

+

15 · 16

1

+

1 · 16

2

= 2 + 240 + 256

⇒ 1F2 16 = 498

10

B N10

N16 1F2=

Page 33: Chapter 4 Bits and Bytes · 2012-01-31 · Page 1 Chapter 4 Bits and Bytes • Why bits? • Representing information as bits –Binary/Hexadecimal –Byte representations • Representing

Page 20 Chapter 4: Binary Data Representation and Arithmetic © Christian Jacob

Prev Next Last

its.

ary digits.

First Back TOC Conversions Between Number Systems

Conversions between Numbers 2n and 2m

Conversion

2

n

2

1

n = 3

: octal

dual:

Replace 1 octal digit by 3 binary dig

Example

: 3 5 7

8

= 011 101 111

2

n = 4:

hexadecimal

dual

Replace 1 hexadecimal digit by 4 bin

Example

: 1 F 2

16

= 0001 1111 0010

2

Page 34: Chapter 4 Bits and Bytes · 2012-01-31 · Page 1 Chapter 4 Bits and Bytes • Why bits? • Representing information as bits –Binary/Hexadecimal –Byte representations • Representing

Page 21 Chapter 4: Binary Data Representation and Arithmetic © Christian Jacob

Prev Next Last

it.

imal digit.

111 00102

F 216

First Back TOC Conversions Between Number Systems

• Conversion 21 ⇒ 2

n

n = 3

: dual

octal:

Replace 3 binary digits by 1 octal dig

Example

: 101111010

2

= 101 111 010

2

= 5

7

2

8

n = 4:

hexadecimal

dual

Replace 4 binary digits by 1 hexadec

Example

: 11000111110010

2

= 11 0001 1

= 3 1

Page 35: Chapter 4 Bits and Bytes · 2012-01-31 · Page 1 Chapter 4 Bits and Bytes • Why bits? • Representing information as bits –Binary/Hexadecimal –Byte representations • Representing

Page 22 Chapter 4: Binary Data Representation and Arithmetic © Christian Jacob

Prev Next Last

18

012

012

16

First Back TOC Conversions Between Number Systems

• Conversion 2n ⇒ 2

m

2

n

2

1

2

m

Example

: octal

hexadecimal

26351

8

= 2 6 3 5

octal to binary: = 010 110 011 101 0

rearrange binary: = 0010 1100 1110 10

= 2 C E 9

Page 36: Chapter 4 Bits and Bytes · 2012-01-31 · Page 1 Chapter 4 Bits and Bytes • Why bits? • Representing information as bits –Binary/Hexadecimal –Byte representations • Representing

Page 23 Chapter 4: Binary Data Representation and Arithmetic © Christian Jacob

Prev Next Last

⇒ or ⇒

tly” small number.

+ 1 · 2-5

0.0625 + 1 · 0.03125

10 RB RB

First Back TOC Conversions Between Number Systems

4.4.2 Conversion of Rational Numbers

Note: An exact conversion of rational numbers

is not always possible.

• We want: , where is a “sufficien

Base

B

Base 10 Conversion

Example

: 0.11001

2

= ?

10

0.11001 = 1 · 2

-1

+ 1 · 2

-2

+ 0 · 2

-3

+ 0 · 2

-4

= 1 · 0.5 + 1 · 0.25 + 0 · 0.125 + 0 ·

= 0.5 + 0.25 + 0.03125

= 0.78125

10

R

R10

R10 RB ε+= ε

Page 37: Chapter 4 Bits and Bytes · 2012-01-31 · Page 1 Chapter 4 Bits and Bytes • Why bits? • Representing information as bits –Binary/Hexadecimal –Byte representations • Representing

Page 24 Chapter 4: Binary Data Representation and Arithmetic © Christian Jacob

Prev Next Last

0

0

1

1

0

0

0

0

1

z i–( )

lication!

First Back TOC Conversions Between Number Systems

Base 10 ⇒ Base B Conversion

Example: 0.1910

= ?

2

with

k = 9

bit precision

0.19

10

= 0.001100001

2

+

ε

Step Operation

1 0.19 0.19 · 2 = 0.38 0.38

2 0.38 0.38 · 2 = 0.76 0.76

3 0.76 0.76 · 2 = 1.52 0.52

4 0.52 0.52 · 2 = 1.04 0.04

5 0.04 0.04 · 2 = 0.08 0.08

6 0.08 0.08 · 2 = 0.16 0.16

7 0.16 0.16 · 2 = 0.32 0.32

8 0.32 0.32 · 2 = 0.64 0.64

9 0.64 0.64 · 2 = 1.28 0.28

i N R

Multip

Page 38: Chapter 4 Bits and Bytes · 2012-01-31 · Page 1 Chapter 4 Bits and Bytes • Why bits? • Representing information as bits –Binary/Hexadecimal –Byte representations • Representing

Page 25 Chapter 4: Binary Data Representation and Arithmetic © Christian Jacob

Prev Next Last

First Back TOC Binary Logic

4.5 Binary Logic

Logical AND ( )

Logical OR ( )

AND 0 1

0 0 0

1 0 1

OR 0 1

0 0 1

1 1 1

x y∧

x y∨

Page 39: Chapter 4 Bits and Bytes · 2012-01-31 · Page 1 Chapter 4 Bits and Bytes • Why bits? • Representing information as bits –Binary/Hexadecimal –Byte representations • Representing

Page 26 Chapter 4: Binary Data Representation and Arithmetic © Christian Jacob

Prev Next Last

First Back TOC Binary Logic

Logical NOT ( , negation)

Logical XOR (exclusive OR)

Logical NAND (negated AND)

x

0 1

1 0

XOR 0 1

0 0 1

1 1 0

NAND 0 1

0 1 1

1 1 0

Page 40: Chapter 4 Bits and Bytes · 2012-01-31 · Page 1 Chapter 4 Bits and Bytes • Why bits? • Representing information as bits –Binary/Hexadecimal –Byte representations • Representing

Page 27

Chapter 4: Binary Data Representation and Arithmetic © Christian Jacob

Prev Next Last

First Back TOC Binary Logic

Examples:

11001010 01000111 1

1

1

10000

AND

00110101 0

1

1100

1

0

1

0

1

0101000000000 0

1

0000

1

0

1

0

1

00000

11001010 0

1

0001

1

1

1

1

1

10000

NAND

00110101 0

1

1100

1

0

1

0

1

0101011111111 1

0

1111

0

1

0

1

0

11111

11001010

0

100

0

111 11110

0

0

0OR

00110101

0

111

0

010 10101

0

1

0

11111111

0

111

0

111 11111

0

1

0

11001010

01

00

0

1

1

1

1

1

1

10

0

0

0XOR

00110101

01

11

0

0

1

0

1

0

1

01

0

1

011111111 00110101 01011010

Page 41: Chapter 4 Bits and Bytes · 2012-01-31 · Page 1 Chapter 4 Bits and Bytes • Why bits? • Representing information as bits –Binary/Hexadecimal –Byte representations • Representing

Page 28

Chapter 4: Binary Data Representation and Arithmetic

©

Christian Jacob

Prev Next Last

multiplication

arry

000

1

0

100

0000

First Back TOC Binary Arithmetic

4.6 Binary Arithmetic

Elementary Rules for addition, subtraction, and

Operation Result C

Addition0 + 00 + 11 + 01 + 1

0110

Subtraction0 - 00 - 11 - 01 - 1

0110

Multiplication0 · 00 · 11 · 01 · 1

0001

Page 42: Chapter 4 Bits and Bytes · 2012-01-31 · Page 1 Chapter 4 Bits and Bytes • Why bits? • Representing information as bits –Binary/Hexadecimal –Byte representations • Representing

Page 29 Chapter 4: Binary Data Representation and Arithmetic © Christian Jacob

Prev Next Last

–––-

–––-

–––-

First Back TOC Binary Arithmetic

Examples:

Addition: Subtraction:

1 0 1 0 1 1 0 1+ 1 0 1 1 - 1 0 1 0––––––––––––––––- –––––––––––––carry: 1

0

1

1 carry: 1 0

1

0+ 1 0 1 1 1 -1 1 1 0––––––––––––––––- –––––––––––––

1 0 1 0 1 0 0 1 1

10

10

13

10

+ 11

10

- 10

10

––––––––––––––––- –––––––––––––21

10

3

10

Page 43: Chapter 4 Bits and Bytes · 2012-01-31 · Page 1 Chapter 4 Bits and Bytes • Why bits? • Representing information as bits –Binary/Hexadecimal –Byte representations • Representing

Page 30 Chapter 4: Binary Data Representation and Arithmetic © Christian Jacob

Prev Next Last

First Back TOC Binary Arithmetic

Multiplication:

1 1 1 · 1 0 1––––––––––––––––––––––––

1 1 1 0 0 0

+ 1 1 1––––––––––––––––––––––––carry: 1+ 1carry: 1+ 1carry: 1+

1––––––––––––––––––––––––

1 0 0 0 1 1

Page 44: Chapter 4 Bits and Bytes · 2012-01-31 · Page 1 Chapter 4 Bits and Bytes • Why bits? • Representing information as bits –Binary/Hexadecimal –Byte representations • Representing

Page 31 Chapter 4: Binary Data Representation and Arithmetic © Christian Jacob

Prev Next Last

ments

2n-1-1

0

First Back TOC Negative Numbers and Complements

4.7 Negative Numbers and Comple

Integer Numbers: A possible representation

Number = sign & value

• sign = 0 ⇒ +, positive number

• sign = 1 ⇒ -, negative number

Range of integer numbers for

n

bits: - 2

n-1

+1 …

15 14 1

sign value

Page 45: Chapter 4 Bits and Bytes · 2012-01-31 · Page 1 Chapter 4 Bits and Bytes • Why bits? • Representing information as bits –Binary/Hexadecimal –Byte representations • Representing

Page 32 Chapter 4: Binary Data Representation and Arithmetic © Christian Jacob

Prev Next Last

Negative zero?

First Back TOC Negative Numbers and Complements

Example (1): n = 3

Range: - 23-1+1 … 23-1-1

= - 2

2

+1 … 2

2

-1= - 4 + 1 … 4 -1 = -3 ... +3

Sign Value Number

0 0 0 0

0 0 1 1

0 1 0 2

0 1 1 3

1 0 0

?

1 0 1 - 1

1 1 0 - 2

1 1 1 - 3

Page 46: Chapter 4 Bits and Bytes · 2012-01-31 · Page 1 Chapter 4 Bits and Bytes • Why bits? • Representing information as bits –Binary/Hexadecimal –Byte representations • Representing

Page 33 Chapter 4: Binary Data Representation and Arithmetic © Christian Jacob

Prev Next Last

tation

tive

se 10)

1 1––

0

First Back TOC Negative Numbers and Complements

4.7.1 Problems of Signed Number Represen

• Given 2 binary numbers: one positive, one nega

0 0 1 1 0 0 (base 2) 12 (ba+ 1 0 1 1 1 1 -15

––––––––––––––– ––––––1 1 1 0 1 1 (= -2710) -3

• “1-1”: 1 - 1 = 1 + (-1) = 0 ?

0 0 0 1 0 0+ 1 0 0 1 - 0 0–––––––––––– –––––––

1 0 1 0 (= -210) 0 0

Clearly this won´t work!

Solution: 1-Complement and 2-Complement

Page 47: Chapter 4 Bits and Bytes · 2012-01-31 · Page 1 Chapter 4 Bits and Bytes • Why bits? • Representing information as bits –Binary/Hexadecimal –Byte representations • Representing

Page 34 Chapter 4: Binary Data Representation and Arithmetic © Christian Jacob

Prev Next Last

positive

Decimal

- 9

+ 6

-15

?

Problem!

First Back TOC Negative Numbers and Complements

4.7.2 1-Complement ((B-1)-Complement)

We represent negative numbers by negating the

representation of the number (and vice versa):

• any 1 is changed to a 0

• any 0 is changed to a 1

Example

:

Binary Decimal One´s Complement

0 1001 9 1 0110

1 1001 -6 0 0110

0 0000 0 1 1111

0 1111 15 1 0000

Page 48: Chapter 4 Bits and Bytes · 2012-01-31 · Page 1 Chapter 4 Bits and Bytes • Why bits? • Representing information as bits –Binary/Hexadecimal –Byte representations • Representing

Page 35 Chapter 4: Binary Data Representation and Arithmetic © Christian Jacob

Prev Next Last

Decimal

-9

-3

0

-15

1

10

First Back TOC Negative Numbers and Complements

4.7.3 2-Complement (B-Complement)

• Positive numbers are represented as usual.

• Negative numbers are created by

- negating the positive value and

- adding one

(this avoids the negative zero).

Examples:

Binary Decimal Two´s Complement

0 1001 9 1 0110 + 1 = 1 0111

0 1101 3 1 0010 + 1 = 1 0011

0 0000 0 1 1111 + 1 = 0 0000

0 1111 15 1 0000 + 1 = 1 0001

1 1111 -1 ¬(1 1111 - 1) = ¬(1 1110) = 0 0001

1 0110 -10 0 1010

Page 49: Chapter 4 Bits and Bytes · 2012-01-31 · Page 1 Chapter 4 Bits and Bytes • Why bits? • Representing information as bits –Binary/Hexadecimal –Byte representations • Representing

Page 36 Chapter 4: Binary Data Representation and Arithmetic © Christian Jacob

Prev Next Last

ent

1

10

111

00000110

Subtraction

First Back TOC Negative Numbers and Complements

Number ring for 5-digit 2-complem

00000

0000100010

0001100100

0010

001

00

11000

10111

10110

10101

1010010011

1001010001

1111

1

1111

0

1110

1

1110

0110111101011001

10000

1 2 34

5

67

8

910

1112

131415

16-16

positivenumbers

negativenumbers

0

-1-2-3-4

-5

-6-7

-8

-9-10

-11-12

-13-14-15

31302928

27

2625

24

2322

2120

19 18 17

0101

0100101101

100

0110

1

0111

0

0111

1

Addition

Page 50: Chapter 4 Bits and Bytes · 2012-01-31 · Page 1 Chapter 4 Bits and Bytes • Why bits? • Representing information as bits –Binary/Hexadecimal –Byte representations • Representing

Page 37 Chapter 4: Binary Data Representation and Arithmetic © Christian Jacob

Prev Next Last

formed as simple

ample 2

0 - 1310

13: 011013)B-1: 10010

+ 1–––––––

-13)B: 10011

9: 01001-13)B: + 10011

–––––––––-4: 11100

First Back TOC Negative Numbers and Complements

With the 2-complement, subtractions can be peradditions:

Example: 5 digit precision, base 10

Example 1 Ex

B = 10 1410 -

7

10

9

1

Complement 7: 00111(-7)

B-1

: 11000+ 1–––––––

(-7)

B

: 11001

(-1

(

Addition 14: 01110+(-7)

B

: + 11001

–––––––––

7: 1 00111

+(

Page 51: Chapter 4 Bits and Bytes · 2012-01-31 · Page 1 Chapter 4 Bits and Bytes • Why bits? • Representing information as bits –Binary/Hexadecimal –Byte representations • Representing

Page 38 Chapter 4: Binary Data Representation and Arithmetic © Christian Jacob

Prev Next Last

scientific notation:

7

look like this:

ndancy.

g point) number is:

= 11 bits

First Back TOC Floating Point Numbers

4.8 Floating Point Numbers

4.8.1 Mantissa and Exponent

Recall the general form of numbers expressed in

• 3.14159265358979323846264338327950288419

• 6.02 x 1023

Printed out by a computer these numbers usually

• 3.1415e0

• 6.02e23

This representation saves space and reduces redu

Hence, the usual format for a 32-bit real (floatin

mantissa = 21 bits exponent

Page 52: Chapter 4 Bits and Bytes · 2012-01-31 · Page 1 Chapter 4 Bits and Bytes • Why bits? • Representing information as bits –Binary/Hexadecimal –Byte representations • Representing

Page 39 Chapter 4: Binary Data Representation and Arithmetic © Christian Jacob

Prev Next Last

s and Mantissae

0.740 · 105

+ 0.008 · 105

sult: 0.748 · 105

First Back TOC Floating Point Numbers

4.8.2 Floating Point Arithmetics

Example: 0.740 · 105 + 0.843 · 103

Addition:

• 1. Identify mantissae and exponents

• 2. Compare exponents

• 3. Adjust exponents if necessary

• 4. Adjust the shorter mantissa

• 5. Add mantissae

• 6. Normalize mantissa

• 7. Adjust exponent

Operands Compare Exponents Adjust Exponent

0.740 · 10

5

0.843 · 10

3

e

1

- e

2

= 2

Re

Page 53: Chapter 4 Bits and Bytes · 2012-01-31 · Page 1 Chapter 4 Bits and Bytes • Why bits? • Representing information as bits –Binary/Hexadecimal –Byte representations • Representing

Page 40 Chapter 4: Binary Data Representation and Arithmetic © Christian Jacob

Prev Next Last

First Back TOC Floating Point Numbers

Example: Multiplication

• 1. Multiply of mantissae

• 2. Normalize mantissae

• 3. Adjust exponents

• 4. Add exponents

Example: (0.792 · 105) · (0.116 · 10-3

)

Step 1

: multiplication of the mantissae:

0.792 · 0.116 =

0.792 · 10

-1

+

0.792 · 10

-2

+

4.752 · 10

-3

Page 54: Chapter 4 Bits and Bytes · 2012-01-31 · Page 1 Chapter 4 Bits and Bytes • Why bits? • Representing information as bits –Binary/Hexadecimal –Byte representations • Representing

Page 41 Chapter 4: Binary Data Representation and Arithmetic © Christian Jacob

Prev Next Last

e mantissae

1

0.918 · 10-1

1

ntissa)

e mantissae

1

0.918 · 10-1

1

First Back TOC Floating Point Numbers

• Step 1 (cont.): step-by-step multiplication of th

0.792 · 10-1 0.792 · 10-1

0.871 · 10

-

+ 0.792 · 10

-2

0.079 · 10

-1

+ 0.4752 · 10

-2

0.047 · 10

-

Step 2

:

5 + (-3) + (-1) = 1

Result

:

0.918 · 10

1 (with 3 digits of precision in the ma

Step 1

(cont.): step-by-step multiplication of th

0.792 · 10

-1

0.792 · 10

-1

0.871 · 10

-

+ 0.792 · 10

-2

0.079 · 10

-1

+ 0.4752 · 10

-2

0.047 · 10

-

Step 2

: Add exponents

5 + (-3) + (-1) = 1

Result

:

0.918 · 10

1 (with 3 digits of precision)

Page 55: Chapter 4 Bits and Bytes · 2012-01-31 · Page 1 Chapter 4 Bits and Bytes • Why bits? • Representing information as bits –Binary/Hexadecimal –Byte representations • Representing

Page 42 Chapter 4: Binary Data Representation and Arithmetic © Christian Jacob

Prev Next Last

0?

First Back TOC Floating Point Numbers

4.8.3 Limited Precision

Note: Not all values can be represented!

Example:

- mantissa: 2 decimal digits

- exponent: 1 decimal digit

• Sample number: 74 · 102 = 7400

What is the next higher value?

75 · 10

2

= 7500

What about values 7400 < x < 750

They cannot be represented!!!

• Remedy, but not a perfect solution:

Increase the number of mantissa digits.

Page 56: Chapter 4 Bits and Bytes · 2012-01-31 · Page 1 Chapter 4 Bits and Bytes • Why bits? • Representing information as bits –Binary/Hexadecimal –Byte representations • Representing

Page 43 Chapter 4: Binary Data Representation and Arithmetic © Christian Jacob

Prev Next Last

by 1 byte

C2

y 2 bytes

First Back TOC Representation of Strings

4.9 Representation of Strings

4.9.1 Representation of Characters

Usually: single characters are represented

different standards: ASCII1, EBCDI

Currently: internationalization with

Unicode

a single character is represented b

1. ASCII: American Standard Code for Information Interchange2. EBCDIC: Extended Binary Coded Decimal Interchange Code (derived from punch card standards)

Page 57: Chapter 4 Bits and Bytes · 2012-01-31 · Page 1 Chapter 4 Bits and Bytes • Why bits? • Representing information as bits –Binary/Hexadecimal –Byte representations • Representing

Page 44 Chapter 4: Binary Data Representation and Arithmetic © Christian Jacob

Prev Next Last

D C D C

96 ‘ 112 p

97 a 113 q

98 b 114 r

99 c 115 s

100 d 116 t

101

e

117

u

102

f

118

v

103

g

119

w

104 h 120 x

105 i 121 y

106 j 122 z

107 k 123 {

108 l 124 |

109 m 125 }

110 n 126 ~

111 o 127 DEL

First Back TOC Representation of Strings

ASCII Character Encoding

Compare the Unicode character encodings …

D C D

C

D

C

D

C

D

C

D

C

0

NUL

16

DLE

32

SP

48

0

64

@

80

P

1

SCH

17

DC1

33

!

49

1

65

A

81

Q

2

STX

18

DC2

34

50

2

66

B

82

R

3

ETX

19

DC3

35

#

51

3

67

C

83

S

4

EOT

20

DC4

36

$

52

4

68

D

84

T

5

ENQ

21

NAK

37

% 53 5 69 E 85 U

6 ACK 22 SYN 38 & 54 6 70 F 86 V

7 BEL 23 ETB 39 ´ 55 7 71 G 87 W

8 BS 24 CAN 40 ( 56 8 72 H 88 X

9 HT 25 EM 41 ) 57 9 73 I 89 Y

10 LF 26 SUB 42 * 58 : 74 J 90 Z

11 VT 27 ESC 43 + 59 ; 75 K 91 [

12 FF 28 FS 44 , 60 < 76 L 92 \

13 CR 29 GS 45 - 61 = 77 M 93 ]

14 S0 30 RS 46 . 62 > 78 N 94 ^

15 S1 31 US 47 / 63 ? 79 O 95 _

Page 58: Chapter 4 Bits and Bytes · 2012-01-31 · Page 1 Chapter 4 Bits and Bytes • Why bits? • Representing information as bits –Binary/Hexadecimal –Byte representations • Representing

Page 45 Chapter 4: Binary Data Representation and Arithmetic © Christian Jacob

Prev Next Last

rs (

usually: byte

6

t

7

e r

First Back TOC Representation of Strings

4.10 Representation of Strings

Strings are represented by sequences of charactesequence)

1. Fixed length strings (example: length = 7)

2. Variable length strings

3. Encoding with string length at the beginning

1

P

2

e

3

t

4

e

5

r

6

SP

7

SP

1

C

2

o

3

m

4

p

5

u

1

P

2

e

3

t

4

e

5

r

6

NUL

7

n-1

n

2

P

3

e

4

t

5

e

6

r

7

8

n-1

n1

5

Page 59: Chapter 4 Bits and Bytes · 2012-01-31 · Page 1 Chapter 4 Bits and Bytes • Why bits? • Representing information as bits –Binary/Hexadecimal –Byte representations • Representing

Page 46 Chapter 4: Binary Data Representation and Arithmetic © Christian Jacob

Prev Next Last

are possible:

101 1048

101 1418

101 1018

First Back TOC Representation of Strings

With ASCII charcter encoding string comparisons

´AA´ < ´AD´ because: 101 1018 <

´AA´ < ´Aa´ 101 101

8

<

´A1´ < ´AA´ 101 061

8

<

Hence, sorting of strings becomes easy:

Compare their numerical representations!