Section 4.2
4 Data Representation
4.2 Binary Integers
Most computer systems allow positive (or negative) integer nnumbers to be
stored.
4.2.1 Introduction
For input and output, ASCII codes are used.
ASCII codes are fine for input and output but useless for arithmetic because:
- The numbers would occupy too much memory.
- Two numbers stored character by character are difficult to add.
In the binary system we move right to left, the value of each digit been twice
that of the previous one.
Binary To Decimal
We can set out the binary number 1001 0101 under these column headings.
27
|
26
|
25
|
24
|
23
|
22
|
21
|
20
|
128
|
64
|
32
|
16
|
8
|
4
|
2
|
1
|
1
|
0
|
0
|
1
|
0
|
1
|
0
|
1
|
128 +
|
0 +
|
0 +
|
16 +
|
0 +
|
4 +
|
0 +
|
1 =
|
149
|
Decimal To Binary
Subtract the highest power of two possible from the denary number and place a
'1' under the column for that power of two. Take the remainder and repeat until
there is no remainder.
4.2.2 Binary Arithmetic (Addition)
To see the carry system works in binary we examine the first eight numbers:
Decimal
|
Binary
|
0
|
0000
|
1
|
0001
|
2
|
0010
|
3
|
0011
|
4
|
0100
|
5
|
0101
|
6
|
0110
|
7
|
0111
|
8
|
1000
|
We can now try some binary arithmetic:
|
27
|
26
|
25
|
24
|
23
|
22
|
21
|
20
|
|
128
|
64
|
32
|
16
|
8
|
4
|
2
|
1
|
137
|
1
|
0
|
0
|
0
|
1
|
0
|
0
|
1
|
+ 44
|
0
|
0
|
1
|
0
|
1
|
1
|
0
|
0
|
|
|
|
|
1
|
|
|
|
|
181
|
1
|
0
|
1
|
1
|
0
|
1
|
0
|
1
|
The most significant bit (msb) or leftmost bit can be used as a sign bit.
If the msb is 0 then the number is positive. If the the msb is 1 then the number
is negative.
This is called the sign and magnitude representation of binary numbers.
4.2.4 Two's Complement
Two's complement can be thought of as working like the odometer in a car.
Decimal
|
Binary
|
-4
|
1111 1100
|
-3
|
1111 1101
|
-2
|
1111 1110
|
-1
|
1111 1111
|
0
|
0000 0000
|
1
|
0000 0001
|
2
|
0000 0010
|
3
|
0000 0011
|
4
|
0000 0100
|
Converting From Negative Denary To Binary Two's Complement
Step 1
Find the binary value of the equivalent positive denary
number.
|
0 0 0 0 0 1 1 0
|
Step 2
Change 0s to 1s and 1s to 0s (complement).
|
1 1 1 1 1 0 0 1
|
Step 3
Add 1 to the result.
|
1 1 1 1 1 0 1 0
|
Converting From Binary Two's Complement To Denary
|
1 1 1 1 1 0 1 1
|
Step 1
Complement the number.
|
0 0 0 0 0 1 0 0
|
Step 2
Add one add prefix a minus sign.
|
- 0 0 0 0 0 1 0 1
|
Step 3
Convert binary to decimal.
|
- 5
|
4.2.5 Binary Arithmetic (Subtraction)
To perform subtraction in binary, the number to be subtracted is made neagtive
and converted into Binary Two's Complement form. The two numbers are then added.
E.g. Subtract 12 from 15 in binary (1 byte).
12
|
0
|
0
|
0
|
0
|
1
|
1
|
0
|
0
|
-12
|
1
|
1
|
1
|
1
|
0
|
1
|
0
|
0
|
+ 15
|
0
|
0
|
0
|
0
|
1
|
1
|
1
|
1
|
|
1
|
1
|
1
|
1
|
1
|
|
|
|
3
|
0
|
0
|
0
|
0
|
0
|
0
|
1
|
1
|
4.2.6 Binary Multiplication
Multiplication can be acheived by adding the first multiplier the number of
times specified by the second multiplier.
E.g. 6 × 3 = 6 + 6 + 6 = 18