Skip to main content
Engineering LibreTexts

2.2: Translating Binary, Decimal, and Hex Numbers

  • Page ID
    76098
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    2.2.1 Translating Binary to Decimal

    Computers operate in 0's and 1's, therefore when dealing with the internal workings of a computer it is the humans who must adjust to how computers work. However, when the computer produces answers, the humans that use them like to think in decimal. So, it is the job of a programmer to be able to translate between what the computer uses (binary) and what the human end users want to see (decimal). These next sections will deal with how to translate binary to decimal and give 2 ways to translate decimal to binary. The section after that it will explain a useful representation for handling large binary numbers called hexadecimal.

    To translate 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 \(1001010_2\). This number has \((1 * 2^6) + (0 * 2^5) + (0 * 2^4) + (1 * 2^3) + (0 * 2^2) + (1 * 2^1) + (0 * 2^0)= 64 + 8 + 2 = 74_{10}\). This can be generalized into an easy way to do this conversion. To translate from binary to decimal put the \(2^n\) 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  
    \(1001010_2\) = 1 0 0 1 0 1 0 = 64 + 8 + 2

    2.2.2 Translating Decimal to Binary using Binary Powers

    Two ways to translate 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 translate 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 \(2^9\) (512) value in 433, but there is one value of \(2^8\) (or 256). So, in the 9th digit of the base 2 number (note: \(9^{\rm th}\) digit because the numbering is zero based) we would put a 1, and subtract that 256 from the value of 433.

    433 - 256 = 177

    \(2^8\) \(2^7\) \(2^6\) \(2^5\) \(2^4\) \(2^3\) \(2^2\) \(2^1\) \(2^0\)
    1 - - - - - - - -

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

    177 - 128 = 49

    \(2^8\) \(2^7\) \(2^6\) \(2^5\) \(2^4\) \(2^3\) \(2^2\) \(2^1\) \(2^0\)
    1 1 - - - - - - -

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

    49 - 0 = 49

    \(2^8\) \(2^7\) \(2^6\) \(2^5\) \(2^4\) \(2^3\) \(2^2\) \(2^1\) \(2^0\)
    1 1 0 - - - - - -

    Continuing this process
    for \(2^5\) (32), \(2^4\)(16), \(2^3\)(8),
    \(2^2\)(4), \(2^1\)(2), and \(2^0\)(1) results in the final answer.

    \(2^8\) \(2^7\) \(2^6\) \(2^5\) \(2^4\) \(2^3\) \(2^2\) \(2^1\) \(2^0\)
    1 1 0 1 1 0 0 0 1

    Thus \(433_{10} = 110110001_2\). This result can be checked by converting the base 2 number back to base 10.

    2.2.3 Translating Decimal to Binary using Division

    While conceptually easy to understand, the method to translate decimal numbers to binary numbers in Chapter 1.2.2 is not easy to implement as an algorithm since the starting and stopping conditions are hard to define. There is a way to implement the translation from Base 10 to Base 2 which results in a nicer algorithm.

    This second method 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 \(2^0\) bit. Likewise, if the result of the division in step 1 is divided again by 2 (so essentially dividing by 2*2 or 4), the reminder is the value of the \(2^1\) 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\)

    \(2^8\) \(2^7\) \(2^6\) \(2^5\) \(2^4\) \(2^3\) \(2^2\) \(2^1\) \(2^0\)
    - - - - - - - - 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

    \(2^8\) \(2^7\) \(2^6\) \(2^5\) \(2^4\) \(2^3\) \(2^2\) \(2^1\) \(2^0\)
    - - - - - - - 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.

    \(2^8\) \(2^7\) \(2^6\) \(2^5\) \(2^4\) \(2^3\) \(2^2\) \(2^1\) \(2^0\)
    1 1 0 1 1 0 0 0 1

    Note that this algorithm gives the same result as the algorithm in Chapter 2.2.2. Why it works will be obvious later in the chapter when multiplication is covered.

    2.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 \(10^3\). 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 0xD   1110 0xE   1111 0xF

    Table 2-4: Binary to Hexadecimal Conversion

    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 \(00433_{10} = 0433_{10} = 433_{10}\). The same rule applies to binary. So, the binary number \(110110001_2 = 000110110001_2\).

    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, 4 binary digits are needed to do it. The binary number \(110110001_2\) 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 0001_2 = 0001 1011 0001_2=\) 0x1B1. Note that even the hex numbers are often padded 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 that 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 most (or 1) bit of a number is set, you can write the number in binary as:

    \(11111111111111111111111111111110_2\)

    A second option is to write the decimal value as: \(4294967295_{10}\)

    Finally, the hex value can be written as 0xFFFF FFFE

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


    This page titled 2.2: Translating Binary, Decimal, and Hex Numbers is shared under a CC BY 4.0 license and was authored, remixed, and/or curated by Charles W. Kann III via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.

    • Was this article helpful?