Binary Lesson 5 – Hexadecimal

September 3, 2014
lesson05Computers love strings of bits, such as 10001001 01100111 10101110, but they can be difficult for humans to read and write accurately.

To facilitate readability and conserve space when viewed in a monitor or in print, we use the hexadecimal (base 16) system as a shorthand to represent binary.

 


Hexadecimal, or hex, is a base 16 number system that contains sixteen digits.

0 1 2 3 4 5 6 7 8 9 A B C D E F

For digits 0 through 9, we use the same numbers as we do in the decimal system, but that only provides digits for the first ten values. We need six more, so we use the first six letters from the English alphabet to represent the hexadecimal values 10 through 15.

Base 16 relies upon a 4-bit system because 16 unique combinations of bit patterns are possible. 2^4 = 16. Each bit pattern corresponds to exactly one 4-bit pattern.

Binary    Hex Value   Decimal
-----------------------------------
0000      0           0
0001      1           1
0010      2           2
0011      3           3
0100      4           4
0101      5           5
0110      6           6
0111      7           7
1000      8           8
1001      9           9
1010      A           10
1011      B           11
1100      C           12
1101      D           13
1110      E           14
1111      F           15

To convert from binary to hexadecimal, divide the binary bit pattern, from right to left, into 4-bit groups, and replace each group with its corresponding hex value. Pad with leading zeros if necessary to aid readability.

Convert Binary 10101110 into Hex

This has eight bits, so split the byte into two 4-bit groups. Keep the bit ordering the same.

1010 1110

Replace each group with its hex value.

1010 1110
   A    E

AE is the hex equivalent of binary 10101110.

Convert Binary 110011 to Base 16

From right to left, split 110011 into two 4-bit groups.

11 0011

If a group has fewer than four bits, pad it with leading zeros to its left. Leading zeros do not alter the value.

11 --> 0011 0011

Replace each 4-bit group with the corresponding hex value.

0011 0011
   3    3

33 is the hex equivalent of binary 110011.

Denoting Hexadecimal

In the previous example, how do we know that 33 is hexadecimal and not 33 in base 10? The two have different values even though they look the same, so we need a way to differentiate them. There are four methods used to label a number as hexadecimal in order to avoid ambiguity.

1.  Place 0x before the hex numeral:  0x33
2.  Append a lowercase letter h after the numeral: 33h
3.  Use the 16 subscript: 3316
4.  Specify base 16: 33 (base 16)

The 0x notation is the most common. Use one 0x for each hex grouping. 0x33, 0xF, 0xFFEE, are 0xAB66CD90 are all valid examples. However, do not prefix 0x separately to each digit, such as 0x3 0x3 to represent hex 33. Use 0x33.

 

Capitalization

Uppercase and lowercase hex digits have the same meaning.

 

 0xAB56 = 0xab56
 0x9F   = 0x9f
 0x33b  = 0x33B

The x in 0x may also be capitalized, but this has no effect on the value of the hex number either.

 0xBC342A = 0XBC342A
 0xbc342a = 0Xbc342a

In practice, the lowercase x in 0x is more common and more readable.

Consistency

Be consistent with the chosen capitalization. Do not mix uppercase and lowercase letters together within the same hex grouping. For example,

 Write 0xAB3C or 0xab3c, not 0xaB3C.
 Write 0xCC, not 0xcC or 0xCc.
 Write 0x2b3f4d, not 0x2B3f4d.

 

Converting Hexadecimal to Binary

Each hex digit represents one 4-bit group, so replace each hex digit with its binary value. Keep the order of the bits the same as the order of the hex digits.

 

Convert 0x34 to binary

 3 = 0011
 4 = 0100

Consult the hexadecimal table above. For 0x34, 3 is replaced with 0011 and 4 is replaced with 0100. Maintain the order.

0x34 --> 0011 0100
0x34 = 00110100 binary

 

Convert 0xBC5 to binary

 B = 1011
 C = 1100
 5 = 0101

Case does not matter, so B and C are the same as b and c. Replace the hex digits with their binary values in the same order of appearance.

0xBC5 = 1011 1100 0101 = 101111000101 base 2

 

Converting Hex to Decimal

First, convert from hex to binary, and then convert the binary to decimal.

 

Convert 45h to decimal

 4 = 0100
 5 = 0101

Once we have converted 45h to binary 0100 0101, we perform the same binary-to-decimal conversion as described in Lesson 4.

45h = 01000101 (binary) = 64 + 4 + 1 = 69 (decimal)

 

Convert c3816 to decimal

The subscript 16 in c3816 specifies base 16, so we know that c38 is a hexadecimal value.

 c = 1100
 3 = 0011
 8 = 1000
c3816 = 1100 0011 10002 = 3,12810

 

The subscript 2 in indicates base 2 and the subscript 10 indicates base 10.

 

Readability

For bit strings containing more than eight bits, recognition is easier by preserving the spaces between nibbles or bytes. The place values still increment across the spaces.

0001 1101 1100 1100 is easier to read than 0001110111001100.

10011001 11110000 10100000 11110001 is slightly easier to read than 10011001111100001010000011110001.

 

Memorizing the Hex Digits

For faster mental calculations, we can memorize the sixteen hexadecimal values, their bit patterns, and their decimal equivalents. This is easier than it sounds.

The Extremes

The two extremes, all zeros and all ones, are simple. Zero is zero no matter the number system, so a 4-bit hex system always produces zero.

0h = 0000 binary = 0 decimal

The maximum value represented with four bits is 15 (base 10) or 1111 (base 2). Associate 0xF with 1111 (base 2) and 15 (base 10).

0xf = 1111 (binary) = 15 (decimal)

The First Ten

The first ten hex digits, 0 to 9, are the same as decimal. We already know that 0h is zero, so that leaves us with nine to learn. The binary equivalents of 1 to 9 are easily calculated mentally by converting from decimal to binary since we can easily add 1, 2, and 4 in our heads. The number 8 is needed twice to find the values for 8d and 9d.

 0x0 00002 010
 0x1 00012 110
 0x2 00102 210
 0x3 00112 310
 0x4 01002 410
 0x5 01012 510
 0x6 01102 610
 0x7 01112 710
 0x8 10002 810
 0x9 10012 910
 0xF 11112 1510

 

If 0x7 poses a problem, always remember that decimal 7 in binary is three bits, the maximum value expressed in a 3-bit system.

The Letter Digits

These are the last six digits, A to F. 0xA is the first hex digit that begins with a letter, so memorize 1010 (binary) for 0xA and 10 (decimal), and use 1010 (binary) as an anchor point to divide the “number range” (0 to 9) from “letter range” (A to F).

So far, we have learned twelve of the sixteen hex digits: 0 to 9, A, and F. The remaining four — B, C, D, E — are the only patterns that require rote memorization. Again, use 0xA as an anchor point. If 1010 (binary) is 0xA, then add 1 to it to produce 1011 (binary), which is 0xB, and so on until 1111b is reached.

With practice, these sixteen values with their associated bit patterns and decimal values will become second nature.

 

Practice

Let’s practice!

Set 1

Convert each of the hex numbers into binary.

  1. 0x33
  2. 45h
  3. 100h
  4. 010116
  5. 0xabcdef

 Set 1 Answers

  1. 00110011
  2. 01000101
  3. 000100000000
  4. 0000 0001 0000 0001 or 0001 0000 0001 (The leading zeros may be dropped, but not the inner zeros.)
  5. 1010 1011 1100 1101 1110 1111 (See the pattern?)

 

Set 2

Convert the following binary numbers to hexadecimal.

  1. 1010
  2. 110011
  3. 11111000011110001

 

Set 2 Answers

  1. 0xa
  2. 110011 –> 0011 0011 –> 33h (Split into groups of four bits from the right and pad with leading zeros before conversion for clarity.)
  3. 1 1111 0000 1111 0001 (Divide into groups of four bits starting from the least significant bit) –> 0001 1111 0000 1111 0001 (Pad with leading zeros so all groups have four bits.)
      0001 1111 0000 1111 0001
         1    f    0    f    1

      Result:
      11111000011110001 = 0001 1111 0000 1111 0001 = 0x1f0f1

Set 3

Convert the following hex numbers into decimal.

  1. 3h
  2. 0xb
  3. 3316
  4. 100 (base 16)
  5. 0x87
  6. 0xa11f (Calculator allowed for binary to decimal conversion)
  7. 0xffffffff (Calculator allowed for binary to decimal conversion)

 

Set 3 Answers

  1.  3 (decimal, same as hex number)
  2. 11 (decimal, look up the chart)
  3. 33h = 0011 0011 = 51 (decimal, this requires binary to decimal conversion instead of simple lookups)
  4. 100 (base 16) = 100000000 (base 2) = 256 (decimal)
  5. 0x87 = 1000 0111 = 135 (decimal)
  6. 0xa11f = 1010 0001 0001 1111 = 41,247 (decimal)
  7. 0xffffffff = 11111111 11111111 11111111 11111111 = 4,294,967,295 (decimal)

Take note of 6 and 7 above. These are large values expressed succinctly using hexadecimal. Often, hex numbers appear in even groups, such as 2, 4, 6, and 8. Number 6, groups of four hex digits, represents 16-bit binary values, and number 7, groups of eight hex digits, represents 32-bit binary values. Number 7 is a contrived problem to show the largest possible value that can be accessed using a 32-bit processor with a 32-bit address space. 32-bit values are often shown in groups of eight hexadecimal digits.

 

 

 

Advertisements

  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: