Binary Slides
Binary Slides
In a digital computer, all information is represented using the binary number system.
It is easy to build fast electronic circuits that operate on the basis of two states: on and off.
Number systems:
Addition:
9
9
4
4
2
2 +
1 9 1 1
1 1
2 + 9 = 1 * 10
1
+ 1 * 10
0
Carry
thousand
Carry
ten
2
Hundreds Tens Units
9
10
2
9 * 10
2
9
10
1
9 * 10
1
9
10
0
9 * 10
0
CSc 252 Computer Organization 2 Binary Numbers
Binary Basics (continued):
To convert to decimal, add the decimal values for each place that has a 1:
= 128 + 32 + 8 + 2 + 1 = 171ten.
Terminology:
The left most bit is called the most signicant bit, or MSB.
The right most bit is called the least signicant bit, or LSB.
CSc 252 Computer Organization 2 Binary Numbers
= 42
ten
= 31
ten
= 73
ten
0 0 1
1 1 1 1 1
0 1 0 1 0
0 0 1 0 1 1 1 1
0 1 0 0 1 0 0 1
+
= 1*2
5
+ 1*2
3
+ 1*2
1
= 1*2
4
+ 1*2
3
+ 1*2
2
+ 1*2
1
+ 1*2
0
= 1*2
6
+ 1*2
3
+ 1*2
0
1+1+1 = 1*2
1
+ 1*2
0
Binary Addition:
Computing devices can only represent binary numbers up to a xed number of bits.
Want to represent a range of positive numbers, and a range of negative numbers, and zero.
Ones Complement.
Twos Complement.
Range: -(2
N-1
- 1) .. +(2
N-1
- 1)
Problems:
Ones Complement:
Range: -(2
N-1
- 1) .. +(2
N-1
- 1)
Problem:
Twos Complement:
E.g.,
1 1 1 1 1 0 1 1 = -5ten
0 0 0 0 0 1 0 0 Invert step
+ 1 Add 1 step
0 0 0 0 0 1 0 1 = 5ten
E.g.,
0 0 0 0 1 0 0 0 = 8ten
1 1 1 1 0 1 1 1 Invert step
+ 1 Add 1 step
1 1 1 1 1 0 0 0 = -8ten
9
8-bit, Twos Complement Values 8-bit, Twos Complement Values
Positive Numbers Negative numbers
00000000 = 0ten
00000001 = 1ten
00000010 = 2ten
00000011 = 3ten
00000100 = 4ten
00000101 = 5ten
00000110 = 6ten
00000111 = 7ten
00001000 = 8ten
00001001 = 9ten
...
01111111 = ?ten
00000000 = -0ten
11111111 = -1ten
11111110 = -2ten
11111101 = -3ten
11111100 = -4ten
11111011 = -5ten
11111010 = -6ten
11111001 = -7ten
11111000 = -8ten
11110111 = -9ten
...
10000000 = -?ten
Only one zero. Apply Twos Complement to zero (16-bit version shown):
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Invert step
+ 1 Add 1 step
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
We lost the 1 that carried out past the left side in that addition (did you notice?).
However, the negative value 10000000 00000000 does not have an equivalent positive representation:
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 Invert step
+ 1 Add 1 step
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Often you will have a number that needs to be padded out to a larger number of bits. For example, an 8-bit value
to place in a 16-bit storage location; or, a 16-bit value in a 32-bit location; etc.
Put the number in the least signicant bits of the location and pad to the left by replicating the sign bit.
Addition:
1 1 1 1 1
0 0 0 1 1 1 1 1 = 31ten
+ 1 1 0 1 0 1 1 1 = + -41ten
1 1 1 1 0 1 1 0 = -10ten
Subtraction:
1 0 0 0 1 1 1 0 = -114ten
- 1 0 0 0 0 0 1 1 = - -125ten
= 11ten
10
0
1 0 0 0 0 1 1 = 11ten 0
0 10
1
12
CSc 252 Computer Organization 2 Binary Numbers
Binary Arithmetic (continued):
Easier approach: Change the subtrahend by nding its Twos Complement, then Add the two values:
1 0 0 0 1 1 1 0 = -114ten
- 1 0 0 0 0 0 1 1 = - -125ten
13
Find the twos complement of the subtrahend:
1 0 0 0 0 0 1 1
0 1 1 1 1 1 0 0 Invert step
+ 1 Add 1 step
0 1 1 1 1 1 0 1 Twos complement of subtrahend
Now, add the two numbers:
1 1 1 1 1 1
1 0 0 0 1 1 1 0
+ 0 1 1 1 1 1 0 1
0 0 0 0 1 0 1 1 Ignore the carry out
CSc 252 Computer Organization 2 Binary Numbers
Step 3:
pad with 0s to 16-bits
sign-bit for positive number:
0 0 0 0 0 0 0 0 1 0 0 0 0 1 1 1
Step 4:
Twos complement to get
negative number:
1 1 1 1 1 1 1 1 1 0 1 1 0 1 0 0
+ 1
1 1 1 1 1 1 1 1 1 0 1 1 0 1 0 1
Step 2:
= 1 0 0 1 0 1 1
75 2
37 2
18 2
9 2
4 2
2 2
1
1
1
0
1
0
0
1
2
0
Step 1:
Step 2:
= 1 0 0 0 0 1 1 1
135 2
67 2
33 2
16 2
8 2
4 2
2 2
1 2
0 1
0
0
0
0
1
1
1
Step 1:
14
Successive Division:
4-bit example:
1 0 0 0
* 0 1 1 0
0 0 0 0
1 0 0 0
1 0 0 0
0 0 0 0
0 1 1 0 0 0 0
In general, to multiply two N-bit binary numbers, you need to allow for up to 2 * N bits in the answer. For
example:
1 1 0 0
* 1 1 0 1
1 1 0 0
0 0 0 0
1 1 0 0
1 1 0 0
1 0 0 1 1 1 0 0
16
CSc 252 Computer Organization 2 Binary Numbers
Multiplication in Binary (continued):
Unsigned:
0 0 1 0 1 1 0 1 = 2
5
+ 2
3
+ 2
2
+ 2
0
= 32 + 8 + 4 + 1 = 45ten
0 1 0 1 1 0 1 0 = 2
6
+ 2
4
+ 2
3
+ 2
1
= 64 + 16 + 8 + 2 = 90ten
1 0 1 1 0 1 0 0 = 2
7
+ 2
5
+ 2
4
+ 2
2
= 128 + 32 + 16 + 4 = 180ten
Signed:
1 1 1 0 0 0 0 0 = -32ten
1 1 0 0 0 0 0 0 = -64ten
1 0 0 0 0 0 0 0 = ??
85ten / 6ten = 14 !
Signed number:
How does shift right maintain the correct result for dividing by 2?
No overow possible when adding a positive and a negative number: positive + negative negative + positive
No overow possible when the signs are the same for subtraction: positive - positive negative - negative
Overow: subtract a negative from a positive and get a negative (should be positive).
0 1 1 1 1 1 1 1 127ten
- 1 1 1 1 1 1 1 1 - -1ten
1 0 0 0 0 0 0 0 -> Twos complement for -128, but answer should be +128.
Overow: subtract a positive from a negative and get a positive (should be negative).
1 0 0 0 0 0 0 1 -127ten
- 0 0 0 0 0 1 0 1 - +5ten
0 1 1 1 1 1 0 0 -> Twos complement for +124ten, but answer should be -132ten
Sometimes, we do not!
Java example:
import java.io.*;
public class Zap {
public static void main (String args[]) {
int i, j;
for (i = 2147483640, j = 0; j < 15; i++, j++)
System.out.println("i = " + i + " j = " + j);
} // main
} // class Zap
Sometimes, we do
Duplicate versions of assembly language instructions that cause an exception when overow occurs.
Octal = base 8.
75,
377,
23
Binary Octal
000
001
010
011
100
101
110
111
0
1
2
3
4
5
6
7
CSc 252 Computer Organization 2 Binary Numbers
Hexadecimal and Octal (continued):
Group the binary number into groups of three digits, starting from the right.
Examples:
00 110 111two = 067octal
10 101 001two = 251octal
11 111 111two = 377octal
01 110 100 010 101 011 111 010 010 101 100two = ??
97,
2F9,
26
Binary Hexadecimal Binary Hexadecimal
0000
0001
0010
0011
0100
0101
0110
0111
0
1
2
3
4
5
6
7
1000
1001
1010
1011
1100
1101
1110
1111
8
9
A
B
C
D
E
F
CSc 252 Computer Organization 2 Binary Numbers
Hexadecimal and Octal (continued):
Group the binary number in groups of four digits, starting from the right.
Examples:
0100 1001two = 49hex
1111 1111two = FFhex
0110 1100two = 6Chex
1001 1000 1101 1011 1100 0100 0101 0011two =
9 8 D B C 4 5 3hex
0110 1101 1011 0110 1100 0100 1011 1110two = ??
2F 37 A0 C6hex = ??
27
CSc 252 Computer Organization 2 Binary Numbers
Hexadecimal and Octal (continued):
Complete the rows for 4 and 8 (they are useful) and F (for fun :-)
30
0 1 2 3 4 5 6 7 8 9 A B C D E F
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 0 1 2 3 4 5 6 7 8 9 A B C D E F
2 0 2 4 6 8 A C E 10 12 14 16 18 1A 1C 1E
3 0 3
4 0 4
5 0 5
6 0 6
7 0 7
8 0 8
9 0 9
A 0 A
B 0 B
C 0 C
D 0 D
E 0 E
F 0 F
CSc 252 Computer Organization 2 Binary Numbers
Floating Point (a brief look).
Review:
6.023 * 10
23
23 is the exponent.
Computer arithmetic that supports this form is called oating point arithmetic.
31
CSc 252 Computer Organization 2 Binary Numbers
Floating Point Representation:
Example: 176,128.75ten
0.75ten = " + # = 2
-1
+ 2
-2
= 0.11two
176,128.75ten = 10 1011 0000 0000 0000.11two = 1.0 1011 0000 0000 0000 11 * 2
17
If we have a xed amount of space (and we always do!), we have to trade off the number of bits among m and e:
Exponent.
With 8 bits for the exponent we can represent numbers from approximately 2
-127
.. 2
127
2
-127
$ 10
-38
; 2
127
$ 10
38
.
Mantissa.
Since the rst digit of the Mantissa is always 1 in binary (what is it in decimal?), we can optimize (save
space) by not storing it (but remembering it is there).
33
CSc 252 Computer Organization 2 Binary Numbers
S Ex p o n e n t S i g n i f i c a n d
Floating Point Representation (continued):
Basic layout:
Problem with comparing exponents: -127 appears to be larger than +127 (when both are written as Two's
Complement binary numbers).
Solution: bias the exponent so positive exponents occupy the upper-half of the exponent range.
Sign: 0 (positive)
Exponent:
17
Signicand (without the 1 to the left of the decimal): 010 1100 0000 0000 0011 0000
Result:
S Expo nent Sig nifi cand
0 1001 0000 010 1100 0000 0000 0011 0000
35
When converting from decimal to binary:
Add the bias (always).
Does not matter whether the exponent in
decimal is positive or negative, always add.
CSc 252 Computer Organization 2 Binary Numbers
Floating Point Representation (continued):
Exponent: 8 digits
Signicand: 23 digits
1.101two * 2
-23
1.101two = 1 + (1*2
-1
+ 0*2
-2
+ 1*2
-3
) = 1 + " + " = 1 + # = 1.625ten
Number: 1.625 * 2
-23
, which is approximately 1.937 * 10
-7
.
36
When converting from binary to decimal:
Subtract the bias (always).
Does not matter whether the exponent in
binary starts with 1 or 0, always subtract.
CSc 252 Computer Organization 2 Binary Numbers
Floating Point Representation (continued):
Zero: The standard uses all 0s for the exponent and all 0s for the signicand:
0 0000 0000 000 0000 0000 0000 0000 0000
S Expo nent Sig nifi cand
Innity: The standard uses all 1s for the exponent and all 0s for the signicand:
0 1111 1111 000 0000 0000 0000 0000 0000
S Expo nent Sig nifi cand
The standard uses all 1s for the exponent and some (any) nonzero value for the signicand.
0 1111 1111 101 0000 0000 0000 0000 0000 (as one example).
S Expo nent Sig nifi cand
Exponent:
If the signicand is nonzero, the signicand does not have an implied leading 1.
Example:
Floating point:
Exponent:
Signicand: 1.1
IEEE single precision result: 1 0111 1110 100 0000 0000 0000 0000 0000
1 01111110 10000000000000000000000
S Exponent Significand
39
CSc 252 Computer Organization 2 Binary Numbers
Floating Point Representation (continued):
Example:
4.2 * 10
3
into binary (IEEE format).
4.2 * 10
3
= 4200ten
Normalize it
Result:
Exponent:
000 0011 0100 0000 0000 0000, extra zeros needed on the right to ll out the 23 digits
0 1000 1011 000 0011 0100 0000 0000 0000
S Expo nent Sig nifi cand
40
CSc 252 Computer Organization 2 Binary Numbers
Floating Point Addition:
Have to make the exponents the same (align the oating decimal point).
Base 10 example:
4.2*10
3
+ 4.2*10
5
= 0.042*10
5
+ 4.2*10
5
= 4.242*10
5
Binary example:
We converted 4.2 * 10
3
earlier (see previous slide): 0 1000 1011 000 0011 0100 0000 0000 0000
4.2 * 10
5
= 420,000ten = 110 0110 1000 1010 0000two = 1.1001101000101*2
18
Result:
0 1001 0001 100 1101 0001 0100 0000 0000
S Expo nent Sig nifi cand
41
CSc 252 Computer Organization 2 Binary Numbers
Floating Point Addition (continued):
Adding:
0 1001 0001 100 1101 0001 0100 0000 0000 2
18
4.2 * 10
5
+ 0 1000 1011 000 0011 0100 0000 0000 0000 2
12
+ 4.2 * 10
3
Shift the second number six places to the right to align the exponents:
0 1001 0001 100 1101 0001 0100 0000 0000 2
18
4.2 * 10
5
+ 0 1001 0001 000 0010 0000 1101 0000 0000 2
18
+ 0.042 * 10
5
Have to put the implicit one into the second number when we shift it right!
Result:
0 1001 0001 100 1101 0001 0100 0000 0000 2
18
4.2 * 10
5
+ 0 1001 0001 000 0010 0000 1101 0000 0000 2
18
+ 0.042 * 10
5
0 1001 0001 100 1111 0010 0001 0000 0000
S Exponent Significand
Checking the result to see if it matches (approximates) the decimal result:
Exponent: 1001 0001 = 128 + 16 + 1 = 145ten - 127ten = 18ten
1.100 1111 0010 0001 * 2
18
= 110 0111 1001 0000 1000two = 67,908hex =
6*16
4
+ 7*16
3
+ 9*16
2
+ 8*16
0
= 6*65,536 + 7*4,096 + 9*256 + 8 =
393,216 + 28,672 + 2,304 + 8 = 424,200
42
CSc 252 Computer Organization 2 Binary Numbers
Floating Point (continued):
Single precision:
Sign bit.
Signicand: 23 bits.
Double precision:
Sign bit.
Signicand: 52 bits.
43
CSc 252 Computer Organization 2 Binary Numbers
Floating Point Complexities:
Operations are somewhat more complicated than integer operations (see text).
IEEE 754 uses two extra bits during operations: guard and round to round intermediate answers.
Other complexities
MIPS uses separate oating-point registers (not part of the 32 general-purpose registers that are used for integer
operations) see discussion in the text.
See the text (4th edition: pages 272-279) for description of the 80x86 oating-pt operations and the Pentium
divide bug!
44
CSc 252 Computer Organization 2 Binary Numbers
Representation of Characters:
Characters such as A ? p u 5 (and the characters in this document :-) must also be represented.
Standard representation (ASCII code) 4th edition: Figure 2.15, page 122.
0 1 0 0 0 0 0 1 = A
0 1 0 0 0 0 1 0 = B
0 1 1 0 0 0 0 1 = a
0 1 1 0 0 0 1 0 = b
Also have to represent numbers as characters; e.g. 5, but note these are not the same as 5.
5 (the character) = 0 0 1 1 0 1 0 1
Printable:
Non-Printable:
Characters from the nul character (ascii value 0) to the us character (us = unit separator, ascii value 31).
There are two ascii characters that can control advancing to the next line:
Windows puts both characters at the end of a line. In Java, when you use \n, you get both cr and lf.
Unix puts only the lf character at the end of a line. In Java, when you use \n, you get only lf.
Unicode.
Of course, 128 is not very many possibilities when you have to represent all the characters in the world.
2
16
allows for 65,536 characters.
The rst 128 characters in Unicode are the ASCII characters, of course. (Why of course?)
51
CSc 252 Computer Organization 2 Binary Numbers
Summary:
We always have to know how to interpret a bit pattern -- the pattern alone is not sufcient!
Variety of techniques for representing negative numbers, oating point numbers, etc.
Can (easily?) convert between more readable hexadecimal (or octal) and binary notation.
Often cannot tell from the pattern alone (see the homework).