# 1.2: Converting Binary, Decimal, and Hex Numbers

## 1.2.1 Converting Binary to Decimal

Computers think in 0's and 1's, and when dealing with the internal workings of a computer humans must adjust to the computers mindset. However when the computer produces answers, the humans that use them like to think in decimal. So it is often necessary for programmers to be able to convert between what the computer wants to see (binary), and what the human end users want to see (decimal). These next 3 sections will deal with how to convert binary to decimal, and then give 2 ways to convert decimal to binary. Finally it will give a section on a useful representation for handling large binary numbers called hexadecimal.

To convert binary to decimal, it is only necessary to remember that each 0 or 1 in a binary number represents the amount of that binary power of 2. For each binary power of 2, you have either 0 or 1 instance of that number. To see this, consider the binary number 10010102. This number has 1 * 26 + 0 * 25 + 0 * 24 + 1 * 23 + 0 * 22 + 1 * 21 + 0 * 20 = 64 + 8 + 2 = 7410. This can be generalized into an easy way to do this conversion. To convert from binary to decimal put the 2n value of each bit over the bits in the binary number and add the values which are 1, as in the example below:

 64 32 16 8 4 2 1 10010102 = 1 0 0 1 0 1 0 = 64 + 8 + 2 = 7410

## 1.2.2 Converting Decimal to Binary using Binary Powers

Two ways to convert decimal number to binary numbers are presented here. The first is easy to explain, but harder to implement. The second is a cleaner algorithm, but why the algorithm works is less intuitive.

The first way to convert a number from decimal to binary is to see if a power of 2 is present in the number. For example, consider the number 433. We know that there is no 29 (512) value in 433, but there is one value of 28 (or 256). So in the 9th digit of the base 2 number we would put a 1, and subtract that 256 from the value of 433.

 433 - 256 = 177 28 27 26 25 24 23 22 21 20 1 - - - - - - - -

Next check if there is a 27 (128) value in the number. There is, so add that bit to our string and subtract 128 from the result.

 177 - 128 = 49 28 27 26 25 24 23 22 21 20 1 1 - - - - - - -

Now check for values of 26 (64). Since 64 > 49, put a zero in the 26 position and continue.

 49 - 0 = 49 28 27 26 25 24 23 22 21 20 1 1 0 - - - - - -

Continuing this process for 25 (32), 24(16), 23(8), 22(4), 21(2), and 20(1) results in the final answer.

 49 - 0 = 49 28 27 26 25 24 23 22 21 20 1 1 0 1 1 0 0 0 1

Thus 43310 = 1101100012. This result can be checked by converting the base 2 number back to base 10.

## 1.2.3 Converting Decimal to Binary using Division

While conceptually easy to understand, the method to convert decimal numbers to binary numbers in Chapter 1.2.3 is not easy to implement as the starting and stopping conditions are hard to define. There is a way to implement the conversion which results in a nicer algorithm.

The second way to convert a decimal number to binary is to do successive divisions by the number 2. This is because if a number is divided and the remainder taken, the remainder is the value of the 20 bit. Likewise if the result of step 1 is divided again by 2 (so essentially dividing by 2*2 or 4), the reminder is the value of the 21 bit. This process is continued until the result of the division is 0. The example below shows how this works.

Start with the number 433. 433 divided by 2 is 216 with a remainder of 1. So in step 1 the result would have the first bit for the power of 2 set to one, as below:

 433 / 2 = 216 r 1 28 27 26 25 24 23 22 21 20 - - - - - - - - 1

The number 216 is now divided by 2 to give 108, and the remainder, zero, placed in the second bit.

 216 / 2 = 108 r 0 28 27 26 25 24 23 22 21 20 - - - - - - - 0 1

The process continues to divide by 2, filling the remainder in each appropriate bit, until at last the result is 0, as below.

 28 27 26 25 24 23 22 21 20 1 1 0 1 1 0 0 0 1

## 1.2.4 Converting between binary and hexadecimal

One of the biggest problems with binary is that the numbers rapidly become very hard to read. This is also true in decimal, where there is often a "," inserted between groupings of 103. So for example 1632134 is often written as 1,632,134, which is easier to read.

In binary, something similar is done. Most students are familiar with the term byte, which is 8 bits. But fewer know of a nybble, or 4 bits. 4 bits in binary can represent numbers between 0..15, or 16 values. So values of 4 bits are collected together and create a base 16 number, called a hexadecimal (or simply hex) number. To do this, 16 digits are needed, and arbitrarily the numbers and letters 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, and F were chosen as the 16 digits. The binary numbers corresponding to these 16 digit hex numbers are given in the table below (note, the normal way to indicate a value is in hex is to write a 0x before it So decimal 10 would be 0xA).

 Binary Number Hex Digit Binary Number Hex Digit Binary Number Hex Digit Binary Number Hex Digit 0000 0x0 0001 0x1 0010 0x2 0011 0x3 0100 0x4 0101 0x5 0110 0x6 0111 0x7 1000 0x8 1001 0x9 1010 0xA 1011 0xB 1100 0xC 1101 0x D 1110 0xE 1111 0xF

The hex numbers can then be arranged in groups of 4 (or 32 bits) to make it easier to translate from a 32 bit computer.

Note that hex numbers are normally only used to represent groupings of 4 binary digits. Regardless of what the underlying binary values represent, hex will be used just to show what the binary digits are. So in this text all hex values will be unsigned whole numbers.

Most students recognize that a decimal number can be extended by adding a 0 to the left of a decimal number, which does not in any way change that number. For example 0043310 = 043310 = 43310. The same rule applies to binary. So the binary number 1101100012 = 0001101100012.

But why would anyone want to add extra zeros to the left of a number? Because to print out the hex representation of a binary number, I need 4 binary digits to do it. The binary number 1101100012 only has 1 binary digit in the high order byte. So to convert this number to binary it is necessary to pad it with left zeros, which have no effect on the number. Thus 1 10011 00012 = 0001 1011 00012= 0x1B1 in hex. Note that even the hex numbers are often paded with zeros, as the hex number 0x1B1 is normally be written 0x01B1, to get groupings of 4 hex numbers (or 32 bits).

It is often the case where specific bits of a 32 bit number need to be set. This is most easily done using a hex number. For instance, if a number is required where all of the bits except the right left most (or 1) bit of a number is set, you can write the number in binary as:

111111111111111111111111111111102

A second option is to write the decimal value as: 429496729510

Finally the hex value can be written as 0xFFFFFFFE

In almost all cases where specific bits are being set, a hex representation of the number is the easiest to understand and use.