Binary Representation of Numbers
Binary Numbers are numbers represented with 0's and 1's.
They work much the same way as our normal decimal numbers except instead of 10 digits (0 to 9) there are only 2 digits (0 and 1).
Decimal number (subscript indicates base 10):
8401.32= 8 x 1000 + 4 x 100 + 0 x 10 +1 x 1 + 3 x 1/10 + 2 x 1/100
3 x 10-1 + 2 x 10-2
Binary number (subscript indicates base) :
1101.012 = 1 x 8 + 1 x 4 + 0 x 2 + 1 x 1 + 0 x 1/2 + 1 x 1/4
which in base 10 is
Practice: What is 100110.12 in base 10?
How do you add numbers in binary?
Representing Negative Integers: 2's Complement
(Integers are whole numbers - there is no decimal component. Note that negative decimal numbers will be treated very differently.)
A bit is either 0 or 1. There is no symbol for "-" so we need a way of representing negative numbers. Suppose we have 4 bits to work with. That gives is 24 = 16 possible combinations. Let's also say we want roughly half to be positive and half to be negative.
One possibility is to have the leftmost bit be 0 if the number is positive and 1 otherwise (sign magnitude). The remaining part of the number will represent the absolute value of the number in standard binary form.
Hex | All Positive |
Binary
|
Sign Magnitude
|
2's Complement
|
0
1
2
3
4
5
6
7
8
9
A
B
C
D
E
F
|
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
|
0
1
2
3
4
5
6
7
-0
-1
-2
-3
-4
-5
-6
-7
|
0
1
2
3
4
5
6
7
-8
-7
-6
-5
-4
-3
-2
-1
|
An alternative is 2's Complement: Here, negative numbers are obtained as follows:
- write magnitude in binary
- invert all bits (make sure leading zeros are also inverted)
- add 1
- Example: -5 assuming 4-bit numbers.
- magnitude: 0101
- invert: 1010
- add 1: 1011
What are advantages of 2's complement over sign magnitude?
- In sign magnitude, zero is represented in 2 ways so we have one less number that can be represented. Double representation can also cause problems when comparing numbers; numbers may be the same but look different.
- Addition and subtraction are very easy in 2's complement. To compute 6-2, we just add 6 to -2 using normal addition dropping the leftmost carry bit if there is any carry:
01102 = 611102 = -2---------------01002 = 4
Hexadecimal (Hex) Format
Often we write binary numbers in hex format. This is because
- It is very easy to convert between binary and hex
- It is a lot easier for a human to read hex than binary because there are many fewer digits.
What is Hex?
Hex is base 16. The "digits" are 0,1,...9,A,B,C,D,E,F.Examples:1016 = 1610CA16 = 12 x 161 + 10 x 160 = 20210
Converting between Binary and Hex:
4 bits represent 16 numbers, therefore a single hex digit can be represented by exactly 4 binary digits (see table above).1110 1010 0010 00012 = EA2116
No comments:
Post a Comment