seminar 2 what is a number? - idatdde25/info/slides/tdde25_fo2_4sl.pdf · what is a number? seminar...

16
What is a Number? Seminar 2 Types of Numbers Number Systems Representation in Computers Types of Numbers Natural Integer Rational Real Complex Russian Dolls Model Nine Zulu Queens Ruled China N Z Q R C Natural Numbers Addition - Closed Multiplication - Closed Subtraction - Not Closed Division - Not Closed Solving the Subtraction Problem: Discovery of zero (600 CE) Discovery of negative numbers Han Dynasty 220 BCE - 202 CE Europe: 17th century N Counting Numbers CE - Common era Integers Addition - Closed Multiplication - Closed Subtraction - Closed Division - Not Closed Solving the Division Problem: Discovery of rational numbers Ratios between two integers

Upload: vodiep

Post on 25-Feb-2019

220 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: seminar 2 What is a Number? - IDATDDE25/info/slides/TDDE25_Fo2_4sl.pdf · What is a Number? Seminar 2 Types of Numbers Number Systems Representation in Computers Types of Numbers

What is a Number?Seminar 2

Types of NumbersNumber Systems

Representation in Computers

Types of Numbers

Natural

Integer

RationalReal

ComplexRussian Dolls

Model

Nine Zulu Queens Ruled China

Contents

1. seminar 2

NZQRC

1

Contents

1. seminar 2

NZQRC

1

Contents

1. seminar 2

NZQRC

1

Contents

1. seminar 2

NZQRC

1

Contents

1. seminar 2

NZQRC

1

Natural Numbers

• Addition - Closed• Multiplication - Closed• Subtraction - Not Closed• Division - Not Closed

Solving the Subtraction Problem:• Discovery of zero (600 CE)• Discovery of negative numbers

• Han Dynasty 220 BCE - 202 CE• Europe: 17th century

Contents

1. seminar 2

NZQRC

1

Counting Numbers

CE - Common era

Integers

• Addition - Closed• Multiplication - Closed• Subtraction - Closed• Division - Not Closed

Solving the Division Problem:• Discovery of rational numbers• Ratios between two integers

Page 2: seminar 2 What is a Number? - IDATDDE25/info/slides/TDDE25_Fo2_4sl.pdf · What is a Number? Seminar 2 Types of Numbers Number Systems Representation in Computers Types of Numbers

Rational Numbers

• Addition - Closed• Multiplication - Closed• Subtraction - Closed• Division - Closed

Rational numbers are dense. Between any two of them, you can always find another!

• Between 0 and 1 there an infinite number of rationals!

Contents

1. seminar 2

NZQRC

1

NumeratorDenominator

Numerator, Denominator are integersThe denominator can not be 0

Decimal Representation of Fractions

Fractions are ultimately periodic: after a certain point the infinite

sequence of digits consists of somefinite sequence of digits repeated

indefinitely!

Rational numbers are simple quantities:• They can be understood in finite terms;• Yet they can be used to represent distances as small or as large as we please

Decimal Representation

1. seminar 2 1

Contents

1. seminar 2

N

Z

Q

R

C13 = 0.333333333 . . . ,

2912 = 2.416666666 . . . ,

97 = 1.285714285714285 . . . ,

237148 = 1.60135135135 . . .

c2 = a2 + b2

c2 = a2 + b2

= 12 + 12

= 1 + 1 = 2

c2 = 2

c =p2

= 1 + 1 = 2

p2 = 1.4142135623730950488016887 . . .

⇡ = Cd

⇡ = 3.141592653589793238462643383279 . . .

e = 2.7182818284590452353602874713526 . . .1

Are the rationals adequate?

Since the rationals are dense, do we need any other quantity or can we even fit anything more on the number line?

?

What is the length of the diagonal of the unit square?

1. seminar 2 1

Contents

1. seminar 2

NZQRC13 = 0.333333333 . . . ,

2912 = 2.416666666 . . . ,

97 = 1.28571428571485 . . . ,

237148 = 1.60135135135 . . .

c2 = a2 + b2

c2 = a2 + b2

= 12 + 12

= 1 + 1 = 2

1

1. seminar 2 1

Contents

1. seminar 2

NZQRC13 = 0.333333333 . . . ,

2912 = 2.416666666 . . . ,

97 = 1.28571428571485 . . . ,

237148 = 1.60135135135 . . .

c2 = a2 + b2

c2 = a2 + b2

= 12 + 12

= 1 + 1 = 2

c2 = 2

c =p2

= 1 + 1 = 2

1

Can not be expressed asthe ratio of two integers!

It is an irrational number!

Real Numbers

Real Numbers = Rational Numbers + Irrational Numbers

An irrational number is any number that can not be expressed as a ratio a/b where a and b are integers and b is not equal to 0

1. seminar 2 1

Contents

1. seminar 2

NZQRC13 = 0.333333333 . . . ,

2912 = 2.416666666 . . . ,

97 = 1.28571428571485 . . . ,

237148 = 1.60135135135 . . .

c2 = a2 + b2

c2 = a2 + b2

= 12 + 12

= 1 + 1 = 2

c2 = 2

c =p2

= 1 + 1 = 2

p2 = 1.4142135623730950488016887 . . .

1

In decimal notation an irrational number is any number that can not be represented as terminating or or eventually repeating decimals. (Not Ultimately Periodic)

In decimal notation a rational number is any number that can be represented as terminating or eventually repeating decimals. (Ultimately Periodic)

Contents

1. seminar 2

NZQRC

1

Page 3: seminar 2 What is a Number? - IDATDDE25/info/slides/TDDE25_Fo2_4sl.pdf · What is a Number? Seminar 2 Types of Numbers Number Systems Representation in Computers Types of Numbers

Real NumbersSome important irrational numbers in engineering!

1. seminar 2 1

Contents

1. seminar 2

NZQRC13 = 0.333333333 . . . ,

2912 = 2.416666666 . . . ,

97 = 1.28571428571485 . . . ,

237148 = 1.60135135135 . . .

c2 = a2 + b2

c2 = a2 + b2

= 12 + 12

= 1 + 1 = 2

c2 = 2

c =p2

= 1 + 1 = 2

p2 = 1.4142135623730950488016887 . . .

⇡ = Cd

⇡ = 3.141592653589793238462643383279 . . .

1

1. seminar 2 1

Contents

1. seminar 2

NZQRC13 = 0.333333333 . . . ,

2912 = 2.416666666 . . . ,

97 = 1.28571428571485 . . . ,

237148 = 1.60135135135 . . .

c2 = a2 + b2

c2 = a2 + b2

= 12 + 12

= 1 + 1 = 2

c2 = 2

c =p2

= 1 + 1 = 2

p2 = 1.4142135623730950488016887 . . .

⇡ = Cd

⇡ = 3.141592653589793238462643383279 . . .

1

1. seminar 2 1

Contents

1. seminar 2

NZQRC13 = 0.333333333 . . . ,

2912 = 2.416666666 . . . ,

97 = 1.28571428571485 . . . ,

237148 = 1.60135135135 . . .

c2 = a2 + b2

c2 = a2 + b2

= 12 + 12

= 1 + 1 = 2

c2 = 2

c =p2

= 1 + 1 = 2

p2 = 1.4142135623730950488016887 . . .

⇡ = Cd

⇡ = 3.141592653589793238462643383279 . . .

e = 2.7182818284590452353602874713526 . . .

1

2

e = 1 + 11 +

11⇥2 +

11⇥2⇥3 +

11⇥2⇥3⇥4 + · · ·

e = limn!1�1 + 1

n

�n

eln(x) = x ln(ex) = x Euler’s number

2

e = 1 + 11 +

11⇥2 +

11⇥2⇥3 +

11⇥2⇥3⇥4 + · · ·

2

e = 1 + 11 +

11⇥2 +

11⇥2⇥3 +

11⇥2⇥3⇥4 + · · ·

e = limn!1�1 + 1

n

�nInfinite Processes!

Since they are important numbers, we will want to represent them in a computer!

Countability

Contents

1. seminar 2

NZQRC

1

Contents

1. seminar 2

NZQRC

1

Dense

Continuous

2

e = 1 + 11 +

11⇥2 +

11⇥2⇥3 +

11⇥2⇥3⇥4 + · · ·

e = limn!1�1 + 1

n

�n

eln(x) = x ln(ex) = x

1

2

e = 1 + 11 +

11⇥2 +

11⇥2⇥3 +

11⇥2⇥3⇥4 + · · ·

e = limn!1�1 + 1

n

�n

eln(x) = x ln(ex) = x

1

N ⇢ Q ⇢ RAre some infinite sets bigger than others?

Contents

1. seminar 2

NZQRC

1

Contents

1. seminar 2

NZQRC

1

Countable Uncountable

Continuum Hypothesis (Cantor) - There is no set whose cardinality is strictly between that of the (positive) integers and that of the real numbers.

Number SystemsThe base of a system specifies the number of digits used

Numbers are written and manipulated using positional notation.

Base-10: Decimal number system: Digits 0-9

Base-2: Binary number system: Digits 0-1Base-8: Octal number system: Digits 0-7Base-16: Hexadecimal number system: Digits 0-9, Letters A-F

Number Systems

2

e = 1 + 11 +

11⇥2 +

11⇥2⇥3 +

11⇥2⇥3⇥4 + · · ·

e = limn!1�1 + 1

n

�n

eln(x) = x ln(ex) = x

1

N ⇢ Q ⇢ R7365 = 7⇥ 103 + 3⇥ 102 + 6⇥ 101 + 5⇥ 100

dn ⇥ Bn�1 + dn�1 ⇥ Bn�2 + · · ·+ d2 ⇥ B1 + d1Lets generalize from base 10 to any base:

2

e = 1 + 11 +

11⇥2 +

11⇥2⇥3 +

11⇥2⇥3⇥4 + · · ·

e = limn!1�1 + 1

n

�n

eln(x) = x ln(ex) = x

1

N ⇢ Q ⇢ R7365 = 7⇥ 103 + 3⇥ 102 + 6⇥ 101 + 5⇥ 100

dn ⇥ Bn�1 + dn�1 ⇥ Bn�2 + · · ·+ d2 ⇥ B1 + d1

2

e = 1 + 11 +

11⇥2 +

11⇥2⇥3 +

11⇥2⇥3⇥4 + · · ·

e = limn!1�1 + 1

n

�n

eln(x) = x ln(ex) = x

1

N ⇢ Q ⇢ R7365 = 7⇥ 103 + 3⇥ 102 + 6⇥ 101 + 5⇥ 100

dn ⇥ Bn�1 + dn�1 ⇥ Bn�2 + · · ·+ d2 ⇥ B1 + d1

If a number in the base-B number system has n digits, it is representedas the following polynomial, where di represents the digit inthe i-th position

1101 = 1⇥ 23 + 1⇥ 22 + 0⇥ 21 + 1

dn ⇥ 2n�1 + dn�1 ⇥ 2n�2 + · · ·+ d2 ⇥ 21 + d1

dn ⇥ 10n�1 + dn�1 ⇥ 10n�2 + · · ·+ d2 ⇥ 101 + d1

dn ⇥ 16n�1 + dn�1 ⇥ 16n�2 + · · ·+ d2 ⇥ 161 + d1

B2U(0001) = 0⇥ 23 + 0⇥ 22 ++0⇥ 21 + 1 = 0 + 0 + 0 + 1 = 1

B2U(0101) = 0⇥ 23 + 1⇥ 22 ++0⇥ 21 + 1 = 0 + 4 + 0 + 1 = 5

B2U(1011) = 1⇥ 23 + 0⇥ 22 ++1⇥ 21 + 1 = 8 + 0 + 2 + 1 = 11

B2U(1111) = 1⇥ 23 + 1⇥ 22 ++1⇥ 21 + 1 = 8 + 4 + 2 + 1 = 15

B2T(0001) = �0⇥ 23 + 0⇥ 22 ++0⇥ 21 + 1 = 0 + 0 + 0 + 1 = 1

B2T(0101) = 0⇥ 23 + 1⇥ 22 ++0⇥ 21 + 1 = 0 + 4 + 0 + 1 = 5

B2T(1011) = �1⇥ 23 + 0⇥ 22 ++1⇥ 21 + 1 = �8+ 0 + 2 + 1 = �5

B2T(1111) = �1⇥ 23 + 1⇥ 22 ++1⇥ 21 + 1 = �8+ 4 + 2 + 1 = �1

Page 4: seminar 2 What is a Number? - IDATDDE25/info/slides/TDDE25_Fo2_4sl.pdf · What is a Number? Seminar 2 Types of Numbers Number Systems Representation in Computers Types of Numbers

Binary Number System

2

e = 1 + 11 +

11⇥2 +

11⇥2⇥3 +

11⇥2⇥3⇥4 + · · ·

e = limn!1�1 + 1

n

�n

eln(x) = x ln(ex) = x

1

N ⇢ Q ⇢ R7365 = 7⇥ 103 + 3⇥ 102 + 6⇥ 101 + 5⇥ 100

dn ⇥ Bn�1 + dn�1 ⇥ Bn�2 + · · ·+ d2 ⇥ B1 + d1

Decimal: B=10:

2

e = 1 + 11 +

11⇥2 +

11⇥2⇥3 +

11⇥2⇥3⇥4 + · · ·

e = limn!1�1 + 1

n

�n

eln(x) = x ln(ex) = x

1

N ⇢ Q ⇢ R7365 = 7⇥ 103 + 3⇥ 102 + 6⇥ 101 + 5⇥ 100

dn ⇥ Bn�1 + dn�1 ⇥ Bn�2 + · · ·+ d2 ⇥ B1 + d1

If the base-B number system has n digits, it is representedas the following polynomial, where di represents the digit inthe i-th position

1101 = 1⇥ 23 + 1⇥ 22 + 0⇥ 21 + 1

dn ⇥ 2n�1 + dn�1 ⇥ 2n�2 + · · ·+ d2 ⇥ 21 + d1

dn ⇥ 10n�1 + dn�1 ⇥ 10n�2 + · · ·+ d2 ⇥ 101 + d1

2

e = 1 + 11 +

11⇥2 +

11⇥2⇥3 +

11⇥2⇥3⇥4 + · · ·

e = limn!1�1 + 1

n

�n

eln(x) = x ln(ex) = x

1

N ⇢ Q ⇢ R7365 = 7⇥ 103 + 3⇥ 102 + 6⇥ 101 + 5⇥ 100

dn ⇥ Bn�1 + dn�1 ⇥ Bn�2 + · · ·+ d2 ⇥ B1 + d1

Binary: B=2:

2

e = 1 + 11 +

11⇥2 +

11⇥2⇥3 +

11⇥2⇥3⇥4 + · · ·

e = limn!1�1 + 1

n

�n

eln(x) = x ln(ex) = x

1

N ⇢ Q ⇢ R7365 = 7⇥ 103 + 3⇥ 102 + 6⇥ 101 + 5⇥ 100

dn ⇥ Bn�1 + dn�1 ⇥ Bn�2 + · · ·+ d2 ⇥ B1 + d1

If the base-B number system has n digits, it is representedas the following polynomial, where di represents the digit inthe i-th position

1101 = 1⇥ 23 + 1⇥ 22 + 0⇥ 21 + 1

2

e = 1 + 11 +

11⇥2 +

11⇥2⇥3 +

11⇥2⇥3⇥4 + · · ·

e = limn!1�1 + 1

n

�n

eln(x) = x ln(ex) = x

1

N ⇢ Q ⇢ R7365 = 7⇥ 103 + 3⇥ 102 + 6⇥ 101 + 5⇥ 100

dn ⇥ Bn�1 + dn�1 ⇥ Bn�2 + · · ·+ d2 ⇥ B1 + d1

If the base-B number system has n digits, it is representedas the following polynomial, where di represents the digit inthe i-th position

1101 = 1⇥ 23 + 1⇥ 22 + 0⇥ 21 + 1

dn ⇥ 2n�1 + dn�1 ⇥ 2n�2 + · · ·+ d2 ⇥ 21 + d1

From Binary to Decimal

2

e = 1 + 11 +

11⇥2 +

11⇥2⇥3 +

11⇥2⇥3⇥4 + · · ·

e = limn!1�1 + 1

n

�n

eln(x) = x ln(ex) = x

1

N ⇢ Q ⇢ R7365 = 7⇥ 103 + 3⇥ 102 + 6⇥ 101 + 5⇥ 100

dn ⇥ Bn�1 + dn�1 ⇥ Bn�2 + · · ·+ d2 ⇥ B1 + d1

If the base-B number system has n digits, it is representedas the following polynomial, where di represents the digit inthe i-th position

1101 = 1⇥ 23 + 1⇥ 22 + 0⇥ 21 + 1

dn ⇥ 2n�1 + dn�1 ⇥ 2n�2 + · · ·+ d2 ⇥ 21 + d1

How about from Decimal to Binary?

Binary Representation of a Positive Integer1. Divide the value by 2 and record the remainder.2. As long as the quotient obtained is not 0, continue to divide the newest quotient by two and recordthe remainder3. Now that the quotient of 0 has been obtained, the binary representation of the original value consistsof the remainders listed from right to left in the order they were recorded.

Hexadecimal system

2

e = 1 + 11 +

11⇥2 +

11⇥2⇥3 +

11⇥2⇥3⇥4 + · · ·

e = limn!1�1 + 1

n

�n

eln(x) = x ln(ex) = x

1

N ⇢ Q ⇢ R7365 = 7⇥ 103 + 3⇥ 102 + 6⇥ 101 + 5⇥ 100

dn ⇥ Bn�1 + dn�1 ⇥ Bn�2 + · · ·+ d2 ⇥ B1 + d1

B=16:

2

e = 1 + 11 +

11⇥2 +

11⇥2⇥3 +

11⇥2⇥3⇥4 + · · ·

e = limn!1�1 + 1

n

�n

eln(x) = x ln(ex) = x

1

N ⇢ Q ⇢ R7365 = 7⇥ 103 + 3⇥ 102 + 6⇥ 101 + 5⇥ 100

dn ⇥ Bn�1 + dn�1 ⇥ Bn�2 + · · ·+ d2 ⇥ B1 + d1

If the base-B number system has n digits, it is representedas the following polynomial, where di represents the digit inthe i-th position

1101 = 1⇥ 23 + 1⇥ 22 + 0⇥ 21 + 1

dn ⇥ 2n�1 + dn�1 ⇥ 2n�2 + · · ·+ d2 ⇥ 21 + d1

dn ⇥ 10n�1 + dn�1 ⇥ 10n�2 + · · ·+ d2 ⇥ 101 + d1

dn ⇥ 16n�1 + dn�1 ⇥ 16n�2 + · · ·+ d2 ⇥ 161 + d1

0 0 0000 1 1 0001 2 2 0010 3 3 0011 4 4 0100 5 5 0101 6 6 0110 7 7 0111 8 8 1000 9 9 1001 A 10 1010 B 11 1011 C 12 1100 D 13 1101 E 14 1110 F 15 1111

Hex!Decimal!

Binary!• Since there are only 10 decimal digits but the

base is 16, we need additional digits: A,B,C,D,E,F• Hexadecimal is shorthand notation for longpatterns of bits.• Each group of 4 bits can be represented by a single symbol.• 8 bits: 2 digits, 16 bits: 4 digits, 32 bits, 8 digits

10100011 becomes A3A 3

Page 5: seminar 2 What is a Number? - IDATDDE25/info/slides/TDDE25_Fo2_4sl.pdf · What is a Number? Seminar 2 Types of Numbers Number Systems Representation in Computers Types of Numbers

Some Examples

What is 11000011 in hexadecimal notation?

What is 11111101 in hexadecimal notation?

C 3

F D

What is E6 in decimal notation?

1110 0110E*161 + 6*160 = 23014*161 + 6*160 = 230

Information Storage & Processing

Goal: To understand how all modern computing systemsand computation using such systems are based on binary numbers and

operations on them!

Computers execute binary computations. Any information we process is ultimately encoded and stored as binary numbers!

Images Video SoundSymbolsText

Any process on data is ultimately encoded and stored as binary numbers!

BITS - Binary DigitsInformation in a computer is encoded as patterns of 1’s and 0’s.

Information = Bits + Context

The context provides an interpretation of the bit patterns!The same bit pattern can mean different things.

For instance, a numerical value, character or a program instruction!

Logical Computations

1= true0= false

And, OrNot

Context: logic

Numerical Computations

Addition, SubtractionMultiplication, Division

10

Context: Numbers

Bits and Bytes

Byte = 8 bitsBinary 000000002 to 111111112Decimal: 010 to 25510Hexadecimal 0016 to FF16

Most computers use blocks of 8 bits, or bytes, as the smallest addressable unit of memory

0 0 0000 1 1 0001 2 2 0010 3 3 0011 4 4 0100 5 5 0101 6 6 0110 7 7 0111 8 8 1000 9 9 1001 A 10 1010 B 11 1011 C 12 1100 D 13 1101 E 14 1110 F 15 1111

Hex!Decimal!

Binary!

Page 6: seminar 2 What is a Number? - IDATDDE25/info/slides/TDDE25_Fo2_4sl.pdf · What is a Number? Seminar 2 Types of Numbers Number Systems Representation in Computers Types of Numbers

Bytes and Words!  Machine(Has(“Word(Size”(!  Nominal(size(of(integer0valued(data(

!  Including(addresses(

!  Most(current(machines(use(32(bits((4(bytes)(words(

!  Limits(addresses(to(4GB(

!  Becoming(too(small(for(memory0intensive(applicaDons(

!  High0end(systems(use(64(bits((8(bytes)(words(

!  PotenDal(address(space(≈(1.8(X(1019(bytes(

!  x86064(machines(support(480bit(addresses:(256(Terabytes(

!  Machines(support(mulDple(data(formats(

!  FracDons(or(mulDples(of(word(size(

!  Always(integral(number(of(bytes(

(10004)

Metric Prefixes

What is a Googol?

2

e = 1 + 11 +

11⇥2 +

11⇥2⇥3 +

11⇥2⇥3⇥4 + · · ·

e = limn!1�1 + 1

n

�n

eln(x) = x ln(ex) = x

1

N ⇢ Q ⇢ R7365 = 7⇥ 103 + 3⇥ 102 + 6⇥ 101 + 5⇥ 100

dn ⇥ Bn�1 + dn�1 ⇥ Bn�2 + · · ·+ d2 ⇥ B1 + d1

If a number in the base-B number system has n digits, it is representedas the following polynomial, where di represents the digit inthe i-th position

1101 = 1⇥ 23 + 1⇥ 22 + 0⇥ 21 + 1

dn ⇥ 2n�1 + dn�1 ⇥ 2n�2 + · · ·+ d2 ⇥ 21 + d1

dn ⇥ 10n�1 + dn�1 ⇥ 10n�2 + · · ·+ d2 ⇥ 101 + d1

dn ⇥ 16n�1 + dn�1 ⇥ 16n�2 + · · ·+ d2 ⇥ 161 + d1

B2U(0001) = 0⇥ 23 + 0⇥ 22 ++0⇥ 21 + 1 = 0 + 0 + 0 + 1 = 1

B2U(0101) = 0⇥ 23 + 1⇥ 22 ++0⇥ 21 + 1 = 0 + 4 + 0 + 1 = 5

B2U(1011) = 1⇥ 23 + 0⇥ 22 ++1⇥ 21 + 1 = 8 + 0 + 2 + 1 = 11

B2U(1111) = 1⇥ 23 + 1⇥ 22 ++1⇥ 21 + 1 = 8 + 4 + 2 + 1 = 15

B2T(0001) = �0⇥ 23 + 0⇥ 22 ++0⇥ 21 + 1 = 0 + 0 + 0 + 1 = 1

B2T(0101) = 0⇥ 23 + 1⇥ 22 ++0⇥ 21 + 1 = 0 + 4 + 0 + 1 = 5

B2T(1011) = �1⇥ 23 + 0⇥ 22 ++1⇥ 21 + 1 = �8+ 0 + 2 + 1 = �5

B2T(1111) = �1⇥ 23 + 1⇥ 22 ++1⇥ 21 + 1 = �8+ 4 + 2 + 1 = �1

10100

1080

What is a current estimate of atoms in the

“observable” universe?

Storage: Main MemoryMemory Cell: is a unit of memory (usually a byte), organized in a bit order:

Virtual Memory: Programs refer to virtual addresses • Conceptually, it is a very large array of bytes. • System is actually implemented with a hierarchy of different memory types.

Address: A “name” that uniquely identifies one cell in the computer’s main memory • The names are actually numbers. • These numbers are assigned consecutively starting at zero. • Numbering the cells in this manner associates an order with the memory cells

• • •!00•••0

FF•••F

Byte Oriented Memory Organization

Word Oriented Memory Organization

!  Addresses&Specify&Byte&Loca2ons&!  Address&of&first&byte&in&word&!  Addresses&of&successive&words&differ&

by&4&(328bit)&or&8&(648bit)&

0000 0001 0002 0003 0004 0005 0006 0007 0008 0009 0010 0011

32-bit!Words! Bytes! Addr.!

0012 0013 0014 0015

64-bit!Words!

Addr !=!??

Addr !=!??

Addr !=!??

Addr !=!??

Addr !=!??

Addr !=!??

0000

0004

0008

0012

0000

0008

Number of Bytes

Page 7: seminar 2 What is a Number? - IDATDDE25/info/slides/TDDE25_Fo2_4sl.pdf · What is a Number? Seminar 2 Types of Numbers Number Systems Representation in Computers Types of Numbers

Measuring Memory Capacity

•  Kilobyte: 210 bytes = 1024 bytes – Example: 3 KB = 3 times1024 bytes

•  Megabyte: 220 bytes = 1,048,576 bytes – Example: 3 MB = 3 times 1,048,576 bytes

•  Gigabyte: 230 bytes = 1,073,741,824 bytes – Example: 3 GB = 3 times 1,073,741,824 bytes

My MacBook Pro: 16GB of main memory

750GB of secondary memory (flash disk)

Representing Integers in Computers

B2U(X ) = xi ⋅2i

i=0

w−1

[0001]![0101]![1011]![1111]!

0! 1! 2! 3! 4! 5! 6! 7! 8! 9! 10! 11!12!13!14!15!16!

23 = 8!22 = 4!21 = 2!20 = 1!

Unsigned Integers

BinaryToUnsigned

2

e = 1 + 11 +

11⇥2 +

11⇥2⇥3 +

11⇥2⇥3⇥4 + · · ·

e = limn!1�1 + 1

n

�n

eln(x) = x ln(ex) = x

1

N ⇢ Q ⇢ R7365 = 7⇥ 103 + 3⇥ 102 + 6⇥ 101 + 5⇥ 100

dn ⇥ Bn�1 + dn�1 ⇥ Bn�2 + · · ·+ d2 ⇥ B1 + d1

If the base-B number system has n digits, it is representedas the following polynomial, where di represents the digit inthe i-th position

1101 = 1⇥ 23 + 1⇥ 22 + 0⇥ 21 + 1

dn ⇥ 2n�1 + dn�1 ⇥ 2n�2 + · · ·+ d2 ⇥ 21 + d1

dn ⇥ 10n�1 + dn�1 ⇥ 10n�2 + · · ·+ d2 ⇥ 101 + d1

dn ⇥ 16n�1 + dn�1 ⇥ 16n�2 + · · ·+ d2 ⇥ 161 + d1

B2U(0001) = 0⇥ 23 + 0⇥ 22 ++0⇥ 21 + 1 = 0 + 0 + 0 + 1 = 1

B2U(0101) = 0⇥ 23 + 1⇥ 22 ++0⇥ 21 + 1 = 0 + 4 + 0 + 1 = 5

B2U(1011) = 1⇥ 23 + 0⇥ 22 ++1⇥ 21 + 1 = 8 + 0 + 2 + 1 = 11

B2U(1111) = 1⇥ 23 + 1⇥ 22 ++1⇥ 21 + 1 = 8 + 4 + 2 + 1 = 15

w= # of bits

UMin = 0000…0

UMax = 2w – 1111…1

Numeric Rangew=4

Representing Integers

Decimal: !15213 Binary: 0011 1011 0110 1101 Hex: 3 B 6 D

long int C = 15213;

00 00 00 00

6D 3B 00 00

x86-64!

3B 6D

00 00

Sun!

6D 3B 00 00

IA32!

0x100 0x101 0x102 0x103

01 23 45 67

0x100 0x101 0x102 0x103

67 45 23 01

Big Endian!

Little Endian!

01 23 45 67

67 45 23 01

!  Big$Endian$!  Least&significant&byte&has&highest&address&

!  Li*le$Endian$!  Least&significant&byte&has&lowest&address&

!  Example$!  Variable&x&has&47byte&representa9on&0x01234567&!  Address&given&by&&x&is&0x100&

BigEndian

LittleEndian

ByteByte

Byte

Byte

Representing Signed Integers in Computers

X" B2T(X)&B2U(X)&0000" 0"0001" 1"0010" 2"0011" 3"0100" 4"0101" 5"0110" 6"0111" 7"

–8"8"–7"9"–6"10"–5"11"–4"12"–3"13"–2"14"–1"15"

1000"1001"1010"1011"1100"1101"1110"1111"

0"1"2"3"4"5"6"7"

Given 4 bits of memory, we can represent 24 unsigned numbers

(24 - 1 if 0 is not included)

Suppose we wanted to represent both positive and negativenumbers in memory.......

We would need to find an efficient mapping betweenour binary numbers and roughly 24/2 positive numbers

and 24/2 negative numbers

Let’s do that!

Page 8: seminar 2 What is a Number? - IDATDDE25/info/slides/TDDE25_Fo2_4sl.pdf · What is a Number? Seminar 2 Types of Numbers Number Systems Representation in Computers Types of Numbers

Coding -6 in Two’s Complement Notation using 4 bits

Start with the binary representation of 6

Two’s Complement Examples

X" B2T(X)&B2U(X)&0000" 0"0001" 1"0010" 2"0011" 3"0100" 4"0101" 5"0110" 6"0111" 7"

–8"8"–7"9"–6"10"–5"11"–4"12"–3"13"–2"14"–1"15"

1000"1001"1010"1011"1100"1101"1110"1111"

0"1"2"3"4"5"6"7"

Comparisonwith Unsigned

Representing Signed Integers in Computers

Two’s Complement Encoding

B2T (X ) = −xw−1 ⋅2w−1 + xi ⋅2

i

i=0

w−2

Sign Bit

[0001]!

[0101]!

[1011]!

[1111]!

0! 1! 2! 3! 4! 5! 6! 7! 8!

– 23 = –8!22 = 4!21 = 2!20 = 1!

–8!–7!–6!–5!–4!–3!–2!–1!

2

e = 1 + 11 +

11⇥2 +

11⇥2⇥3 +

11⇥2⇥3⇥4 + · · ·

e = limn!1�1 + 1

n

�n

eln(x) = x ln(ex) = x

1

N ⇢ Q ⇢ R7365 = 7⇥ 103 + 3⇥ 102 + 6⇥ 101 + 5⇥ 100

dn ⇥ Bn�1 + dn�1 ⇥ Bn�2 + · · ·+ d2 ⇥ B1 + d1

If the base-B number system has n digits, it is representedas the following polynomial, where di represents the digit inthe i-th position

1101 = 1⇥ 23 + 1⇥ 22 + 0⇥ 21 + 1

dn ⇥ 2n�1 + dn�1 ⇥ 2n�2 + · · ·+ d2 ⇥ 21 + d1

dn ⇥ 10n�1 + dn�1 ⇥ 10n�2 + · · ·+ d2 ⇥ 101 + d1

dn ⇥ 16n�1 + dn�1 ⇥ 16n�2 + · · ·+ d2 ⇥ 161 + d1

B2U(0001) = 0⇥ 23 + 0⇥ 22 ++0⇥ 21 + 1 = 0 + 0 + 0 + 1 = 1

B2U(0101) = 0⇥ 23 + 1⇥ 22 ++0⇥ 21 + 1 = 0 + 4 + 0 + 1 = 5

B2U(1011) = 1⇥ 23 + 0⇥ 22 ++1⇥ 21 + 1 = 8 + 0 + 2 + 1 = 11

B2U(1111) = 1⇥ 23 + 1⇥ 22 ++1⇥ 21 + 1 = 8 + 4 + 2 + 1 = 15

B2T(0001) = �0⇥ 23 + 0⇥ 22 ++0⇥ 21 + 1 = 0 + 0 + 0 + 1 = 1

B2T(0101) = 0⇥ 23 + 1⇥ 22 ++0⇥ 21 + 1 = 0 + 4 + 0 + 1 = 5

B2T(1011) = �1⇥ 23 + 0⇥ 22 ++1⇥ 21 + 1 = �8+ 0 + 2 + 1 = �5

B2T(1111) = �1⇥ 23 + 1⇥ 22 ++1⇥ 21 + 1 = �8+ 4 + 2 + 1 = �1

TMin = –2w–1

100…0TMax = 2w–1 – 1

011…1

Numeric Range

1 = negative Int0 = positive Int

Half of U -1

w=4

Representing Signed Integers in Memory

6D 3B 00 00

IA32, x86-64!

3B 6D

00 00

Sun!

int A = 15213;

93 C4 FF FF

IA32, x86-64!

C4 93

FF FF

Sun!

int B = -15213;

Two’s Complement Representation

long int C = 15213;

00 00 00 00

6D 3B 00 00

x86-64!

3B 6D

00 00

Sun!

6D 3B 00 00

IA32!

Decimal: !15213 Binary: 0011 1011 0110 1101 Hex: 3 B 6 D

Decimal: !-15213 Binary: 1111 1111 1111 1111 1100 0100 1001 0011 Hex: F F F F C 4 9 3

LittleEndian

BigEndian

Page 9: seminar 2 What is a Number? - IDATDDE25/info/slides/TDDE25_Fo2_4sl.pdf · What is a Number? Seminar 2 Types of Numbers Number Systems Representation in Computers Types of Numbers

Addition/Subtraction converted to Two’s Complement Notation

For addition:•Same algorithm as forbinary addition.• any extra carry bit generated on the left is truncated.• addition of any combination of signed integers uses the same algorithm and circuitry!

The Payoff!

For subtraction:• negate the number subtracted and then add both together.• subtraction of any combination of signed integers uses the same algorithm and circuitry for addition plus an additional circuit for negation of an integer!Example: 7 - 5

Numeric Ranges

Unsigned ValuesUMin= 0

000…0UMax = 2w – 1

111…1

! W"! 8" 16" 32" 64"

UMax" 255! 65,535! 4,294,967,295! 18,446,744,073,709,551,615!TMax" 127! 32,767! 2,147,483,647! 9,223,372,036,854,775,807!TMin" -128! -32,768! -2,147,483,648! -9,223,372,036,854,775,808!

!!

Two’s Complement ValuesTMin = –2w–1

100…0TMax = 2w–1 – 1

011…1

For Different Word Sizes

[Bits]

Representing Signed Integers in Excess Notation

• Select pattern length to be used

4

• Write down all different patternsin the order they would appear if counting from bottom up.• Pick the 1st pattern with 1 as most significant bit to represent 0

• Patterns preceding 0 are used for -1, -2, -3 ...• Patterns proceeding 0 are used for 1, 2, 3, ...

If the pattern length is x, the difference between the bit pattern value and the value represented is 2x-1

x=4, excess-8 notation, x= 5, excess-16 notation

Representing Fractions

Radix Point2i#

2i$1#

4#2#1#

1/2#1/4#1/8#

2$j#

bi# bi$1# •••# b2# b1# b0# b$1# b$2# b$3# •••# b$j#

• • •!

! Representa6on#!  Bits#to#right#of#“binary#point”#represent#frac6onal#powers#of#2#!  Represents#ra6onal#number:#

• • •!

Page 10: seminar 2 What is a Number? - IDATDDE25/info/slides/TDDE25_Fo2_4sl.pdf · What is a Number? Seminar 2 Types of Numbers Number Systems Representation in Computers Types of Numbers

Representing Fractions

Radix Point

Shifting the binary point 1 position to the lefthas the effect of dividing the number by 2.

Shifting the binary point 1 position to the righthas the effect of multiplying the number by 2.

101.11 = 5 3/4

10.111 = 2 7/81011.1 =11 1/2

Shifting the decimal point 1 position to the lefthas the effect of dividing the number by 10.

Shifting the binary point 1 position to the righthas the effect of multiplying the number by 10.

145.238

14.5238 1452.38

decimal

Representable Numbers

!  Limita&on)!  Can)only)exactly)represent)numbers)of)the)form)x/2k)!  Other)ra&onal)numbers)have)repea&ng)bit)representa&ons)

!  Value )Representa&on)!  1/3 )0.0101010101[01]…2!  1/5 )0.001100110011[0011]…2!  1/10 )0.0001100110011[0011]…2

Assuming finite length encodings:

Can approximate with increasing accuracy by lengthening the

binary representation

So, we must eventually truncate!

Floating Point RepresentationRepresenting a real value in a computer:• The sign - positive or negative• The mantissa - consists of digits in the value with the radix point assumed to the left• The exponent - which determines how the radix point is shifted relative to the mantissa (can be positive or negative)

The representation is called floating point because the number of digits is fixed but the radix point floats.• a positive exponent shifts the radix point to the right• a negative exponent shifts the radix point to the left

Let’s Assume an 8-bit fixed representation

(usually much larger 64 bits):• sign: 1 bit, • exponent: 11 bits, • mantissa: 52 bits

Decoding the value 01101011

0 01 1 0 1 11

Exponent encoded in excess-4

01 1 1. Extract Mantissa

1 1 0 Extract ExponentInterpret in excess-42

01 1 1. Move radix pointto the right 2

2+0+1/2+1/4 = 2 3/4Translate todecimal form

Check sign bit0 is positive1 is negative

+ 2 3/4

2.75

Page 11: seminar 2 What is a Number? - IDATDDE25/info/slides/TDDE25_Fo2_4sl.pdf · What is a Number? Seminar 2 Types of Numbers Number Systems Representation in Computers Types of Numbers

Decoding the value 00111100

0 10 1 1 1 0 0

Exponent encoded in excess-4

11 0 0. Extract Mantissa

0 1 1 Extract ExponentInterpret in excess-4-1

0/2+1/4+1/8 = 3/8 Translate todecimal form

Check sign bit0 is positive1 is negative

+ 3/8

10 0 01 Move radix pointto the left 1.

0.375

Encoding the value 1 1/8 (1.125)

Exponent encoded in excess-4

0. 0 11 Encode the number in binary notation

0

0Encode sign bit

0 is positive1 is negative

Copy the patterninto the mantissa

(left to right)Starting with leftmost 1

01 0 1

01 0 1

01 0 1. Imagine the radix point to the left of the mantissa value

Determine direction and length to move radix point to recover the

original binary number

1 to the right(+1)

1 0 1

Encode in exponentfield using excess notation1 0 1

Truncation Errors: Encoding the value 2 5/8 (2.625)

Exponent encoded in excess-4

0

0Encode sign bit

0 is positive1 is negative

01 1 0. Imagine the radix point to the left of the mantissa value

Determine direction and length to move radix point to recover the

original binary number

2 to the right(+1)

1 1 0

Encode in exponentfield using excess notation1 1 0

1. 0 10 Encode the number in binary notation1

2+1/2+1/8=2 5/8

Copy the patterninto the mantissa

(left to right)Starting with leftmost 1

01 1 0

01 1 0

Must truncate therightmost 1 (1/8)

Result:01101010 = 2 1/2

Truncation orRounding error!

Truncation Errors

Can cause significant problems in applications requiring high precision!

There are many more repeating decimal numbers in binary than in decimal notation. Example: 1/10

Arithmetic with accumulated rounding errors can be problematic:8 bit representation:

2 1/2 + 1/8 + 1/8 is not the same as 1/8 + 1/8 + 2 1/2 !!

Topic: Numerical Analysis

Page 12: seminar 2 What is a Number? - IDATDDE25/info/slides/TDDE25_Fo2_4sl.pdf · What is a Number? Seminar 2 Types of Numbers Number Systems Representation in Computers Types of Numbers

Worrying about Precisions

!  Single'precision:'32'bits'

!  Double'precision:'64'bits'

!  Extended'precision:'80'bits'(Intel'only)'

sexp frac1 8-bits 23-bits

sexp frac1 11-bits 52-bits

sexp frac1 15-bits 63 or 64-bits

10^-37 to 10^38

Precision: 7 decimal digits

Single Precision•Encodes very small real numbers and very large real numbers• very accurate precision to 7 decimals...

IEEE Floating Point Standard 754

!  IEEE#Standard#754#!  Established#in#1985#as#uniform#standard#for#floa;ng#point#arithme;c#

!  Before#that,#many#idiosyncra;c#formats#!  Supported#by#all#major#CPUs#

!  Driven#by#numerical#concerns#!  Nice#standards#for#rounding,#overflow,#underflow#!  Hard#to#make#fast#in#hardware#

!  Numerical#analysts#predominated#over#hardware#designers#in#defining#standard#

The High Cost of Floating Point Overflow!

Sadly, the primary cause was found to be a piece of software which had been retained from the previous launcher’s systems and which was not required during the flight of Ariane 5. The software was used in the Inertial Reference System (SRI) to calculate the attitude of the launcher. In Ariane 4, this software was allowed to continue functioning during the first 50 seconds of flight as it could otherwise delay launching if the countdown was halted for any other reason, this was not necessary for Ariane 5. Additionly, the software contained implicit assumptions about the parameters, in particular the horizontal velocity that was safe for Ariane 4, but not Ariane 5.

The failure occurred because the horizontal velocity exceeded the maximum value for a 16 bit unsigned integer when it was converted from it's signed 64 bit representation. This failure generated an exception in the code which was not caught and thus propagated up through the processor and ultimately caused the SRI to fail. The failure triggered the automatic fail-over to the backup SRI which had already failed for the same reason. This combined failure was then communicated to the main computer responsible for controlling the jets of the rocket, however, this information was misinterpreted as valid commands. As a result of the invalid commands, the engine nozzles were swung to an extreme position and the launcher was destroyed shortly afterwards.

Ariane 5 was designed by the European Space Agency (ESA) as a replacement for the successful Ariane 4 launcher. The intention was to create a reliable, high capacity, launch vehicle for ESA that could be used to support their contribution to the International Space Station as well as a range of other commercial and scientific launches.

On June 4, 1996, the US$500 million space craft was launched for the first time

Where are We?

Bits, Bytes, Words, RepresentationsInformation

Arithmetical & Logical ComputationsComputation

Abstraction

Information Theory

Discrete Mathematics

Page 13: seminar 2 What is a Number? - IDATDDE25/info/slides/TDDE25_Fo2_4sl.pdf · What is a Number? Seminar 2 Types of Numbers Number Systems Representation in Computers Types of Numbers

Additional Slides if extra time

Storing Text/SymbolsASCII

American Standard Code for Information Interchange

ASCII Character SetOriginally used 7 bits/character +

1 bit used as a check bit

Extended Ascii Character Set

Extended ASCII Character Set

Uses 8 bits/character256 characters

Additional accented and other

special symbols

ASCII Character

Set

+

Enough for englishbut not for other

languages!

Unicode Character Set

Goal: To represent every character in every languageon the planet, including ideograms, archaic languages,

all symbols, etc.

• Includes extended ASCII with same encoding• Generally uses 16 bits per character (65536 chars)• Used by most modern programming languages today

Page 14: seminar 2 What is a Number? - IDATDDE25/info/slides/TDDE25_Fo2_4sl.pdf · What is a Number? Seminar 2 Types of Numbers Number Systems Representation in Computers Types of Numbers

Representing Strings

!  Strings(in(C(!  Represented)by)array)of)characters)!  Each)character)encoded)in)ASCII)format)

!  Standard)79bit)encoding)of)character)set)!  Character)“0”)has)code)0x30)

–  Digit)i))has)code)0x30+i)!  String)should)be)null9terminated)

!  Final)character)=)0)

!  Compa.bility(!  Byte)ordering)not)an)issue)

char S[6] = "18243";

Linux/Alpha! Sun!

313832343300

313832343300

Hex

Storing Images• A photograph is an analogue representation of an image

• it is continuous across its surface and colors blend into each other.

• Digitizing an image involves representing it as a collection of individual dots called pixels (picture elements)

• each pixel is composed of a single color

• the number of pixels used in an image is called its resolution

• if the resolution is high enough the human eye is tricked into thinking it is viewing a continuous picture.

• Apple: New retina displays

• Two types of formats

• Raster graphics format - pixel by pixel representation. (BMP, GIF, JPEG, PNG)

• Vector graphics format - describe in terms of lines and geometric shapes (FLASH, SVG)

Storing Images - Raster Graphics

A Digital Image is a 2D array (matrix) of numbers

• Intensity Images: Photographic like images encoding light intensities. Sensors: cameras, infrared• Range Images: Shape like images encoding shape and distance. Sensors: sonar, laser

Different resolutions

Pixel - Picture Element Bitmap - Collection of encoded pixels

3x3 bitmap using grayscale encoding

1 byte/pixel

Color Images- Raster Graphics FormatRGB Encoding

Each pixel is encoded using 3 bytes, each measuring the intensity of the three primary colors, red, green and blue

1000x1000 pixel image = 1MegaPixel Image3x1000 x 1000 = 3 MegaByte= 24MegaBit!

Lots of Data!!!

Page 15: seminar 2 What is a Number? - IDATDDE25/info/slides/TDDE25_Fo2_4sl.pdf · What is a Number? Seminar 2 Types of Numbers Number Systems Representation in Computers Types of Numbers

Vector Graphics Format

• An image is described in terms of lines and geometric shapes

• A vector graphic is a series of commands that describe a line’s direction, thickness, and color

• Files sizes tend to be small in this format

• Each pixel does not have to be described

• Size is dependent on the number of items in the image

• Advantages

• Raster graphic has to be encoded multiple times to account for different sizes and proportions

• Vector graphics can be resized mathematically with changes calculated dynamically if needed.

• Disadvantages

• Vector graphics is not good for representing real-world images

Storing Sound

• Sound is perceived when a series of air compressions vibrate a membrane in our ear which then sends signals to the brain

• Sound is defined in nature as an air wave that interacts with the brain

• Stereo system

• sends an electrical signal to a speaker to produce sound

• the signal is an analog representation of the sound wave

• the speaker receives a signal and causes the membrane to vibrate, which in turn vibrates the air, which in turn vibrates the airdrum

• The trick is to recreate the original signal in this manner

• Representing audio data on a computer

• The sound wave needs to be digitized

• An analogue signal varies in voltage continuously

• To digitize it, we periodically measure the voltage and record the appropriate numerical value. (We sample the signal)

Storing SoundTelephone: 8000 samples/second

CD Quality: 44,100 samples/second• 16 bits/sample (mono)• 32 bits/sample (stereo)• For stereo: each second of music requires more than a million bits!

Signal ProcessingInformation

Theory

Storing Video• A video is a sequence of images. Most often shown at 25 frames per second

• Due to the large amount of data involved, each image must be compressed in some manner

• Codec (COmpressor/DECompressor)

• A video codec refers to the methods used to shrink the size of a movie so it can be played on a computer, streamed or transferred across the network

• Lossy/Lossless

• Lossy compression (some data can not be retrieved)

• Lossless compression (original data can be retrieved)

• Two types of compression

• Temporal compression

• looks for differences between two frames and saves the changes

• Spatial compression

• removes redundant information

• blue sky: store the color and area coordinates instead of all pixels.

• Some Codecs

• Sorenson, Cinepak, Real Video, MPEG

Page 16: seminar 2 What is a Number? - IDATDDE25/info/slides/TDDE25_Fo2_4sl.pdf · What is a Number? Seminar 2 Types of Numbers Number Systems Representation in Computers Types of Numbers

Data Compression Techniques

• Run-Length Encoding

• Huffman Encoding

Where are We?

Bits, Bytes, Words, RepresentationsInformation

Arithmetical & Logical ComputationsComputation

Abstraction

Information Theory

Discrete Mathematics