Binary arithmetic - Computing and ICT in a Nutshell

Download Report

Transcript Binary arithmetic - Computing and ICT in a Nutshell

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:
x10
x10
x10
1000
100
10
1
1
2
3
4
In each column
we can have
ten different
digits (1 to 9
and 0)
As we move left, the
column headings
increase by a factor
of ten
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
Binary
The binary system is based on the number two:
x2
x2
x2
8
4
2
1
1
0
1
1
In each column
we can have
two different
digits (0 or 1)
As we move left, the
column headings
increase by a factor
of two
This number is:
8 + 2 + 1 = 11
It’s still eleven, it’s just written
down differently
Largest Numbers
1000
100
10
1
0
0
0
9
1000
100
10
1
0
0
9
9
8
4
2
1
0
1
1
1
Binary behaves like
ordinary numbers in
lots of ways – think
of the largest
number we can write
with a given number
of digits...
Binary behaves in
the same way – e.g.
111 is seven.
Multiplying By 10
1000
100
10
1
0
0
2
5
0
2
5
0
8
4
2
1
0
0
1
1
0
1
1
0
In denary, if we
multiply by 10 then
the digits shift one
place to the left.
In binary, if we
multiply by 10 then
the digits shift one
place to the left
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!
Example
8
4
2
1
0
1
1
0
+ 0
0
0
1
In this case, adding
is easy because
there’s nothing to
carry
1
How can we check
the answer?
0
1
1
110 = six, and 1 = one – if we add six and one
together we should get seven = 111.
Example
8
4
2
1
0
1
1
0
+ 0
0
1
1
1
0
0
1
1
1
In this case, we have
two ones in the twos
column – what do we
do?
1 + 1 = 10, so we
need to carry one
Example
8
4
2
1
0
1
1
0
+ 1
0
1
1
Computers use a
fixed number of bits
to represent
numbers – usually 8,
16, 32, or 64 bits.
1
What’s happened
here?
1 0
0
0
There’s an extra digit – the answer is too big to fit!
This is called overflow.
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.
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.
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 twoscomplement form
Example
Imagine we want to subtract one from seven:
Here’s the seven:
One would be 0001;
the 1s-complement
would be 1110, and
the 2s-complement
would be 1111.
1
8
4
2
1
0
1
1
1
+ 1
1
1
1
0
1
1
0
There’s an overflow digit – when we are subtracting
we just discard it. The answer is 0110 = six.