13
Binary Arithmetic In today’s lesson we will look at: a reminder of how binary works adding binary numbers overflow complements negative numbers and

Binary Arithmetic In today’s lesson we will look at: a reminder of how binary works adding binary numbers overflow complements negative numbers and subtraction

Embed Size (px)

Citation preview

Page 1: Binary Arithmetic In today’s lesson we will look at: a reminder of how binary works adding binary numbers overflow complements negative numbers and subtraction

Binary Arithmetic

In today’s lesson we will look at:

• a reminder of how binary works

• adding binary numbers

• overflow

• complements

• negative numbers and subtraction

Page 2: Binary Arithmetic In today’s lesson we will look at: a reminder of how binary works adding binary numbers overflow complements negative numbers and subtraction

Denary

The number system most of us use is based on tens:

1 2 3 4

101000 1100

x10x10 x10

As we move left, the column headings increase by a factor of ten

In each column we can have ten different digits (1 to 9 and 0)

This number is:

1 x 1000 + 2 x 100 + 3 x 10 + 4 x 1

That gives us one thousand two hundred and thirty four

Page 3: Binary Arithmetic In today’s lesson we will look at: a reminder of how binary works adding binary numbers overflow complements negative numbers and subtraction

Binary

The binary system is based on the number two:

1 0 1 1

28 14

x2x2 x2

As we move left, the column headings increase by a factor of two

In each column we can have two different digits (0 or 1)

This number is:

8 + 2 + 1 = 11

It’s still eleven, it’s just written down differently

Page 4: Binary Arithmetic In today’s lesson we will look at: a reminder of how binary works adding binary numbers overflow complements negative numbers and subtraction

Largest Numbers

0 0 0 9

101000 1100

Binary behaves like ordinary numbers in lots of ways – think of the largest number we can write with a given number of digits...

0 0 9 9

101000 1100

0 1 1 1

28 14Binary behaves in the same way – e.g. 111 is seven.

Page 5: Binary Arithmetic In today’s lesson we will look at: a reminder of how binary works adding binary numbers overflow complements negative numbers and subtraction

Multiplying By 10

0 0 2 5

101000 1100

In denary, if we multiply by 10 then the digits shift one place to the left. 0 2 5 0

0 0 1 1

28 14

In binary, if we multiply by 10 then the digits shift one place to the left0 1 1 0

Page 6: Binary Arithmetic In today’s lesson we will look at: a reminder of how binary works adding binary numbers overflow complements negative numbers and subtraction

Adding Binary Numbers

• The same is true of adding numbers.

• Think about what happens when you’re adding two normal numbers:– we line the numbers up so that the units are

aligned

– we start on the right and add up each column separately

– if the column total is 10 or more then we need to carry

• Adding up in binary is exactly the same!

Page 7: Binary Arithmetic In today’s lesson we will look at: a reminder of how binary works adding binary numbers overflow complements negative numbers and subtraction

Example

0 1 1 0

28 14

In this case, adding is easy because there’s nothing to carry0 0 0 1

0 1 1 1 How can we check the answer?

+

110 = six, and 1 = one – if we add six and one together we should get seven = 111.

Page 8: Binary Arithmetic In today’s lesson we will look at: a reminder of how binary works adding binary numbers overflow complements negative numbers and subtraction

Example

0 1 1 0

28 14

In this case, we have two ones in the twos column – what do we do?0 0 1 1

1

0 0 1

+

1 1 + 1 = 10, so we need to carry one

1

Page 9: Binary Arithmetic In today’s lesson we will look at: a reminder of how binary works adding binary numbers overflow complements negative numbers and subtraction

Example

0 1 1 0

28 14

Computers use a fixed number of bits to represent numbers – usually 8, 16, 32, or 64 bits.1 0 1 1

0 0 1

+

What’s happened here?01

There’s an extra digit – the answer is too big to fit! This is called overflow.

Page 10: Binary Arithmetic In today’s lesson we will look at: a reminder of how binary works adding binary numbers overflow complements negative numbers and subtraction

Overflow

• Overflow is the name we give to the situation where a number is too big to fit into the allocated numbers of bits.

• This is an undesirable situation because we don’t really know what the answer is.

• This can also happen when we’re sampling data from the real world – e.g. if we try to record a sound that’s too loud.

• Computers can get around this problem by using multiple bytes to store numbers.

Page 11: Binary Arithmetic In today’s lesson we will look at: a reminder of how binary works adding binary numbers overflow complements negative numbers and subtraction

Subtraction

• You can subtract binary numbers in the same way as we subtract ordinary numbers, e.g. by borrowing if the bottom digit is smaller than the top one.

• There is also another method that uses complements.

• It uses the idea that subtracting a number is the same as adding a negative number – e.g. taking 2 away from 6 is the same as adding -2 to 6.

Page 12: Binary Arithmetic In today’s lesson we will look at: a reminder of how binary works adding binary numbers overflow complements negative numbers and subtraction

Complements• Negative numbers in binary are often represented

using something called a twos-complement.

• A ones-complement takes all of the digits and swaps 1s for 0s and vice versa – e.g. for the number 3:

0 0 1 1 1 1 0 0• A twos-complement just takes this number and adds

one to it:

1 1 0 1 This is -3 in twos-complement form

Page 13: Binary Arithmetic In today’s lesson we will look at: a reminder of how binary works adding binary numbers overflow complements negative numbers and subtraction

Example

0 1 1 1

28 14

Imagine we want to subtract one from seven:

1 1 1 1

1 1 0

+

01

There’s an overflow digit – when we are subtracting we just discard it. The answer is 0110 = six.

Here’s the seven:

One would be 0001; the 1s-complement would be 1110, and the 2s-complement would be 1111.