Other number systems used in computer programming

  • In system level programming (= low level programming), programmers often needs to use the binary representation

  • Writing out binary numbers is very tedious (long):

        1101110111010101     
      

  • Instead of the binary repr., system level programmers often use:

      • Octal numbers (i.e.: base 8)           or        
      • Hexadecimal numbers (i.e.: base 16)      

    to shorten the print out of binary numbers

  • Example:

       Binary representation    Octal representation   
      
          1101110111010101          156725
      

The Octal Number System

The Octal Number System:

  • Has 8 digits: 0, 1, 2, 3, 4, 5, 6, 7

  • The value of a digit at the kth position is weighted by 8k  

Example:


    153(8) = 107(10)
    ^^^
    |||
    ||+----- 3 * 80 = 3 *  1 =     3
    |+------ 5 * 81 = 5 *  8 =    40
    +------- 1 * 82 = 1 * 64 =    64  +
                             ----------     
                                 107(10)
   

Displaying/printing binary numbers
 

Displaying binary numbers is very verbose:

Binary numbers has many digits...

Displaying/printing binary numbers
 

Binary numbers are often displayed as octal/hexadecimal numbers for brevity:

because conversion between binary <--> octal and binary <--> hexadecimal are very easy

How to convert between binary representation <--> octal representation
 

Converting between binary representation <--> octal representation:

  • How to convert a binary number (= representation) an octal number (= representation):

      • Convert a group of 3 binary digits individually into 1 octal digit

  • How to convert an octal number (= representation) a binary number (= representation):

      • Convert each octal digit in the octal number individually into exactly 3 binary digits

Example on how to convert:   binary numbers ⇒ octal numbers
 

Conversion table between 1 octal digit and 3 binary digits:

      Octal digit        Binary digits
      -----------        -------------       
            0      <-->         000
            1      <-->         001
            2      <-->         010
            3      <-->         011
            4      <-->         100
            5      <-->         101
            6      <-->         110
            7      <-->         111

   (Leading 0's can optionally be truncated)
   

Example:   11111101(2) ===> 375(8)

Note: you must start grouping the binary digits from right → left

Note: you may need to add leading 0 bits to the binary number to convert the last octal digit

Example on how to convert:   octal numbers ⇒ binary numbers
 

Conversion table between 1 octal digit and 3 binary digits:

      Octal digit        Binary digits
      -----------        -------------       
            0      <-->         000
            1      <-->         001
            2      <-->         010
            3      <-->         011
            4      <-->         100
            5      <-->         101
            6      <-->         110
            7      <-->         111

   (Leading 0's can optionally be truncated)
   

Example:   174253(8) = 1111100010101011(2) (in 16 bits)

Note: you may need to drop some leading 0 bits from the binary number to get the desired length in the binary number

The octal dump application in UNIX

From Wikipedia:

I will demo the UNIX od command in class using these commands:

   cd /home/cs255001/demo/dump    

   od -t o1 bin-file   // -t o1 means: display 1 byte data in octal
          // Adding z will print corresponding characters if possible 

Each octal number in the output represents 8 bits (1 byte)

Denoting octal numbers in Java

How to denote an octal number in Java:

  • When the leading digit = 0, Java will treat the number as an octal number

Example:

    int x = 10;   // Default is decimal
    int y = 010;  // Octal number !

    System.out.println(x);  // prints 10
    System.out.println(y);  // prints 8      
   

DEMO: /home/cs255001/demo/java/Octal.java  

Application:   you can write a binary number more compactly using octal numbers