Lecture 9 – Hex notation

advertisement
Lecture 9 – Hex notation
Hexadecimal notation is simply Base 16, you really need to understand several
concepts before, discussing the Atmel assembler (next lecture). The assembler
concept is based round binary (base 2) but often the information is presented in HEX
(Base 16). This is to prevent users having to read long strings of 0’s and 1’s.
You need to understand



Base 2 representation (including 2’s complement) (last lecture)
Base 16 representation
Convert from
o HEX to BINARY
o BINARY to HEX
BASE 16 representation
Base 16 (HEX) is similar to BASE 2 (Binary) and Base 10 (decimal)
Base 2 has 2 digits and uses the digits ( 0, 1)
Base 10 has 10 digits and uses the digits ( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
Base 16 has 16 digits and uses the digits ( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F)
X2
The columns in Base 2 go
x8
X2
x4
x
The columns in Base 10 go
x
x2
x
x
X
The columns in Base 16 go
x
x1
x
x10
X
x
53
X2
x1
X
x
x1
Idiots conversion table for Binary/HEX/Decimal
Decimal
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Binary (*1)
0000
0001
0010
0011
0100
0101
0110
0111
1000
1001
1010
1011
1100
1101
1110
1111
HEX
0
1
2
3
4
5
6
7
8
9
A
B
C
D
E
F
(*1) We would not normally show leading zero’s but this will help with HEX/Binary
Binary/HEX conversion later
54
X65,536
(i)
(ii)
(iii)
(i)
2
X4,096
X256
F
1
0
X16
B
2
0
X1
7
E
E
REMEMBER that B = 11 (see table page 54)
(11 x 16) + (7 x 1) = 183
Hence convert
(ii)
(iii)
HEX Binary Conversion
As 16 is a multiple of 2 e.g. 16 = 2 4 , there is an easy method to convert from HEX to
Binary, each HEX digit can essentially be replaced with a group of 4 binary digits
Binary (*1)
0000
0001
0010
0011
0100
0101
0110
0111
1000
1001
1010
1011
1100
1101
1110
1111
HEX
0
1
2
3
4
5
6
7
8
9
A
B
C
D
E
F
Convert 7F to binary
From table
7
F
0111
1111
Hence 7F becomes 01111111 or
Convert the following to binary
1111111 if we ignore leading zero’s
55
2B
1FD
Converting Binary into HEX
This is simply the reverse of HEX/Binary conversion. Still using the table we must
divide the Binary number into groups of 4 digits and then convert these groups into
HEX digits. There are, however, two simple rules


Always start grouping from the least significant end of the string
If you are left with a group of less than 4 digits add zeros to the most
significant part of the group until it becomes a 4 digit number.
Convert
1 1 0 0 1 1 1 1 0 1 into HEX
Divide into groups of 4
11001101101
Padding out the last group with a zero we get
01 1 0 0 1 1 0 1 1 0 1
giving
6
6
D
Hence convert the following strings
10011111
111110011
11111111111111111
HEX Addition
56
Start groups of
4 from here
X256
+
x16
x1
A
9
A
9
1
2
Basically the same as addition in decimal except that when the total comes to 16 or
more ‘1’ is carried over to the next column
Note the following 9 + 9 = 18, which is too big for HEX, we therefore carry 16
over to the next column (18 – 16 = 2) (which is placed in the X1 column)
Notice when 16 is carried over it becomes ‘1’ in the x16 column
Complete the addition remember A = 10 in decimal
Convert to decimal to check your answer
HEX subtraction
Original sum was
x16
A
B
X16
x1
1
0
-
-
9
7
1
9
7 from 0 will not go therefore 16 is carried over from the x16 column
notice the ‘B’ (11) becomes ‘A’ (10)
notice also that the ‘10’ in the x1 column = 16
7 from 16 = 9 (written in the x1 column)
Finally in the x16 column 9 from A (10) = 1
57
x1
B
0
9
7
Complete the following HEX sums
X256
+
x16
x1
2
C
3
F
B
7
decimal
check your answers by converting to decimal
X256
-
x16
x1
2
C
3
1
C
7
check your answers by converting to decimal
58
decimal
Download