ALevelComputing_Session9

Download Report

Transcript ALevelComputing_Session9

Session Objectives#9
perform integer binary arithmetic, that is addition and subtraction
describe and use two’s complement and sign and magnitude to represent negative
integers;
express numbers in binary, binary-coded decimal (BCD), octal and hexadecimal;
Create a GCSE calculator using an IF ELSE statement
A-Level Computing#BristolMet
Binary Arithmetic & BCD
Starter:
How many binary digits would be required for this hexadecimal
code and what is the 8 bit binary equivalent:
#FF9B22
A-Level Computing#BristolMet
Converting Binary to Hexadecimal
Denary 45 in binary is 32 + 8 + 4 + 1 or
128
0
64
0
32
1
16
0
8
1
4
1
2
0
1
1
Split into 2 nibbles and treated as 4 bits each:
8
0
4 2 1
0 1 0
= 2
8
1
=
4 2
1 0
= 13
1
1
2D
TASK: Using the same method convert a) 11101011 b) 10100011
a) EB
Now try the reverse, convert to binary a) A5
b) A3
b) 3B
a) 10100101 b)00111011
GCSE Computing#BristolMet
Binary Coded Decimal (BCD)
Binary Coded Decimal (BCD) uses 4 bit binary to represent
decimal digits 0 – 9.
You simply split
example, decimal
7 =
8 4
0 1
the digits and treat the separately. For
75 in BCD is as follows:
5 =
2 1
8 4 2 1
1 1
0 1 0 1
Therefore 75 in BCD becomes:
01110101
Now convert the following denary into BCD:
a) 39
b) 58
c) 97
A-Level Computing#BristolMet
Binary Arithmetic
Another reason why computers are designed to use binary is that
addition is so simple in binary. In binary there are only 4 sums
which need to be known:
0
0
1
1
+
+
+
+
0
1
0
1
=
=
=
=
0
1
1
0, carry 1
For example;
75 = 0 1 0 0
+
14 = 0 0 0 0
0 1 0 1
Carry
1
1 0 1 1
1 1 1 0
1 0 0 1 = 89
1 1
A-Level Computing#BristolMet
Now try:
a) 01101101
+ 01110001
b) 01111000
+ 00110011
Sign & Magnitude
So far we have learned how to store positive whole numbers in
binary but there is a need to use negative numbers and
fractions.
You will remember that only 7 bits is need to represent the
ASCII character set (127 characters) and this is the purpose of
the 8th bit, to represent a +/For example:
+/- 64 32 16
+75 = 0 1 0 0
8
1
4
0
2
1
1
1
-75 = 1
1
0
1
1
1
0
0
This is called sign/magnitude representation – the byte is in 2
parts, the sign (+/-) and the size of the number.
A-Level Computing#BristolMet
2s Complement
There are 2 problems with sign & magnitude representation.
Firstly, the biggest number that can be represented is now
halved – 127 instead of 255. Secondly, arithmetic now made more
complicated as different bits means different things.
A solution to this is using a system called 2s complement – the
last bit stands for -128. So the diagram looks like this:
-128 64 32 16
8
4
2
1
So -75 in 2s complement is:
-128 64 32 16
-75 =
1 0 1 1
A-Level Computing#BristolMet
8 4 2 1
0 1 0 1
Subtraction in binary
Now using 2s complement subtraction is easier because 75 – 14 is
the same as 75 + (-14)
-128
75 =
0
-14 =
1
0
Carry
1
64
1
1
0
32
1
1
1
16
0
1
1
8 4 2 1
1 0 1 1
0 0 1 0
1 1 0 1
1
= 61
Now attempt
a) 97 + 23
b) 43 – 58
Some more examples:Click Here
Why binary arithmetic – Watch this video
A-Level Computing#BristolMet