To Documents

Binary and Hex Numbers

In the explanations that follow, 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, at least up to 27 = 128.

    In CSC 212, we will only be working with one byte integers (byte variable type) 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 of Two 7 6 5 4 3 2 1 0
    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 from 22.

          Power
          of 2
      22
    - 16    4
      --
       6
     - 4    2
      --    
       2
     - 2    1
      --
       0
    

    Therefore 22 can be written as 22 = 16 + 4 + 2. Then

  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 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 6D (hex) to binary:

    Solution: 01101101 --> 0110   1101 --> 6 D --> 6D.

    The hex value 6D is written as 0x6D in Java source code.

     

  5. Representing Negative Numbers in Binary

    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.

     

  6. Using Binary Operators

    A mask is a binary number that can be used together with bitwise operators (&, |, and ^) to manipulate binary data. Binary data in Java is usually expressed in hex because converting between hex and binary is very easy.

    Here are some bitwise Java operators and their Java implementations:

    1. Bitwise Or

      The symbol for bitwise or is vertical line ( | ).

      Here is the operation table for bitwise or.

      0 = False, 1 = True, Op1 = Operand 1, Op2 = Operand2.

      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
      

      Here is Java code that performs this computation:

      byte a = (byte) 0x6B;
      byte b = (byte) 0x52;
      byte c;
      
      c = (byte) (a | b);
      System.out.println(Integer.toHexString(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
      
      Here is Java code that performs this computation:

      byte a = (byte) 0x6B;
      byte b = (byte) 0x52;
      byte c;
      
      c = (byte) (a & b);
      System.out.println(Integer.toHexString(c));
      
    3. Bitwise Exclusive Or

      The symbol for bitwise exclusive or (XOr) is caret ( ^ ).

      Here is 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
      
      Sample Java Code that performs this computation:

      byte a = (byte) 0x6B;
      byte b = (byte) 0x52;
      byte c;
      
      c = (byte) (a ^ b);
      System.out.println(Integer.toHexString(c));
      
     

  7. Using Binary Masks to Manipulate Data

    1. Use a bitwise or mask (|) to turn on (change to 1) bits in a binary integer. In Section 5a, the mask 0x52 (binary 01010010) turns on bits numbered 2, 4, and 7 in the integer 0x6B.

    2. Use a bitwise and mask (&) to turn off (change to 0) bits in a binary integer. In Section 5b, the mask 0x52 (binary 01010010) turns off bits numbered 1, 3, 5, 6, 8 in the integer 0x6B.

    3. Use a bitwise exclusive or mask (^) to toggle (change from 0 to 1 of from 1 to 0) bits in a binary integer. In Section 5c, the mask 0x52 (binary 01010010) toggles bits numbered 2, 4, and 7 in the integer 0x6b.

     

  8. 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

      locations of deleted items in an array

      locations of changed items in an array

      answers for a true/false questionaire

      status of records in a database (locked or not locked)

      status of mouse buttons (pressed or not pressed)

      status of the Shift, Control, or Alt keys (pressed or not pressed)

      status of a font in a word processor (Bold/Not Bold, Italic/Not Italic, Underlined/Not Underlined, etc.)

      status of books in a library