32 point font 28 point font 24 point font 20 point font 18 point font 16 point font 14 point font

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

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

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

## 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

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

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…

Programming Fundamentals

3

Feliks Kluzniak

Strange arithmetic ...

Executive summary:

• We discuss:
• arithmetic with a limited number of digits

Strange arithmetic ...

Imagine a strip of plastic with 10 digits:

0

0

1

2

3

4

5

6

7

8

9

Strange arithmetic ...

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 ...

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 ...

… 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 ...

… 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 ...

Wow, I have an adding machine!

7

Strange arithmetic ...

Wow, I have an adding machine!

What if I turn it another 4 times?

7

Strange arithmetic ...

1

Strange arithmetic ...

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 ...

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

1

Strange arithmetic ...

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

0

0

Strange arithmetic ...

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 ...

Nice!

What will happen if I turn it 90 more times?

(It’s good exercise.)

1

5

Strange arithmetic ...

After 90 more turns:

So now we have arithmetic modulo 100.

0

5

Strange arithmetic ...

0]

0

0

0

0

Strange arithmetic ...

This machine will allow us to perform addition modulo

10 000.

0]

0

0

0

0

Strange arithmetic ...

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 ...

What range of positive integers can we represent with:

4 digits : 0 .. 9999

Strange arithmetic ...

What range of positive integers can we represent with:

4 digits : 0 .. 9999

2 digits: 0 .. 99

1 digit : 0 .. 9

Strange arithmetic ...

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 ...

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

Strange arithmetic ...

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 ...

An important lesson:

If the number of digits is limited, then the inequality

a + 1 > a

does not always hold!

Strange arithmetic ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

0

0

1

2

3

4

5

6

7

8

9

A

B

C

D

E

F

Strange arithmetic ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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

Strange arithmetic ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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

Strange arithmetic ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

0: 0000

Strange arithmetic ...

0: 0000

1: 0001

Strange arithmetic ...

0: 0000

1: 0001

2: 0010

Strange arithmetic ...

0: 0000

1: 0001

2: 0010

3: 0011

Strange arithmetic ...

0: 0000

1: 0001

2: 0010

3: 0011

4: 0100

Strange arithmetic ...

0: 0000

1: 0001

2: 0010

3: 0011

4: 0100

5: 0101

Strange arithmetic ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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?

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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

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 ...

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.

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

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 ...

Main points:

Strange arithmetic ...

Main points:

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

Strange arithmetic ...

Main points:

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

Strange arithmetic ...

Main points:

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

Strange arithmetic ...

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 ...

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 ...