The binary number system

Download Report

Transcript The binary number system

THE BINARY
NUMBER SYSTEM
A LEVEL COMPUTER SCIENCE
YOU WILL LEARN:
• How to multiply positive binary numbers
• How to work with signed binary numbers using two’s complement
• How fixed numbers are used to represent fractions
UNSIGNED BINARY
• Binary that represents positive numbers only
• This is all we’ve dealt with up to this point
BINARY ADDITION RECAP
Add the following binary numbers together:
+
1 1 1 0
1 0 1
+
1 1 0 1 0 0 1 1
1 1 0 0 1 0 1
1 1 0 0 0 0
Convert 5D from hex to binary:
4
RECAP ADDING WITH BINARY
Complete the following rules when adding with binary:
0+0=
1+0=
0+1=
1+1=
1+1+1=
1+1+1+1=
5
ADDING WITH BINARY
Complete the worksheet
to recap adding with
binary.
6
MULTIPLYING UNSIGNED BINARY
• Multiplication in binary is a multi-step process:
– Multiply the first number by each of the digits in the second number in turn starting from the righthand side
• This means you’re multiplying each digit by 1 or 0, which gives you either 1 or 0 as your answer.
– Do the same for the next digit, shifting your answers to the left
– Then carry out a binary addition to find the answer
• Always convert to decimal and double check your answer, as it is easy to make a mistake
EXAMPLE
11011 is 27
11 is 3
27 x 3 = 81
Is 1010001 81?
Note the zero on the LSB as the
numbers have been shifted to the left
BINARY MULTIPLICATION TASKS
1.
Multiply the binary numbers 111 and 001
2.
Multiply the binary numbers 00111 and 10011
3.
Multiply the binary numbers 010 and 111
4.
Multiply the binary numbers 1100 and 0110
5.
Multiply the binary numbers 10111 and 01111
6.
Multiply the binary numbers 11000 and 00011
7.
Multiply the binary numbers 101100 and 0111
8.
Multiply the binary numbers 11110110 and 1101
9.
Multiply the binary numbers 10111110 and 0001101
10. Multiply the binary numbers 10110101 and 00111101
TYPES OF NUMBERS
• So far in binary we’ve only dealt with integers
• Specifically, we’ve used unsigned integers – positive whole numbers
• As we looked at previously, computers all need to be able to handle real numbers (positive and
negative numbers that may include decimal places)
TWO’S COMPLEMENT
• A method of working with signed binary values
• This means two’s complement can represent both positive and negative integers
• With two’s complement, the most significant bit becomes negative
• Using an 8-bit code, this means the MSB represents a value of -128:
-128
64
32
16
8
4
2
1
EXAMPLE
• Converting 10011100 from binary to denary using two’s complement:
-128
64
32
16
8
4
2
1
1
0
0
1
1
1
0
0
• -128+16+8+4 = -100
NEGATIVE NUMBERS
• Converting from denary to binary using two’s complement can be more difficult for negative
numbers as you are starting from a negative number and working forward
• Remember that with two’s complement, when the MSB is 1 it means that the number must be
negative
• The following method may make conversions easier:
EXAMPLE
• Convert -102 into binary
• First write out the binary equivalent of +102
128
64
32
16
8
4
2
1
0
1
1
0
0
1
1
0
• Starting from the LSB, write out the number again. Once you’ve passed the first one, reverse
every digit:
-128
64
32
16
8
4
2
1
1
0
0
1
1
0
1
0
TWO’S COMPLEMENT
• With 8 bits, it is possible to represent 256 different numbers:
– -128 to 127
• Addition works the same in two’s complement as it does with our previous binary addition,
with the exception that you will sometimes get an overflow (an extra 1)
• We’ll be looking at overflows next week
• To subtract in binary, convert the number to be subtracted into a negative number, and then
add together as normal – computers can’t handle subtraction
• For any number of bits (n), 2n values can be represented
TWO’S COMPLEMENT PRACTICE
Convert the following two’s complement numbers into denary:
1.
01011
2.
100010
3.
11110001
4.
010101
5.
11011000
6.
10110010
7.
01011011
8.
0101111101
9.
1110001010
10. 1011010101
11. 0101100001
12. 0101111001010100
13. 1010011001001010
TWO’S COMPLEMENT SUBTRACTION
• To subtract in binary, convert the number to be subtracted into a negative number,
and then add together as normal – computers can’t handle subtraction
• Perform the following binary subtractions:
1. 31-4
2. 99-45
3. 10-2
4. 50-6
5. 101-12
6. 155-4
7. 200-200
8. 212-99
9. 200-197
10. 230-19
11. 255-184
12. 356-287
FIXED POINT NUMBERS
• To represent real decimal numbers (numbers with decimal places or a fractional part), fixed
point representation can be used
• In the same way that decimal has a decimal point, binary has a binary point
• The numbers after the binary point represent fractions
EXAMPLE
8
4
2
1
1/2
1/4
1/8
1/16
0
1
1
0
0
1
1
0
Bits before the binary point are handed in the same way as usual
After the binary point, the values half every each time as you move from left to right
Therefore the number above is
4 + 2 + ¼ + 1/8
= 6 3/8 or 6.375
FIXED POINT
• The binary point can be placed anywhere within the byte, but the position of the binary point
restricts the size of the number that be represented and the accuracy of the number
• With the binary point in the position shown previously:
– The smallest number we could represent (apart from 0) is 0000.0001 which is 1.16 or 0.0625
– The next smallest number is 0000.0010 which is 1/8, 2/16 or 0.125
– It is not possible to represent any number between 0.0625 and 0.125
– Moving the binary point to the left allows for more accurate decimals, but reduces the range of
whole numbers available
– Moving the binary point to the right increases the range of whole numbers but reduces the accuracy
NEGATIVE FIXED POINT NUMBERS
• Combining the two techniques from today’s lesson allows us to represent negative numbers
-8
4
2
1
1/2
1/4
1/8
1/16
1
1
1
0
1
1
0
0
• This number would be:
• -8 + 4 + 2 + ½ + ¼ = -1¼
FIXED POINT PRACTICE
• Convert the following fixed point numbers to denary:
1. 0110.11
2. 10011011.1010
3. 0101.111
4. 11011.1001
5. 110111.001
6. 111001.01011
7. 10101010.1101
FIXED POINT PRACTICE
• Convert the following denary numbers to fixed point:
1. 13.125
2. 6.3125
3. 5.125
4. 12.625
5. 20.75
6. 36.3125
7. 45.4296875
HOMEWORK TASKS
1.
Finish any unfinished tasks from today’s lesson
2.
Explain the term ‘unsigned integer’
3.
Show how an 8-bit two’s complement integer would be used to store:
a) +64
b) -64
c) +100
d) -100
4.
Add the binary numbers 1001 and 1100. Leave your answer as a binary number and show
your working
5.
Use two’s complement to carry out the following calculations:
a) 12 + 8
6.
b) 25 - 17
Convert 3¼ into a binary number