To Documents

Binary and Hex Numbers

Inside a computer, integer values are stored as binary numbers.

Integer values are usually input into a computer as decimal numbers but, as we will see below, these values can also be input in hex (base 16) or binary (base 2).

In the explanations that follows, an means a to the nth power (multiply a by itself n times).

  1. Converting from Binary to Decimal
    First memorize this Powers of Two table, up to 27 = 128.
    In IT 212, we will only be working with one byte integers with powers of two up to 128.

    Example 1: Convert 00010110 (binary) to decimal.

    Solution: Each binary digit (called a bit) represents a power of two:
    Power     7    6    5    4 3 2 1 0
    Power of Two 128 64 32 16 8 4 2 1
    Digit     0    0    0    1 0 1 1 0

    Therefore, the value in decimal is
  2. Converting from Decimal to Binary

    Example 2:  Convert 22 (decimal) to binary.

    Solution: Repeatedly subtract the largest power of two that can be subtracted, starting with 22. Therefore 22 can be written as 22 = 16 + 4 + 2. Then

    This binary value is written in Python as 0b00010110.
  3. Converting from Hex to Binary

    First memorize this Hex Digits table:
    Then for each hex digit, replace it by the 4 bits in the binary column.

    Example 3: Convert 6d (hex) to binary:
    Solution: 6 → 0110 and d → 1101, so 6d → 01101101.
  4. Converting from Binary to Hex

    Group the binary bits into groups of 4. Then translate each group of 4 bits (called a nybble!) into hex. Each nybble is equivalent to one hex digit.

    Example 4: Convert 01101101 (binary) to hex:

    Solution: 01101101
                 0110   1101
                    6        d
                       6d.
    The hex value 6d is written in Python as 0x6d.
  5. Using Bitwise Operators

    A mask is a binary number that can be used together with bitwise operators (&, |, and ^) to manipulate binary data. Binary data is usually expressed in hex because converting between hex and binary is very easy. Here are some bitwise operators and Python code that manipulates them:
    1. Bitwise Or

      The symbol for bitwise or is vertical line ( | ).
      Here is the operation table for bitwise or:

      Op 1 Op 2 Result
      0 0 0
      0 1 1
      1 0 1
      1 1 1

      Example 5: Compute the bitwise or of 0x6b and 0x52.
      Hex          Binary
      ====        ========
      0x6B  --->  01101011
      0x52  --->  01010010
                  --------
      0x7b  <---  01111011
      
      Python code that performs this computation:
      a = 0x6b
      b = 0x52
      c = a | b
      print("{0:02x}".format(c))
      
    2. Bitwise And
      The symbol for bitwise and is ampersand ( & ).
      Here is the operation table for bitwise and:

      Op 1 Op 2 Result
      0 0 0
      0 1 0
      1 0 0
      1 1 1

      Example 6: Compute the bitwise and of 0x6B and 0x52.
       
      Hex          Binary
      ====        ========
      0x6B  --->  01101011
      0x52  --->  01010010
                  --------
      0x42  <---  01000010
      
      Python code that performs this computation:
      a = 0x6b
      b = 0x52
      c = a & b
      print("{0:02x}".format(c))
      
    3. Bitwise Exclusive Or
      The symbol for bitwise exclusive or (XOr) is caret ( ^ ).
      The operation table for bitwise exclusive or:
      Op 1 Op 2 Result
      0 0 0
      0 1 1
      1 0 1
      1 1 0

      Example 7: Compute the bitwise and of 0x6B and 0x52.
      Hex          Binary
      ====        ========
      0x6B  --->  01101011
      0x52  --->  01010010
                  --------
      0x39  <---  00111001
      
      Python code that performs this computation:
      a = 0x6b
      b = 0x52
      c = a ^ b
      print("{0:02x}".format(c))
      
  6. Uses for Bitmaps

    A bitmap is a sequence of bits stored in one or more binary integers. Here are some of the uses of bitmaps: to store the
  7. Using Binary Masks to Manipulate Data
  8. Representing Negative Numbers in Binary
  9. Binary negative numbers are represented in twos-complement notation. An explicit negative sign is not used. Use this algorithm to represent the negative number -a in binary:

    Twos-complement Algorithm 1 (Decimal to Binary)
    1. Convert the positive number +a to the binary representation b.
    2. Complement the binary representation of b (change each 1 to 0 and each 0 to 1).
    3. Searching from the right, find the first 0 bit.
    4. Change this 0 bit to 1.
    5. Change all of the 1 bits to the right of the newly changed bit to 0.

    Example: Represent -48 in twos-complement notation.
    1. Convert +48 to binary: 00110000.
    2. Complement 00110000 → 11001111.
    3. The first 0 bit searching from the right is marked in red: 11001111. Change it to 1: 11011111.
    4. Change all the bits to the left of the red bit to 0: 11010000.

    This answer of 11010000 can also be written in hex: d0.

    Note: If the binary representation of a number starts with 0, the number is positive. If the binary representation starts with 1, the number is negative.

    Use the following algorithm to convert a negative binary number b to decimal:
    Twos-complement Algorithm 2 (Binary to Decimal)
    1. Complement the binary number b.
    2. Searching from the right, find the first 0 bit.
    3. Change this 0 bit to 1.
    4. Change all of the 1 bits to the right of the newly changed bit to 0.
    5. Convert the resulting binary number to the decimal number a.
    6. The original negative binary number represents -a.

    Example: Convert the negative binary number 11110100 to decimal.
    1. Complement 11110100 → 00001011.
    2. The first 0 bit searching from the right is marked in red: 00001011. Change it to 1: 00001111.
    3. Change all the bits to the left of the red bit to 0: 00001100.
    4. Convert 00001100 to decimal: 12.
    5. The original negative binary number 11110100 is -12 in binary.