|
The Octal Number System:
|
Example:
153(8) = 107(10) ^^^ ||| ||+----- 3 * 80 = 3 * 1 = 3 |+------ 5 * 81 = 5 * 8 = 40 +------- 1 * 82 = 1 * 64 = 64 + ---------- 107(10) |
Displaying binary numbers is very verbose:
Binary numbers has many digits...
Binary numbers are often displayed as octal/hexadecimal numbers for brevity:
because conversion between binary <--> octal and binary <--> hexadecimal are very easy
Converting between binary representation <--> octal representation:
|
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
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
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)
How to denote an octal number in Java:
|
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