slide1
Download
Skip this Video
Download Presentation
32 point font 28 point font 24 point font 20 point font 18 point font 16 point font 14 point font

Loading in 2 Seconds...

play fullscreen
1 / 184

32 point font 28 point font 24 point font 20 point font 18 point font 16 point font 14 point font - PowerPoint PPT Presentation


  • 286 Views
  • Uploaded on

32 point font 28 point font 24 point font 20 point font 18 point font 16 point font 14 point font 12 point font 11 point font 10 point font. Strange arithmetic…. Programming Fundamentals 3 Feliks Kluzniak. Executive summary : We discuss: arithmetic with a limited number of digits

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' 32 point font 28 point font 24 point font 20 point font 18 point font 16 point font 14 point font' - eddy


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
slide1

32 point font

28 point font

24 point font

20 point font

18 point font

16 point font

14 point font

12 point font

11 point font

10 point font

Strange arithmetic ...

strange arithmetic

Strange arithmetic…

Programming Fundamentals

3

Feliks Kluzniak

Strange arithmetic ...

slide3

Executive summary:

  • We discuss:
    • arithmetic with a limited number of digits
    • hexadecimal and binary arithmetic

Strange arithmetic ...

slide4

Imagine a strip of plastic with 10 digits:

0

0

1

2

3

4

5

6

7

8

9

Strange arithmetic ...

slide5

Imagine a strip of plastic with 10 digits:

0

0

1

2

3

4

5

6

7

8

9

We glue the ends together, getting a ring, with

the digits facing outwards.

Strange arithmetic ...

slide6

Imagine a strip of plastic with 10 digits:

0

0

1

2

3

4

5

6

7

8

9

We glue the ends together, getting a ring, with

the digits facing outwards. We fit a wheel into

the ring, tightly, and …

Strange arithmetic ...

slide7

… connect it to a crank, so that one turn of the crank will advance the wheel by 1/10 th.

We then put it in a box with a little window:

0

Strange arithmetic ...

slide8

… connect it to a crank, so that one turn of the crank will advance the wheel by 1/10 th.

We then put it in a box with a little window:

What will happen if I turn the crank 3 times?

0

Strange arithmetic ...

slide10

Wow, I have an adding machine!

7

Strange arithmetic ...

slide11

Wow, I have an adding machine!

What if I turn it another 4 times?

7

Strange arithmetic ...

slide12

1

Strange arithmetic ...

slide13

What we have here is arithmetic modulo 10.

The result of adding a to b is (a + b) mod 10.

(The remainder of dividing a + b by 10.)

1

Strange arithmetic ...

slide14

Let us now buy another strip, and connect it in the same way to the first wheel:

1

Strange arithmetic ...

slide15

One complete turn of the right wheel will make the left wheel turn by 1/10 th.

0

0

Strange arithmetic ...

slide16

One complete turn of the right wheel will make the left wheel turn by 1/10 th.

What will happen when I turn the crank 15 times?

0

0

Strange arithmetic ...

slide17

Nice!

What will happen if I turn it 90 more times?

(It’s good exercise.)

1

5

Strange arithmetic ...

slide18

After 90 more turns:

So now we have arithmetic modulo 100.

0

5

Strange arithmetic ...

slide21

This machine will allow us to perform addition modulo

10 000.

0]

0

0

0

0

Strange arithmetic ...

slide22

This machine will allow us to perform addition modulo

10 000.

10000 = 10**4 (i.e., 10 to the power of 4)

100 = 10**2

10 = 10**1

Do you see a pattern here? 

0]

0

0

0

0

Strange arithmetic ...

slide24

What range of positive integers can we represent with:

4 digits : 0 .. 9999

Strange arithmetic ...

slide25

What range of positive integers can we represent with:

4 digits : 0 .. 9999

2 digits: 0 .. 99

1 digit : 0 .. 9

Strange arithmetic ...

slide26

What range of positive integers can we represent with:

4 digits : 0 .. 9999 = 0 .. 10**4 – 1

2 digits: 0 .. 99 = 0 .. 10**2 – 1

1 digit : 0 .. 9 = 0 .. 10**1 – 1

NOTE:

** denotes exponentiation, i.e., “raising to the power” (a convention established by Fortran).

So, e.g., 10**3 = 10 * 10 * 10 = 1000 .

Notice that ** binds more tightly than other operators,

so 10**4 – 1 = (10**4) – 1

Strange arithmetic ...

slide27

What range of positive integers can we represent with:

4 digits : 0 .. 9999 = 0 .. 10**4 – 1

2 digits: 0 .. 99 = 0 .. 10**2 – 1

1 digit : 0 .. 9 = 0 .. 10**1 – 1

What about 0 digits?

Strange arithmetic ...

slide28

What range of positive integers can we represent with:

4 digits : 0 .. 9999 = 0 .. 10**4 – 1

2 digits: 0 .. 99 = 0 .. 10**2 – 1

1 digit : 0 .. 9 = 0 .. 10**1 – 1

What about 0 digits? 0 .. 2**0 – 1

By universal convention, raising an integer to the power of 0 yields 1 .

Strange arithmetic ...

slide29

An important lesson:

If the number of digits is limited, then the inequality

a + 1 > a

does not always hold!

Strange arithmetic ...

slide30

An important lesson:

If the number of digits is limited, then the inequality

a + 1 > a

does not always hold!

This is, of course, what we deal with in programming!

Strange arithmetic ...

slide31

An important lesson:

If the number of digits is limited, then the inequality

a + 1 > a

does not always hold!

This is, of course, what we deal with in programming!

In practice we treat arithmetic as normal, and worry about staying “in range” separately.

Strange arithmetic ...

slide32

An important lesson:

If the number of digits is limited, then the inequality

a + 1 > a

does not always hold!

This is, of course, what we deal with in programming!

In practice we treat arithmetic as normal, and worry about staying “in range” separately.

This is known as separation of concerns.

Strange arithmetic ...

slide33

Let the initial state of the machine be 0 0 0 0.

How many times would I have to turn the crank to obtain:

0 0 0 1 ?

Strange arithmetic ...

slide34

Let the initial state of the machine be 0 0 0 0.

How many times would I have to turn the crank to obtain:

0 0 0 1 ? One, i.e., 1 * (10**0) .

Strange arithmetic ...

slide35

Let the initial state of the machine be 0 0 0 0.

How many times would I have to turn the crank to obtain:

0 0 0 1 ? One, i.e., 1 * (10**0) .

0 0 3 0 ?

Strange arithmetic ...

slide36

Let the initial state of the machine be 0 0 0 0.

How many times would I have to turn the crank to obtain:

0 0 0 1 ? One, i.e., 1 * (10**0) .

0 0 3 0 ? Thirty, i.e., 3 * (10**1) .

Strange arithmetic ...

slide37

Let the initial state of the machine be 0 0 0 0.

How many times would I have to turn the crank to obtain:

0 0 0 1 ? One, i.e., 1 * (10**0) .

0 0 3 0 ? Thirty, i.e., 3 * (10**1) .

0 2 0 0 ?

Strange arithmetic ...

slide38

Let the initial state of the machine be 0 0 0 0.

How many times would I have to turn the crank to obtain:

0 0 0 1 ? One, i.e., 1 * (10**0) .

0 0 3 0 ? Thirty, i.e., 3 * (10**1) .

0 2 0 0 ? Two hundred, i.e., 2 * (10**2) .

Strange arithmetic ...

slide39

Let the initial state of the machine be 0 0 0 0.

How many times would I have to turn the crank to obtain:

0 0 0 1 ? One, i.e., 1 * (10**0) .

0 0 3 0 ? Thirty, i.e., 3 * (10**1) .

0 2 0 0 ? Two hundred, i.e., 2 * (10**2) .

So, how many times would I have to turn the crank to obtain

0 2 3 1 ?

Strange arithmetic ...

slide40

Let the initial state of the machine be 0 0 0 0.

How many times would I have to turn the crank to obtain:

0 0 0 1 ? One, i.e., 1 * (10**0) .

0 0 3 0 ? Thirty, i.e., 3 * (10**1) .

0 2 0 0 ? Two hundred, i.e., 2 * (10**2) .

So, how many times would I have to turn the crank to obtain

0 2 3 1 ?

Two hundred thirty one,

i.e., 2 * (10**2) + 3 * (10**1) + 1 * (10**0) .

Strange arithmetic ...

slide41

Let the initial state of the machine be 0 0 0 0.

How many times would I have to turn the crank to obtain:

0 0 0 1 ? One, i.e., 1 * (10**0) .

0 0 3 0 ? Thirty, i.e., 3 * (10**1) .

0 2 0 0 ? Two hundred, i.e., 2 * (10**2) .

So, how many times would I have to turn the crank to obtain

0 2 3 1 ?

Two hundred thirty one,

i.e., 2 * (10**2) + 3 * (10**1) + 1 * (10**0) .

Do you see the general pattern ?

Strange arithmetic ...

slide42

I can’t afford another box, but I can afford four longer strips:

0

0

1

2

3

4

5

6

7

8

9

A

B

C

D

E

F

Strange arithmetic ...

slide43

At zero the box looks the same, but there are 16 digits on each wheel (and the gears have been changed accordingly):

0]

0

0

0

0

Strange arithmetic ...

slide44

At zero the box looks the same, but there are 16 digits on each wheel (and the gears have been changed accordingly):

What range of integers can I represent?

0]

0

0

0

0

Strange arithmetic ...

slide45

At zero the box looks the same, but there are 16 digits on each wheel (and the gears have been changed accordingly):

What range of integers can I represent?

0 .. 16**4 – 1

0]

0

0

0

0

Strange arithmetic ...

slide46

At zero the box looks the same, but there are 16 digits on each wheel (and the gears have been changed accordingly):

What range of integers can I represent?

0 .. 16**4 – 1

0 .. 65535

0]

0

0

0

0

Strange arithmetic ...

slide47

Let the initial state of the machine be 0 0 0 0.

How many times would I have to turn the crank to obtain:

0 0 0 1 ? One, i.e., 1 * (16**0) .

Strange arithmetic ...

slide48

Let the initial state of the machine be 0 0 0 0.

How many times would I have to turn the crank to obtain:

0 0 0 1 ? One, i.e., 1 * (16**0) .

0 0 3 0 ?

Strange arithmetic ...

slide49

Let the initial state of the machine be 0 0 0 0.

How many times would I have to turn the crank to obtain:

0 0 0 1 ? One, i.e., 1 * (16**0) .

0 0 3 0 ? Forty eight, i.e., 3 * (16**1) .

Strange arithmetic ...

slide50

Let the initial state of the machine be 0 0 0 0.

How many times would I have to turn the crank to obtain:

0 0 0 1 ? One, i.e., 1 * (16**0) .

0 0 3 0 ? Forty eight, i.e., 3 * (16**1) .

0 2 0 0 ?

Strange arithmetic ...

slide51

Let the initial state of the machine be 0 0 0 0.

How many times would I have to turn the crank to obtain:

0 0 0 1 ? One, i.e., 1 * (16**0) .

0 0 3 0 ? Forty eight, i.e., 3 * (16**1) .

0 2 0 0 ? Five hundred twelve, 2 * (16**2) .

Strange arithmetic ...

slide52

Let the initial state of the machine be 0 0 0 0.

How many times would I have to turn the crank to obtain:

0 0 0 1 ? One, i.e., 1 * (16**0) .

0 0 3 0 ? Forty eight, i.e., 3 * (16**1) .

0 2 0 0 ? Five hundred twelve, 2 * (16**2) .

So, how many times would I have to turn the crank to obtain

0 2 3 1 ?

Strange arithmetic ...

slide53

Let the initial state of the machine be 0 0 0 0.

How many times would I have to turn the crank to obtain:

0 0 0 1 ? One, i.e., 1 * (16**0) .

0 0 3 0 ? Forty eight, i.e., 3 * (16**1) .

0 2 0 0 ? Five hundred twelve, 2 * (16**2) .

So, how many times would I have to turn the crank to obtain

0 2 3 1 ?

Five hundred sixty one,

i.e., 2 * (16**2) + 3 * (16**1) + 1 * (16**0) .

Strange arithmetic ...

slide54

Let the initial state of the machine be 0 0 0 0.

How many times would I have to turn the crank to obtain:

0 0 0 1 ? One, i.e., 1 * (16**0) .

0 0 3 0 ? Forty eight, i.e., 3 * (16**1) .

0 2 0 0 ? Five hundred twelve, 2 * (16**2) .

So, how many times would I have to turn the crank to obtain

0 2 3 1 ?

Five hundred sixty one,

i.e., 2 * (16**2) + 3 * (16**1) + 1 * (16**0).

Strange arithmetic ...

slide55

Let the initial state of the machine be 0 0 0 0.

How many times would I have to turn the crank to obtain:

0 0 0 1 ? One, i.e., 1 * (16**0) .

0 0 3 0 ? Forty eight, i.e., 3 * (16**1) .

0 2 0 0 ? Five hundred twelve, 2 * (16**2) .

So, how many times would I have to turn the crank to obtain

0 2 3 1 ?

Five hundred sixty one,

i.e., 2 * (16**2) + 3 * (16**1) + 1 * (16**0).

It is the same pattern: we just use 16 instead of 10 . We call that number (16 or 10) the base in which numbers are represented.

Strange arithmetic ...

slide56

Arithmetic with 10 digits (i.e., with base 10) is called decimal.

Arithmetic with 16 digits (i.e., with base 16) is called hexadecimal.

Strange arithmetic ...

slide57

Arithmetic with 10 digits (i.e., with base 10) is called decimal.

Arithmetic with 16 digits (i.e., with base 16) is called hexadecimal.

When it is not clear what base we are using, we may write it as a subscript, thus: 23110 .

We read this as “231 to the base 10”.

Strange arithmetic ...

slide58

Arithmetic with 10 digits (i.e., with base 10) is called decimal.

Arithmetic with 16 digits (i.e., with base 16) is called hexadecimal.

When it is not clear what base we are using, we may write it as a subscript, thus: 23110 .

From our examples we know that

23116 = 56110.

Strange arithmetic ...

slide59

Arithmetic with 10 digits is called decimal.

Arithmetic with 16 digits is called hexadecimal.

Here is how we work in hexadecimal arithmetic:

FE

+ 11

------

??

Strange arithmetic ...

slide60

Arithmetic with 10 digits is called decimal.

Arithmetic with 16 digits is called hexadecimal.

Here is how we work in hexadecimal arithmetic:

FE

+ 11

------

?? We just have to turn that crank…

Strange arithmetic ...

slide61

Arithmetic with 10 digits is called decimal.

Arithmetic with 16 digits is called hexadecimal.

Here is how we work in hexadecimal arithmetic:

FE

+ 11

------

? F

Strange arithmetic ...

slide62

Arithmetic with 10 digits is called decimal.

Arithmetic with 16 digits is called hexadecimal.

Here is how we work in hexadecimal arithmetic:

FE

+ 11

------

10F

Strange arithmetic ...

slide63

Arithmetic with 10 digits is called decimal.

Arithmetic with 16 digits is called hexadecimal.

Here is how we work in hexadecimal arithmetic:

FE FE16 = 1510 * 1610 + 1410 =

+ 11

------

10F

Strange arithmetic ...

slide64

Arithmetic with 10 digits is called decimal.

Arithmetic with 16 digits is called hexadecimal.

Here is how we work in hexadecimal arithmetic:

FE FE16 = 1510 * 1610 + 1410 = 25410

+ 11

------

10F

Strange arithmetic ...

slide65

Arithmetic with 10 digits is called decimal.

Arithmetic with 16 digits is called hexadecimal.

Here is how we work in hexadecimal arithmetic:

FE FE16 = 1510 * 1610 + 1410 = 25410

+ 11 1116 = 110 * 1610 + 110 =

------

10F

Strange arithmetic ...

slide66

Arithmetic with 10 digits is called decimal.

Arithmetic with 16 digits is called hexadecimal.

Here is how we work in hexadecimal arithmetic:

FE FE16 = 1510 * 1610 + 1410 = 25410

+ 11 1116 = 110 * 1610 + 110 = 1710

------

10F

Strange arithmetic ...

slide67

Arithmetic with 10 digits is called decimal.

Arithmetic with 16 digits is called hexadecimal.

Here is how we work in hexadecimal arithmetic:

FE FE16 = 1510 * 1610 + 1410 = 25410

+ 11 1116 = 110 * 1610 + 110 = 1710

------

10F 10F16 = 110 * 25610 + 010 * 1610 + 1510

=

Strange arithmetic ...

slide68

Arithmetic with 10 digits is called decimal.

Arithmetic with 16 digits is called hexadecimal.

Here is how we work in hexadecimal arithmetic:

FE FE16 = 1510 * 1610 + 1410 = 25410

+ 11 1116 = 110 * 1610 + 110 = 1710

------

10F 10F16 = 110 * 25610 + 010 * 1610 + 1510

= 27110

Strange arithmetic ...

slide69

Arithmetic with 10 digits is called decimal.

Arithmetic with 16 digits is called hexadecimal.

Here is how we work in hexadecimal arithmetic:

FE FE16 = 1510 * 1610 + 1410 = 25410

+ 11 1116 = 110 * 1610 + 110 = 1710

------

10F 10F16 = 110 * 25610 + 010 * 1610 + 1510

= 27110

Strange arithmetic ...

slide70

Arithmetic with 10 digits is called decimal.

Arithmetic with 16 digits is called hexadecimal.

Here is how we work in hexadecimal arithmetic:

FE FE16 = 1510 * 1610 + 1410 = 25410

+ 11 1116 = 110 * 1610 + 110 = 1710

------

10F 10F16 = 110 * 25610 + 010 * 1610 + 1510

= 27110

So we get the same answer, regardless of the base.

Which is reassuring…

Strange arithmetic ...

slide71

The gears are complicated, and it is hard to turn the crank. Let us simplify our adding machine.

Strange arithmetic ...

slide72

The gears are complicated, and it is hard to turn the crank. Let us simplify our adding machine.

Each strip now contains only two digits.

0

0

1

Strange arithmetic ...

slide73

The gears are complicated, and it is hard to turn the crank. Let us simplify our adding machine.

Each strip now contains only two digits.

One window in our machine is therefore called a binary digit.

0

0

1

Strange arithmetic ...

slide74

The gears are complicated, and it is hard to turn the crank. Let us simplify our adding machine.

Each strip now contains only two digits.

One window in our machine is therefore called a binary digit (bit).

0

0

1

Strange arithmetic ...

slide75

The gears are complicated, and it is hard to turn the crank. Let us simplify our adding machine.

Each strip now contains only two digits.

One window in our machine is therefore called a binary digit (bit).

Obviously, we are now talking about binary arithmetic.

0

0

1

Strange arithmetic ...

slide76

The gears are complicated, and it is hard to turn the crank. Let us simplify our adding machine.

Each strip now contains only two digits.

One window in our machine is therefore called a binary digit (bit).

We have four bits. What is the range of positive integers we can represent?

0

0

1

Strange arithmetic ...

slide77

The gears are complicated, and it is hard to turn the crank. Let us simplify our adding machine.

Each strip now contains only two digits.

One window in our machine is therefore called a binary digit (bit).

We have four bits. What is the range of positive integers we can represent?

0 .. 2**4 – 1

0

0

1

Strange arithmetic ...

slide78

The gears are complicated, and it is hard to turn the crank. Let us simplify our adding machine.

Each strip now contains only two digits.

One window in our machine is therefore called a binary digit (bit).

We have four bits. What is the range of positive integers we can represent?

0 .. 2**4 – 1

0 .. 15

0

0

1

Strange arithmetic ...

slide79

The gears are complicated, and it is hard to turn the crank. Let us simplify our adding machine.

Each strip now contains only two digits.

The base is now 2. So, in order to get 1 0 1 in our windows we must turn the crank how many times?

0

0

1

Strange arithmetic ...

slide80

The gears are complicated, and it is hard to turn the crank. Let us simplify our adding machine.

Each strip now contains only two digits.

The base is now 2. So, in order to get 1 0 1 in our windows we must turn the crank

1 * (2**2) + 0 * (2**1) + 1 * (2**0) = ?

0

0

1

Strange arithmetic ...

slide81

The gears are complicated, and it is hard to turn the crank. Let us simplify our adding machine.

Each strip now contains only two digits.

The base is now 2. So, in order to get 1 0 1 in our windows we must turn the crank

1 * (2**2) + 0 * (2**1) + 1 * (2**0) =

1* 4 + 0 * 2 + 1 = 5 times.

0

0

1

Strange arithmetic ...

slide82

The gears are complicated, and it is hard to turn the crank. Let us simplify our adding machine.

Each strip now contains only two digits.

The base is now 2. So, in order to get 1 0 1 in our windows we must turn the crank

1 * (2**2) + 0 * (2**1) + 1 * (2**0) =

1* 4 + 0 * 2 + 1 = 5 times.

In other words, 1012 = 510.

0

0

1

Strange arithmetic ...

slide83

0: 0000

Strange arithmetic ...

slide84

0: 0000

1: 0001

Strange arithmetic ...

slide85

0: 0000

1: 0001

2: 0010

Strange arithmetic ...

slide86

0: 0000

1: 0001

2: 0010

3: 0011

Strange arithmetic ...

slide87

0: 0000

1: 0001

2: 0010

3: 0011

4: 0100

Strange arithmetic ...

slide88

0: 0000

1: 0001

2: 0010

3: 0011

4: 0100

5: 0101

Strange arithmetic ...

slide89

0: 0000

1: 0001

2: 0010

3: 0011

4: 0100

5: 0101

6: 0110

7: 0111

8: 1000

9: 1001

10: 1010

11: 1011

12: 1100

13: 1101

14: 1110

15: 1111

Strange arithmetic ...

slide90

0: 0000 0

1: 0001 1

2: 0010 2

3: 0011 3

4: 0100 4

5: 0101 5

6: 0110 6

7: 0111 7

8: 1000 8

9: 1001 9

10: 1010 A

11: 1011 B

12: 1100 C

13: 1101 D

14: 1110 E

15: 1111 F

Strange arithmetic ...

slide91

0: 0000 0

1: 0001 1

2: 0010 2

3: 0011 3

4: 0100 4

5: 0101 5

6: 0110 6

7: 0111 7

8: 1000 8

9: 1001 9

10: 1010 A

11: 1011 B

12: 1100 C

13: 1101 D

14: 1110 E

15: 1111 F

This table is one of the not-so-many things that are worth learning by heart.

Strange arithmetic ...

slide92

The addition table for binary arithmetic is very simple:

+ 0 1

0

1

0: 0000 0

1: 0001 1

2: 0010 2

3: 0011 3

4: 0100 4

5: 0101 5

6: 0110 6

7: 0111 7

8: 1000 8

9: 1001 9

10: 1010 A

11: 1011 B

12: 1100 C

13: 1101 D

14: 1110 E

15: 1111 F

Strange arithmetic ...

slide93

The addition table for binary arithmetic is very simple:

+ 0 1

0 0

1

0: 0000 0

1: 0001 1

2: 0010 2

3: 0011 3

4: 0100 4

5: 0101 5

6: 0110 6

7: 0111 7

8: 1000 8

9: 1001 9

10: 1010 A

11: 1011 B

12: 1100 C

13: 1101 D

14: 1110 E

15: 1111 F

Strange arithmetic ...

slide94

The addition table for binary arithmetic is very simple:

+ 0 1

0 0 1

1

0: 0000 0

1: 0001 1

2: 0010 2

3: 0011 3

4: 0100 4

5: 0101 5

6: 0110 6

7: 0111 7

8: 1000 8

9: 1001 9

10: 1010 A

11: 1011 B

12: 1100 C

13: 1101 D

14: 1110 E

15: 1111 F

Strange arithmetic ...

slide95

The addition table for binary arithmetic is very simple:

+ 0 1

0 0 1

1 1

0: 0000 0

1: 0001 1

2: 0010 2

3: 0011 3

4: 0100 4

5: 0101 5

6: 0110 6

7: 0111 7

8: 1000 8

9: 1001 9

10: 1010 A

11: 1011 B

12: 1100 C

13: 1101 D

14: 1110 E

15: 1111 F

Strange arithmetic ...

slide96

The addition table for binary arithmetic is very simple:

+ 0 1

0 0 1

1 1 10

0: 0000 0

1: 0001 1

2: 0010 2

3: 0011 3

4: 0100 4

5: 0101 5

6: 0110 6

7: 0111 7

8: 1000 8

9: 1001 9

10: 1010 A

11: 1011 B

12: 1100 C

13: 1101 D

14: 1110 E

15: 1111 F

Strange arithmetic ...

slide97

The addition table for binary arithmetic is very simple:

+ 0 1

0 0 1

1 1 10

The multiplication table is even simpler:

* 0 1

0

1

0: 0000 0

1: 0001 1

2: 0010 2

3: 0011 3

4: 0100 4

5: 0101 5

6: 0110 6

7: 0111 7

8: 1000 8

9: 1001 9

10: 1010 A

11: 1011 B

12: 1100 C

13: 1101 D

14: 1110 E

15: 1111 F

Strange arithmetic ...

slide98

The addition table for binary arithmetic is very simple:

+ 0 1

0 0 1

1 1 10

The multiplication table is even simpler:

* 0 1

0 0

1

0: 0000 0

1: 0001 1

2: 0010 2

3: 0011 3

4: 0100 4

5: 0101 5

6: 0110 6

7: 0111 7

8: 1000 8

9: 1001 9

10: 1010 A

11: 1011 B

12: 1100 C

13: 1101 D

14: 1110 E

15: 1111 F

Strange arithmetic ...

slide99

The addition table for binary arithmetic is very simple:

+ 0 1

0 0 1

1 1 10

The multiplication table is even simpler:

* 0 1

0 0 0

1

0: 0000 0

1: 0001 1

2: 0010 2

3: 0011 3

4: 0100 4

5: 0101 5

6: 0110 6

7: 0111 7

8: 1000 8

9: 1001 9

10: 1010 A

11: 1011 B

12: 1100 C

13: 1101 D

14: 1110 E

15: 1111 F

Strange arithmetic ...

slide100

The addition table for binary arithmetic is very simple:

+ 0 1

0 0 1

1 1 10

The multiplication table is even simpler:

* 0 1

0 0 0

1 0

0: 0000 0

1: 0001 1

2: 0010 2

3: 0011 3

4: 0100 4

5: 0101 5

6: 0110 6

7: 0111 7

8: 1000 8

9: 1001 9

10: 1010 A

11: 1011 B

12: 1100 C

13: 1101 D

14: 1110 E

15: 1111 F

Strange arithmetic ...

slide101

The addition table for binary arithmetic is very simple:

+ 0 1

0 0 1

1 1 10

The multiplication table is even simpler:

* 0 1

0 0 0

1 0 1

0: 0000 0

1: 0001 1

2: 0010 2

3: 0011 3

4: 0100 4

5: 0101 5

6: 0110 6

7: 0111 7

8: 1000 8

9: 1001 9

10: 1010 A

11: 1011 B

12: 1100 C

13: 1101 D

14: 1110 E

15: 1111 F

Strange arithmetic ...

slide102

binary:

Addition: 310 + 210 = 0 0 1 1

+ 0 0 1 0

------------

0: 0000 0

1: 0001 1

2:00102

3:00113

4: 0100 4

5: 0101 5

6: 0110 6

7: 0111 7

8: 1000 8

9: 1001 9

10: 1010 A

11: 1011 B

12: 1100 C

13: 1101 D

14: 1110 E

15: 1111 F

Strange arithmetic ...

slide103

binary:

Addition: 310 + 210 = 0 0 1 1

+ 0 0 1 0

------------

1

0: 0000 0

1: 0001 1

2: 0010 2

3: 0011 3

4: 0100 4

5: 0101 5

6: 0110 6

7: 0111 7

8: 1000 8

9: 1001 9

10: 1010 A

11: 1011 B

12: 1100 C

13: 1101 D

14: 1110 E

15: 1111 F

Strange arithmetic ...

slide104

binary:

Addition: 310 + 210 = 0 0 1 1

+ 0 0 1 0

------------

0 1 carry 1

0: 0000 0

1: 0001 1

2: 0010 2

3: 0011 3

4: 0100 4

5: 0101 5

6: 0110 6

7: 0111 7

8: 1000 8

9: 1001 9

10: 1010 A

11: 1011 B

12: 1100 C

13: 1101 D

14: 1110 E

15: 1111 F

Strange arithmetic ...

slide105

binary:

Addition: 310 + 210 = 0 0 1 1

+ 0 0 1 0

------------

1 0 1

0: 0000 0

1: 0001 1

2: 0010 2

3: 0011 3

4: 0100 4

5: 0101 5

6: 0110 6

7: 0111 7

8: 1000 8

9: 1001 9

10: 1010 A

11: 1011 B

12: 1100 C

13: 1101 D

14: 1110 E

15: 1111 F

Strange arithmetic ...

slide106

binary:

Addition: 310 + 210 = 0 0 1 1

+ 0 0 1 0

------------

0 1 0 1 = 510

0: 0000 0

1: 0001 1

2: 0010 2

3: 0011 3

4: 0100 4

5:01015

6: 0110 6

7: 0111 7

8: 1000 8

9: 1001 9

10: 1010 A

11: 1011 B

12: 1100 C

13: 1101 D

14: 1110 E

15: 1111 F

Strange arithmetic ...

slide107

binary:

Addition: 310 + 210 = 0 0 1 1

+ 0 0 1 0

------------

0 1 0 1 = 510

Subtraction:

510 – 310 = 0 1 0 1

- 0 0 1 1

-----------

0: 0000 0

1: 0001 1

2: 0010 2

3:00113

4: 0100 4

5:01015

6: 0110 6

7: 0111 7

8: 1000 8

9: 1001 9

10: 1010 A

11: 1011 B

12: 1100 C

13: 1101 D

14: 1110 E

15: 1111 F

Strange arithmetic ...

slide108

binary:

Addition: 310 + 210 = 0 0 1 1

+ 0 0 1 0

------------

0 1 0 1 = 510

Subtraction:

510 – 310 = 0 1 0 1

- 0 0 1 1

-----------

0

0: 0000 0

1: 0001 1

2: 0010 2

3: 0011 3

4: 0100 4

5: 0101 5

6: 0110 6

7: 0111 7

8: 1000 8

9: 1001 9

10: 1010 A

11: 1011 B

12: 1100 C

13: 1101 D

14: 1110 E

15: 1111 F

Strange arithmetic ...

slide109

binary:

Addition: 310 + 210 = 0 0 1 1

+ 0 0 1 0

------------

0 1 0 1 = 510

Subtraction:

510 – 310 = 0 1 0 1

- 0 0 1 1

-----------

1 0 borrow 10

0: 0000 0

1: 0001 1

2: 0010 2

3: 0011 3

4: 0100 4

5: 0101 5

6: 0110 6

7: 0111 7

8: 1000 8

9: 1001 9

10: 1010 A

11: 1011 B

12: 1100 C

13: 1101 D

14: 1110 E

15: 1111 F

Strange arithmetic ...

slide110

binary:

Addition: 310 + 210 = 0 0 1 1

+ 0 0 1 0

------------

0 1 0 1 = 510

Subtraction:

510 – 310 = 0 1 0 1

- 0 0 1 1

-----------

0 0 1 0 = 210

0: 0000 0

1: 0001 1

2:00102

3: 0011 3

4: 0100 4

5: 0101 5

6: 0110 6

7: 0111 7

8: 1000 8

9: 1001 9

10: 1010 A

11: 1011 B

12: 1100 C

13: 1101 D

14: 1110 E

15: 1111 F

Strange arithmetic ...

slide111

Digression:

2** 0 = 1 12

2** 1 = 2 102

2** 2 = 4 1002

2** 3 = 8 1 0002

2** 4 = 16 10 0002

2** 5 = 32 100 0002

2** 6 = 64 1 000 0002

2** 7 = 128 ….

2** 8 = 256

2** 9 = 512

2**10 = 1 024 (Kilo)

2**20 = 1 048 576 (Mega)

2**30 = 1 073 741 824 (Giga)

Strange arithmetic ...

slide112

Digression:

2** 0 = 1 12

2** 1 = 2 102

2** 2 = 4 1002

2** 3 = 8 1 0002

2** 4 = 16 10 0002

2** 5 = 32 100 0002

2** 6 = 64 1 000 0002

2** 7 = 128 ….

2** 8 = 256

2** 9 = 512

2**10 = 1 024 (Kilo)

2**20 = 1 048 576 (Mega)

2**30 = 1 073 741 824 (Giga)

So, what is the largest number we can represent in 32 bits?

slide113

What about negative numbers?

0: 0000 0

1: 0001 1

2: 0010 2

3: 0011 3

4: 0100 4

5: 0101 5

6: 0110 6

7: 0111 7

8: 1000 8

9: 1001 9

10: 1010 A

11: 1011 B

12: 1100 C

13: 1101 D

14: 1110 E

15: 1111 F

Strange arithmetic ...

slide114

What about negative numbers?

Use the leftmost bit for the sign:

0 – no minus, 1 – minus

0: 0000 0

1: 0001 1

2: 0010 2

3: 0011 3

4: 0100 4

5: 0101 5

6: 0110 6

7: 0111 7

8: 1000 8

9: 1001 9

10: 1010 A

11: 1011 B

12: 1100 C

13: 1101 D

14: 1110 E

15: 1111 F

Strange arithmetic ...

slide115

What about negative numbers?

Use the leftmost bit for the sign :

0 – no minus, 1 – minus

Notice that within each group the original ordering still holds: - 6 > - 7 etc.

0: 0000 0

1: 0001 1

2: 0010 2

3: 0011 3

4: 0100 4

5: 0101 5

6: 0110 6

7: 0111 7

-8: 1000 8

-7: 1001 9

-6: 1010 A

-5: 1011 B

-4: 1100 C

-3: 1101 D

-2: 1110 E

-1: 1111 F

Strange arithmetic ...

slide116

What about negative numbers?

Use the leftmost bit for the sign :

0 – no minus, 1 – minus

Notice that within each group the original ordering still holds: - 6 > - 7 etc.

The range is

- 8 .. 7, i.e., - 2**(k – 1) .. 2 **(k – 1) – 1

0: 0000 0

1: 0001 1

2: 0010 2

3: 0011 3

4: 0100 4

5: 0101 5

6: 0110 6

7: 0111 7

-8: 1000 8

-7: 1001 9

-6: 1010 A

-5: 1011 B

-4: 1100 C

-3: 1101 D

-2: 1110 E

-1: 1111 F

Strange arithmetic ...

slide117

What about negative numbers?

Use the leftmost bit for the sign :

0 – no minus, 1 – minus

Notice that within each group the original ordering still holds: - 6 > - 7 etc.

The range is

- 8 .. 7, i.e., - 2**(k – 1) .. 2 **(k – 1) – 1

It is asymmetric, the negation of the smallest integer cannot be represented!

0: 0000 0

1: 0001 1

2: 0010 2

3: 0011 3

4: 0100 4

5: 0101 5

6: 0110 6

7: 0111 7

-8: 1000 8

-7: 1001 9

-6: 1010 A

-5: 1011 B

-4: 1100 C

-3: 1101 D

-2: 1110 E

-1: 1111 F

Strange arithmetic ...

slide118

This convention for representing negative numbers is called two’s complement: it is the most frequently used.

0: 0000 0

1: 0001 1

2: 0010 2

3: 0011 3

4: 0100 4

5: 0101 5

6: 0110 6

7: 0111 7

-8: 1000 8

-7: 1001 9

-6: 1010 A

-5: 1011 B

-4: 1100 C

-3: 1101 D

-2: 1110 E

-1: 1111 F

Strange arithmetic ...

slide119

This convention for representing negative numbers is called two’s complement: it is the most frequently used.

The name is due to the fact that

- a = 2**k – a

where k is the number of bits

0: 0000 0

1: 0001 1

2: 0010 2

3: 0011 3

4: 0100 4

5: 0101 5

6: 0110 6

7: 0111 7

-8: 1000 8

-7: 1001 9

-6: 1010 A

-5: 1011 B

-4: 1100 C

-3: 1101 D

-2: 1110 E

-1: 1111 F

Strange arithmetic ...

slide120

This convention for representing negative numbers is called two’s complement: it is the most frequently used.

The name is due to the fact that

- a = 2**k – a

1 0 0 0 0

- 0 1 1 0

--------------

0: 0000 0

1: 0001 1

2: 0010 2

3: 0011 3

4: 0100 4

5: 0101 5

6: 0110 6

7: 0111 7

-8: 1000 8

-7: 1001 9

-6: 1010 A

-5: 1011 B

-4: 1100 C

-3: 1101 D

-2: 1110 E

-1: 1111 F

Strange arithmetic ...

slide121

This convention for representing negative numbers is called two’s complement: it is the most frequently used.

The name is due to the fact that

- a = 2**k – a

1 0 0 0 0

- 0 1 1 0

--------------

0

0: 0000 0

1: 0001 1

2: 0010 2

3: 0011 3

4: 0100 4

5: 0101 5

6: 0110 6

7: 0111 7

-8: 1000 8

-7: 1001 9

-6: 1010 A

-5: 1011 B

-4: 1100 C

-3: 1101 D

-2: 1110 E

-1: 1111 F

Strange arithmetic ...

slide122

This convention for representing negative numbers is called two’s complement: it is the most frequently used.

The name is due to the fact that

- a = 2**k – a

1 0 0 0 0 To subtract 1 from 0 we must

- 0 1 1 0 “borrow” 10 (i.e., two!).

--------------

0

0: 0000 0

1: 0001 1

2: 0010 2

3: 0011 3

4: 0100 4

5: 0101 5

6: 0110 6

7: 0111 7

-8: 1000 8

-7: 1001 9

-6: 1010 A

-5: 1011 B

-4: 1100 C

-3: 1101 D

-2: 1110 E

-1: 1111 F

Strange arithmetic ...

slide123

This convention for representing negative numbers is called two’s complement: it is the most frequently used.

The name is due to the fact that

- a = 2**k – a

10

1 0 0 0 0 To subtract 1 from 0 we must

- 0 1 1 0 “borrow” 10 (i.e., two!).

--------------

0

0: 0000 0

1: 0001 1

2: 0010 2

3: 0011 3

4: 0100 4

5: 0101 5

6: 0110 6

7: 0111 7

-8: 1000 8

-7: 1001 9

-6: 1010 A

-5: 1011 B

-4: 1100 C

-3: 1101 D

-2: 1110 E

-1: 1111 F

Strange arithmetic ...

slide124

This convention for representing negative numbers is called two’s complement: it is the most frequently used.

The name is due to the fact that

- a = 2**k – a

1 10

1 0 0 0 0 To subtract 1 from 0 we must

- 0 1 1 0 “borrow” 10 (i.e., two!).

--------------

0

0: 0000 0

1: 0001 1

2: 0010 2

3: 0011 3

4: 0100 4

5: 0101 5

6: 0110 6

7: 0111 7

-8: 1000 8

-7: 1001 9

-6: 1010 A

-5: 1011 B

-4: 1100 C

-3: 1101 D

-2: 1110 E

-1: 1111 F

Strange arithmetic ...

slide125

This convention for representing negative numbers is called two’s complement: it is the most frequently used.

The name is due to the fact that

- a = 2**k – a

1 1 10

1 0 0 0 0 To subtract 1 from 0 we must

- 0 1 1 0 “borrow” 10 (i.e., two!).

--------------

0

0: 0000 0

1: 0001 1

2: 0010 2

3: 0011 3

4: 0100 4

5: 0101 5

6: 0110 6

7: 0111 7

-8: 1000 8

-7: 1001 9

-6: 1010 A

-5: 1011 B

-4: 1100 C

-3: 1101 D

-2: 1110 E

-1: 1111 F

Strange arithmetic ...

slide126

This convention for representing negative numbers is called two’s complement: it is the most frequently used.

The name is due to the fact that

- a = 2**k – a

1 1 10

1 0 0 0 0

- 0 1 1 0

--------------

1 0

0: 0000 0

1: 0001 1

2: 0010 2

3: 0011 3

4: 0100 4

5: 0101 5

6: 0110 6

7: 0111 7

-8: 1000 8

-7: 1001 9

-6: 1010 A

-5: 1011 B

-4: 1100 C

-3: 1101 D

-2: 1110 E

-1: 1111 F

Strange arithmetic ...

slide127

This convention for representing negative numbers is called two’s complement: it is the most frequently used.

The name is due to the fact that

- a = 2**k – a

1 1

1 0 0 0 0

- 0 1 1 0

--------------

0 1 0

0: 0000 0

1: 0001 1

2: 0010 2

3: 0011 3

4: 0100 4

5: 0101 5

6: 0110 6

7: 0111 7

-8: 1000 8

-7: 1001 9

-6: 1010 A

-5: 1011 B

-4: 1100 C

-3: 1101 D

-2: 1110 E

-1: 1111 F

Strange arithmetic ...

slide128

This convention for representing negative numbers is called two’s complement: it is the most frequently used.

The name is due to the fact that

- a = 2**k – a

1

1 0 0 0 0

- 0 1 1 0

--------------

1 0 1 0

0: 0000 0

1: 0001 1

2: 0010 2

3: 0011 3

4: 0100 4

5: 0101 5

6:01106

7: 0111 7

-8: 1000 8

-7: 1001 9

-6:1010A

-5: 1011 B

-4: 1100 C

-3: 1101 D

-2: 1110 E

-1: 1111 F

Strange arithmetic ...

slide129

This convention for representing negative numbers is called two’s complement: it is the most frequently used.

The name is due to the fact that

- a = 2**k – a

1 0 0 0 0 1 0 0 0 0

- 0 1 1 0 - 1 0 1 0

-------------- ------------

1 0 1 0 0 1 1 0

0: 0000 0

1: 0001 1

2: 0010 2

3: 0011 3

4: 0100 4

5: 0101 5

6:01106

7: 0111 7

-8: 1000 8

-7: 1001 9

-6:1010A

-5: 1011 B

-4: 1100 C

-3: 1101 D

-2: 1110 E

-1: 1111 F

Strange arithmetic ...

slide130

This convention for representing negative numbers is called two’s complement: it is the most frequently used.

The name is due to the fact that

- a = 2**k – a

A simpler method, actually used in computers: reverse all bits, then add 1

0: 0000 0

1: 0001 1

2: 0010 2

3: 0011 3

4: 0100 4

5: 0101 5

6: 0110 6

7: 0111 7

-8: 1000 8

-7: 1001 9

-6: 1010 A

-5: 1011 B

-4: 1100 C

-3: 1101 D

-2: 1110 E

-1: 1111 F

Strange arithmetic ...

slide131

This convention for representing negative numbers is called two’s complement: it is the most frequently used.

The name is due to the fact that

- a = 2**k – a

A simpler method, actually used in computers: reverse all bits, then add 1

0 1 1 0

0: 0000 0

1: 0001 1

2: 0010 2

3: 0011 3

4: 0100 4

5: 0101 5

6:01106

7: 0111 7

-8: 1000 8

-7: 1001 9

-6: 1010 A

-5: 1011 B

-4: 1100 C

-3: 1101 D

-2: 1110 E

-1: 1111 F

Strange arithmetic ...

slide132

This convention for representing negative numbers is called two’s complement: it is the most frequently used.

The name is due to the fact that

- a = 2**k – a

A simpler method, actually used in computers: reverse all bits, then add 1

0 1 1 0

1 0 0 1

0: 0000 0

1: 0001 1

2: 0010 2

3: 0011 3

4: 0100 4

5: 0101 5

6:01106

7: 0111 7

-8: 1000 8

-7: 1001 9

-6: 1010 A

-5: 1011 B

-4: 1100 C

-3: 1101 D

-2: 1110 E

-1: 1111 F

Strange arithmetic ...

slide133

This convention for representing negative numbers is called two’s complement: it is the most frequently used.

The name is due to the fact that

- a = 2**k – a

A simpler method, actually used in computers: reverse all bits, then add 1

0 1 1 0

1 0 0 1

1 0 1 0

0: 0000 0

1: 0001 1

2: 0010 2

3: 0011 3

4: 0100 4

5: 0101 5

6:01106

7: 0111 7

-8: 1000 8

-7: 1001 9

-6:1010A

-5: 1011 B

-4: 1100 C

-3: 1101 D

-2: 1110 E

-1: 1111 F

Strange arithmetic ...

slide134

This convention for representing negative numbers is called two’s complement: it is the most frequently used.

The name is due to the fact that

- a = 2**k – a

A simpler method, actually used in computers: reverse all bits, then add 1

0 1 1 0 1 0 1 0

1 0 0 1 0 1 0 1

1 0 1 0 0 1 1 0

0: 0000 0

1: 0001 1

2: 0010 2

3: 0011 3

4: 0100 4

5: 0101 5

6:01106

7: 0111 7

-8: 1000 8

-7: 1001 9

-6:1010A

-5: 1011 B

-4: 1100 C

-3: 1101 D

-2: 1110 E

-1: 1111 F

Strange arithmetic ...

slide135

This convention for representing negative numbers is called two’s complement: it is the most frequently used.

The name is due to the fact that

- a = 2**k – a

An even simpler method, for use by people:

find the rightmost occurrence of 1, then reverse all the bits to its left.

0: 0000 0

1: 0001 1

2: 0010 2

3: 0011 3

4: 0100 4

5: 0101 5

6: 0110 6

7: 0111 7

-8: 1000 8

-7: 1001 9

-6: 1010 A

-5: 1011 B

-4: 1100 C

-3: 1101 D

-2: 1110 E

-1: 1111 F

Strange arithmetic ...

slide136

This convention for representing negative numbers is called two’s complement: it is the most frequently used.

The name is due to the fact that

- a = 2**k – a

An even simpler method, for use by people:

find the rightmost occurrence of 1, then reverse all the bits to its left.

0 1 1 0

1 0 1 0

and, of course, this works both ways.

0: 0000 0

1: 0001 1

2: 0010 2

3: 0011 3

4: 0100 4

5: 0101 5

6: 0110 6

7: 0111 7

-8: 1000 8

-7: 1001 9

-6: 1010 A

-5: 1011 B

-4: 1100 C

-3: 1101 D

-2: 1110 E

-1: 1111 F

Strange arithmetic ...

slide137

This convention for representing negative numbers is called two’s complement: it is the most frequently used.

The name is due to the fact that

- a = 2**k – a

An even simpler method, for use by people:

find the rightmost occurrence of 1, then reverse all the bits to its left.

Notice that when we try to negate the minimum integer, i.e., 1000, we get (WHAT?)

0: 0000 0

1: 0001 1

2: 0010 2

3: 0011 3

4: 0100 4

5: 0101 5

6: 0110 6

7: 0111 7

-8: 1000 8

-7: 1001 9

-6: 1010 A

-5: 1011 B

-4: 1100 C

-3: 1101 D

-2: 1110 E

-1: 1111 F

Strange arithmetic ...

slide138

The beauty of two’s complement is that subtraction can be performed by addition:

7 – 5 = 7 + ( - 5 )

0: 0000 0

1: 0001 1

2: 0010 2

3: 0011 3

4: 0100 4

5: 0101 5

6: 0110 6

7: 0111 7

-8: 1000 8

-7: 1001 9

-6: 1010 A

-5: 1011 B

-4: 1100 C

-3: 1101 D

-2: 1110 E

-1: 1111 F

Strange arithmetic ...

slide139

The beauty of two’s complement is that subtraction can be performed by addition:

7 – 5 = 7 + ( - 5 )

0 1 1 1

1 0 1 1

----------

0 carry 1

0: 0000 0

1: 0001 1

2: 0010 2

3: 0011 3

4: 0100 4

5: 0101 5

6: 0110 6

7:01117

-8: 1000 8

-7: 1001 9

-6: 1010 A

-5:1011B

-4: 1100 C

-3: 1101 D

-2: 1110 E

-1: 1111 F

Strange arithmetic ...

slide140

The beauty of two’s complement is that subtraction can be performed by addition:

7 – 5 = 7 + ( - 5 )

0 1 1 1

1 0 1 1

----------

1 0 carry 1

0: 0000 0

1: 0001 1

2: 0010 2

3: 0011 3

4: 0100 4

5: 0101 5

6: 0110 6

7:01117

-8: 1000 8

-7: 1001 9

-6: 1010 A

-5:1011 B

-4: 1100 C

-3: 1101 D

-2: 1110 E

-1: 1111 F

Strange arithmetic ...

slide141

The beauty of two’s complement is that subtraction can be performed by addition:

7 – 5 = 7 + ( - 5 )

0 1 1 1

1 0 1 1

----------

0 1 0 carry 1

0: 0000 0

1: 0001 1

2: 0010 2

3: 0011 3

4: 0100 4

5: 0101 5

6: 0110 6

7:01117

-8: 1000 8

-7: 1001 9

-6: 1010 A

-5:1011 B

-4: 1100 C

-3: 1101 D

-2: 1110 E

-1: 1111 F

Strange arithmetic ...

slide142

The beauty of two’s complement is that subtraction can be performed by addition:

7 – 5 = 7 + ( - 5 )

0 1 1 1

1 0 1 1

----------

0 0 1 0 carry 1

0: 0000 0

1: 0001 1

2: 0010 2

3: 0011 3

4: 0100 4

5: 0101 5

6: 0110 6

7:01117

-8: 1000 8

-7: 1001 9

-6: 1010 A

-5:1011B

-4: 1100 C

-3: 1101 D

-2: 1110 E

-1: 1111 F

Strange arithmetic ...

slide143

The beauty of two’s complement is that subtraction can be performed by addition:

7 – 5 = 7 + ( - 5 )

0 1 1 1

1 0 1 1

----------

0 0 1 0 and the carried 1 is lost…

0: 0000 0

1: 0001 1

2:00102

3: 0011 3

4: 0100 4

5: 0101 5

6: 0110 6

7:01117

-8: 1000 8

-7: 1001 9

-6: 1010 A

-5:1011B

-4: 1100 C

-3: 1101 D

-2: 1110 E

-1: 1111 F

Strange arithmetic ...

slide144

The beauty of two’s complement is that subtraction can be performed by addition:

5 – 7 = 5 + ( - 7 )

0 1 0 1

1 0 0 1

----------

0: 0000 0

1: 0001 1

2: 0010 2

3: 0011 3

4: 0100 4

5:01015

6: 0110 6

7: 0111 7

-8: 1000 8

-7:10019

-6: 1010 A

-5: 1011 B

-4: 1100 C

-3: 1101 D

-2: 1110 E

-1: 1111 F

Strange arithmetic ...

slide145

The beauty of two’s complement is that subtraction can be performed by addition:

5 – 7 = 5 + ( - 7 )

0 1 0 1

1 0 0 1

----------

0 carry 1

0: 0000 0

1: 0001 1

2: 0010 2

3: 0011 3

4: 0100 4

5:01015

6: 0110 6

7: 0111 7

-8: 1000 8

-7:10019

-6: 1010 A

-5: 1011 B

-4: 1100 C

-3: 1101 D

-2: 1110 E

-1: 1111 F

Strange arithmetic ...

slide146

The beauty of two’s complement is that subtraction can be performed by addition:

5 – 7 = 5 + ( - 7 )

0 1 0 1

1 0 0 1

----------

1 0

0: 0000 0

1: 0001 1

2: 0010 2

3: 0011 3

4: 0100 4

5:01015

6: 0110 6

7: 0111 7

-8: 1000 8

-7:10019

-6: 1010 A

-5: 1011 B

-4: 1100 C

-3: 1101 D

-2: 1110 E

-1: 1111 F

Strange arithmetic ...

slide147

The beauty of two’s complement is that subtraction can be performed by addition:

5 – 7 = 5 + ( - 7 )

0 1 0 1

1 0 0 1

----------

1 1 0

0: 0000 0

1: 0001 1

2: 0010 2

3: 0011 3

4: 0100 4

5:01015

6: 0110 6

7: 0111 7

-8: 1000 8

-7:10019

-6: 1010 A

-5: 1011 B

-4: 1100 C

-3: 1101 D

-2: 1110 E

-1: 1111 F

Strange arithmetic ...

slide148

The beauty of two’s complement is that subtraction can be performed by addition:

5 – 7 = 5 + ( - 7 )

0 1 0 1

1 0 0 1

----------

1 1 1 0

0: 0000 0

1: 0001 1

2: 0010 2

3: 0011 3

4: 0100 4

5:01015

6: 0110 6

7: 0111 7

-8: 1000 8

-7:10019

-6: 1010 A

-5: 1011 B

-4: 1100 C

-3: 1101 D

-2:1110E

-1: 1111 F

Strange arithmetic ...

slide149

The beauty of two’s complement is that subtraction can be performed by addition:

- 3 – 4 = - 3 + ( - 4 )

1 1 0 1

1 1 0 0

----------

0: 0000 0

1: 0001 1

2: 0010 2

3: 0011 3

4: 0100 4

5: 0101 5

6: 0110 6

7: 0111 7

-8: 1000 8

-7: 1001 9

-6: 1010 A

-5: 1011 B

-4:1100 C

-3:1101D

-2: 1110 E

-1: 1111 F

Strange arithmetic ...

slide150

The beauty of two’s complement is that subtraction can be performed by addition:

- 3 – 4 = - 3 + ( - 4 )

1 1 0 1

1 1 0 0

----------

1

0: 0000 0

1: 0001 1

2: 0010 2

3: 0011 3

4: 0100 4

5: 0101 5

6: 0110 6

7: 0111 7

-8: 1000 8

-7: 1001 9

-6: 1010 A

-5: 1011 B

-4:1100 C

-3:1101 D

-2: 1110 E

-1: 1111 F

Strange arithmetic ...

slide151

The beauty of two’s complement is that subtraction can be performed by addition:

- 3 – 4 = - 3 + ( - 4 )

1 1 0 1

1 1 0 0

----------

0 1

0: 0000 0

1: 0001 1

2: 0010 2

3: 0011 3

4: 0100 4

5: 0101 5

6: 0110 6

7: 0111 7

-8: 1000 8

-7: 1001 9

-6: 1010 A

-5: 1011 B

-4:1100 C

-3:1101D

-2: 1110 E

-1: 1111 F

Strange arithmetic ...

slide152

The beauty of two’s complement is that subtraction can be performed by addition:

- 3 – 4 = - 3 + ( - 4 )

1 1 0 1

1 1 0 0

----------

0 0 1 carry 1

0: 0000 0

1: 0001 1

2: 0010 2

3: 0011 3

4: 0100 4

5: 0101 5

6: 0110 6

7: 0111 7

-8: 1000 8

-7: 1001 9

-6: 1010 A

-5: 1011 B

-4:1100C

-3:1101D

-2: 1110 E

-1: 1111 F

Strange arithmetic ...

slide153

The beauty of two’s complement is that subtraction can be performed by addition:

- 3 – 4 = - 3 + ( - 4 )

1 1 0 1

1 1 0 0

----------

1 0 0 1 carry 1, and the 1 is lost

0: 0000 0

1: 0001 1

2: 0010 2

3: 0011 3

4: 0100 4

5: 0101 5

6: 0110 6

7: 0111 7

-8: 1000 8

-7:10019

-6: 1010 A

-5: 1011 B

-4:1100C

-3:1101D

-2: 1110 E

-1: 1111 F

Strange arithmetic ...

slide154

The beauty of two’s complement is that subtraction can be performed by addition:

- 3 – 4 = - 3 + ( - 4 )

1 1 0 1

1 1 0 0

----------

1 0 0 1 carry 1, and the 1 is lost

Notice that it is not so trivial to detect overflow!

We need 2 extra bits for that, and many machines don’t do it.

0: 0000 0

1: 0001 1

2: 0010 2

3: 0011 3

4: 0100 4

5: 0101 5

6: 0110 6

7: 0111 7

-8: 1000 8

-7: 1001 9

-6: 1010 A

-5: 1011 B

-4: 1100 C

-3: 1101 D

-2: 1110 E

-1: 1111 F

Strange arithmetic ...

slide155

Multiplication by 2: just multiply by 102

0011  0110

0: 0000 0

1: 0001 1

2: 0010 2

3:00113

4: 0100 4

5: 0101 5

6:01106

7: 0111 7

-8: 1000 8

-7: 1001 9

-6: 1010 A

-5: 1011 B

-4: 1100 C

-3: 1101 D

-2: 1110 E

-1: 1111 F

Strange arithmetic ...

slide156

Multiplication by 2: just multiply by 102

0011  0110

This is quite like multiplying by 10 in decimal arithmetic.

0: 0000 0

1: 0001 1

2: 0010 2

3:00113

4: 0100 4

5: 0101 5

6:01106

7: 0111 7

-8: 1000 8

-7: 1001 9

-6: 1010 A

-5: 1011 B

-4: 1100 C

-3: 1101 D

-2: 1110 E

-1: 1111 F

Strange arithmetic ...

slide157

Multiplication by 2: just multiply by 10

0011  0110

This is quite like multiplying by 10 in decimal arithmetic.

We can also think about it in terms of shifting the bits.

0: 0000 0

1: 0001 1

2: 0010 2

3:00113

4: 0100 4

5: 0101 5

6:01106

7: 0111 7

-8: 1000 8

-7: 1001 9

-6: 1010 A

-5: 1011 B

-4: 1100 C

-3: 1101 D

-2: 1110 E

-1: 1111 F

Strange arithmetic ...

slide158

Multiplication by 2: just shift left

0011  0110

0: 0000 0

1: 0001 1

2: 0010 2

3:00113

4: 0100 4

5: 0101 5

6:01106

7: 0111 7

-8: 1000 8

-7: 1001 9

-6: 1010 A

-5: 1011 B

-4: 1100 C

-3: 1101 D

-2: 1110 E

-1: 1111 F

Strange arithmetic ...

slide159

Multiplication by 2: just shift left

0011  0110

1101  1010

0: 0000 0

1: 0001 1

2: 0010 2

3: 0011 3

4: 0100 4

5: 0101 5

6: 0110 6

7: 0111 7

-8: 1000 8

-7: 1001 9

-6:1010A

-5: 1011 B

-4: 1100 C

-3:1101D

-2: 1110 E

-1: 1111 F

Strange arithmetic ...

slide160

Multiplication by 2: just shift left

0011  0110

1101  1010

Of course, this doesn’t work when we get out of range:

1011  0110

0: 0000 0

1: 0001 1

2: 0010 2

3: 0011 3

4: 0100 4

5: 0101 5

6:01106

7: 0111 7

-8: 1000 8

-7: 1001 9

-6: 1010 A

-5:1011B

-4: 1100 C

-3: 1101 D

-2: 1110 E

-1: 1111 F

Strange arithmetic ...

slide161

Integer division by 2: shift right, extending the sign bit.

0: 0000 0

1: 0001 1

2: 0010 2

3: 0011 3

4: 0100 4

5: 0101 5

6: 0110 6

7: 0111 7

-8: 1000 8

-7: 1001 9

-6: 1010 A

-5: 1011 B

-4: 1100 C

-3: 1101 D

-2: 1110 E

-1: 1111 F

Strange arithmetic ...

slide162

Integer division by 2: shift right, extending the sign bit.

0110  0011

0: 0000 0

1: 0001 1

2: 0010 2

3:00113

4: 0100 4

5: 0101 5

6:01106

7: 0111 7

-8: 1000 8

-7: 1001 9

-6: 1010 A

-5: 1011 B

-4: 1100 C

-3: 1101 D

-2: 1110 E

-1: 1111 F

Strange arithmetic ...

slide163

Integer division by 2: shift right, extending the sign bit.

0110  0011

1010  1101

0: 0000 0

1: 0001 1

2: 0010 2

3: 0011 3

4: 0100 4

5: 0101 5

6: 0110 6

7: 0111 7

-8: 1000 8

-7: 1001 9

-6:1010A

-5: 1011 B

-4: 1100 C

-3:1101D

-2: 1110 E

-1: 1111 F

Strange arithmetic ...

slide164

Integer division by 2: shift right, extending the sign bit.

0110  0011

1010  1101

0111  0011

0: 0000 0

1: 0001 1

2: 0010 2

3:00113

4: 0100 4

5: 0101 5

6: 0110 6

7:01117

-8: 1000 8

-7: 1001 9

-6: 1010 A

-5: 1011 B

-4: 1100 C

-3: 1101 D

-2: 1110 E

-1: 1111 F

Strange arithmetic ...

slide165

Integer division by 2: shift right, extending the sign bit.

0110  0011

1010  1101

0111  0011

1001  1100 ???

0: 0000 0

1: 0001 1

2: 0010 2

3: 0011 3

4: 0100 4

5: 0101 5

6: 0110 6

7: 0111 7

-8: 1000 8

-7:10019

-6: 1010 A

-5: 1011 B

-4:1100C

-3: 1101 D

-2: 1110 E

-1: 1111 F

Strange arithmetic ...

slide166

Integer division by 2: shift right, extending the sign bit.

0110  0011

1010  1101

0111  0011

1001  1100

This is consistent if the remainder is + 1 :

- 4 * 2 + 1 = - 8 + 1 = - 7

0: 0000 0

1: 0001 1

2: 0010 2

3: 0011 3

4: 0100 4

5: 0101 5

6: 0110 6

7: 0111 7

-8: 1000 8

-7:10019

-6: 1010 A

-5: 1011 B

-4:1100C

-3: 1101 D

-2: 1110 E

-1: 1111 F

Strange arithmetic ...

slide167

In general, we must have

A = A div B * B + A mod B

For negative A or B there are two consistent choices of the divisor and the remainder:

-7 = - 4 * 2 + 1 = -3 * 2 – 1

-7 = 4 * (-2) + 1 = 3 * (-2) – 1

0: 0000 0

1: 0001 1

2: 0010 2

3: 0011 3

4: 0100 4

5: 0101 5

6: 0110 6

7: 0111 7

-8: 1000 8

-7: 1001 9

-6: 1010 A

-5: 1011 B

-4: 1100 C

-3: 1101 D

-2: 1110 E

-1: 1111 F

Strange arithmetic ...

slide168

In general, we must have

A = A div B * B + A mod B

For negative A or B there are two consistent choices of the divisor and the remainder:

-7 = - 4 * 2 + 1 = -3 * 2 – 1

-7 = 4 * (-2) + 1 = 3 * (-2) – 1

The first choice is simpler: the remainder is the low bit, the divisor is obtained by shifting.

0: 0000 0

1: 0001 1

2: 0010 2

3: 0011 3

4: 0100 4

5: 0101 5

6: 0110 6

7: 0111 7

-8: 1000 8

-7:10019

-6: 1010 A

-5: 1011 B

-4: 1100 C

-3: 1101 D

-2: 1110 E

-1: 1111 F

Strange arithmetic ...

slide169

In general, we must have

A = A div B * B + A mod B

For negative A or B there are two consistent choices of the divisor and the remainder:

-7 = - 4 * 2 + 1 = -3 * 2 – 1

-7 = 4 * (-2) + 1 = 3 * (-2) – 1

The first choice is simpler: the remainder is the low bit, the divisor is obtained by shifting.

The second choice is more “human friendly”.

0: 0000 0

1: 0001 1

2: 0010 2

3: 0011 3

4: 0100 4

5: 0101 5

6: 0110 6

7: 0111 7

-8: 1000 8

-7:10019

-6: 1010 A

-5: 1011 B

-4: 1100 C

-3: 1101 D

-2: 1110 E

-1: 1111 F

Strange arithmetic ...

slide170

In general, we must have

A = A div B * B + A mod B

For negative A or B there are two consistent choices of the divisor and the remainder:

-7 = - 4 * 2 + 1 = -3 * 2 – 1

-7 = 4 * (-2) + 1 = 3 * (-2) – 1

The first choice is simpler: the remainder is the low bit, the divisor is obtained by shifting.

The second choice is more “human friendly”.

There is no general agreement which is best.

0: 0000 0

1: 0001 1

2: 0010 2

3: 0011 3

4: 0100 4

5: 0101 5

6: 0110 6

7: 0111 7

-8: 1000 8

-7: 1001 9

-6: 1010 A

-5: 1011 B

-4: 1100 C

-3: 1101 D

-2: 1110 E

-1: 1111 F

slide171

Multiplication and long division is carried out in the usual fashion, except that it is simpler (because the multiplication table is trivial).

0: 0000 0

1: 0001 1

2: 0010 2

3: 0011 3

4: 0100 4

5: 0101 5

6: 0110 6

7: 0111 7

-8: 1000 8

-7: 1001 9

-6: 1010 A

-5: 1011 B

-4: 1100 C

-3: 1101 D

-2: 1110 E

-1: 1111 F

Strange arithmetic ...

slide172

Multiplication and long division is carried out in the usual fashion, except that it is simpler (because the multiplication table is trivial).

I’ll leave this as an exercise.

Please do some examples, not forgetting about negative numbers!

0: 0000 0

1: 0001 1

2: 0010 2

3: 0011 3

4: 0100 4

5: 0101 5

6: 0110 6

7: 0111 7

-8: 1000 8

-7: 1001 9

-6: 1010 A

-5: 1011 B

-4: 1100 C

-3: 1101 D

-2: 1110 E

-1: 1111 F

Strange arithmetic ...

slide173

The 4-bit units we have been discussing are called nibbles.

An 8-bit unit is called a byte: the range is

0 .. 255 or -128 .. 127.

0: 0000 0

1: 0001 1

2: 0010 2

3: 0011 3

4: 0100 4

5: 0101 5

6: 0110 6

7: 0111 7

-8: 1000 8

-7: 1001 9

-6: 1010 A

-5: 1011 B

-4: 1100 C

-3: 1101 D

-2: 1110 E

-1: 1111 F

Strange arithmetic ...

slide174

The 4-bit units we have been discussing are called nibbles.

An 8-bit unit is called a byte: the range is

0 .. 255 or -128 .. 127.

How many nibbles do we need to represent a byte?

0: 0000 0

1: 0001 1

2: 0010 2

3: 0011 3

4: 0100 4

5: 0101 5

6: 0110 6

7: 0111 7

-8: 1000 8

-7: 1001 9

-6: 1010 A

-5: 1011 B

-4: 1100 C

-3: 1101 D

-2: 1110 E

-1: 1111 F

Strange arithmetic ...

slide175

The 4-bit units we have been discussing are called nibbles.

An 8-bit unit is called a byte: the range is

0 .. 255 or -128 .. 127.

How many nibbles do we need to represent a byte?

Two, of course.

For example, B716 = 1011 0111 2

0: 0000 0

1: 0001 1

2: 0010 2

3: 0011 3

4: 0100 4

5: 0101 5

6: 0110 6

7: 0111 7

-8: 1000 8

-7: 1001 9

-6: 1010 A

-5: 1011 B

-4: 1100 C

-3: 1101 D

-2: 1110 E

-1: 1111 F

Strange arithmetic ...

slide176

The 4-bit units we have been discussing are called nibbles.

An 8-bit unit is called a byte: the range is

0 .. 255 or -128 .. 127.

Integers are normally represented in words.

For a 16-bit word the range is

0 .. 65535 or - 32768 .. 32767 .

0: 0000 0

1: 0001 1

2: 0010 2

3: 0011 3

4: 0100 4

5: 0101 5

6: 0110 6

7: 0111 7

-8: 1000 8

-7: 1001 9

-6: 1010 A

-5: 1011 B

-4: 1100 C

-3: 1101 D

-2: 1110 E

-1: 1111 F

Strange arithmetic ...

slide177

The 4-bit units we have been discussing are called nibbles.

An 8-bit unit is called a byte: the range is

0 .. 255 or -128 .. 127.

Integers are normally represented in words.

For a 16-bit word the range is

0 .. 65535 or - 32768 .. 32767 .

For a 32-bit word (most common) it is:

0 .. 4 294 967 295 or

- 2 147 483 648 .. 2 147 483 647

0: 0000 0

1: 0001 1

2: 0010 2

3: 0011 3

4: 0100 4

5: 0101 5

6: 0110 6

7: 0111 7

-8: 1000 8

-7: 1001 9

-6: 1010 A

-5: 1011 B

-4: 1100 C

-3: 1101 D

-2: 1110 E

-1: 1111 F

Strange arithmetic ...

slide178

The 4-bit units we have been discussing are called nibbles.

An 8-bit unit is called a byte: the range is

0 .. 255 or -128 .. 127.

Integers are normally represented in words.

For a 16-bit word the range is

0 .. 65535 or - 32768 .. 32767 .

For a 32-bit word (most common) it is:

0 .. 4 294 967 295 or

- 2 147 483 648 .. 2 147 483 647

This range is large, but it is still finite!

Remember, a + 1 > a does not always hold.

0: 0000 0

1: 0001 1

2: 0010 2

3: 0011 3

4: 0100 4

5: 0101 5

6: 0110 6

7: 0111 7

-8: 1000 8

-7: 1001 9

-6: 1010 A

-5: 1011 B

-4: 1100 C

-3: 1101 D

-2: 1110 E

-1: 1111 F

Strange arithmetic ...

slide179

Main points:

Strange arithmetic ...

slide180

Main points:

  • Finite arithmetic is tricky: addition “wraps around”.

Strange arithmetic ...

slide181

Main points:

  • Finite arithmetic is tricky: addition “wraps around”.
  • For two’s complement binary arithmetic:

Strange arithmetic ...

slide182

Main points:

  • Finite arithmetic is tricky: addition “wraps around”.
  • For two’s complement binary arithmetic:
    • the “wrap around” is to negative numbers;

Strange arithmetic ...

slide183

Main points:

  • Finite arithmetic is tricky: addition “wraps around”.
  • For two’s complement binary arithmetic:
    • the “wrap around” is to negative numbers;
    • the range of integers that can be represented is asymmetric;

Strange arithmetic ...

slide184

Main points:

  • Finite arithmetic is tricky: addition “wraps around” etc.
  • For two’s complement binary arithmetic:
    • the “wrap around” is to negative numbers;
    • the range of integers that can be represented is asymmetric;
    • multiplication and division by powers of 2 is particularly simple.

Strange arithmetic ...

ad