From Wikipedia, the free encyclopedia - View original article
|Positional systems by base|
|List of numeral systems|
The octal numeral system, or oct for short, is the base-8 number system, and uses the digits 0 to 7. Octal numerals can be made from binary numerals by grouping consecutive binary digits into groups of three (starting from the right). For example, the binary representation for decimal 74 is 1001010, which can be grouped into (00)1 001 010 – so the octal representation is 112.
In the decimal system each decimal place is a power of ten. For example:
In the octal system each place is a power of eight. For example:
By performing the calculation above in the familiar decimal system we see why 112 in octal is equal to 64+8+2 = 74 in decimal.
In the TV series Stargate SG-1, the Ancients, a race of beings responsible for the invention of the Stargates, used an octal system of mathematics.
In the tabletop game series Warhammer 40,000, the Tau race use an octal number system.
Octal became widely used in computing when systems such as the PDP-8, ICL 1900 and IBM mainframes employed 12-bit, 24-bit or 36-bit words. Octal was an ideal abbreviation of binary for these machines because their word size is divisible by three (each octal digit represents three binary digits). So four, eight or twelve digits could concisely display an entire machine word. It also cut costs by allowing Nixie tubes, seven-segment displays, and calculators to be used for the operator consoles, where binary displays were too complex to use, decimal displays needed complex hardware to convert radices, and hexadecimal displays needed to display more numerals.
All modern computing platforms, however, use 16-, 32-, or 64-bit words, further divided into eight-bit bytes. On such systems three octal digits per byte would be required, with the most significant octal digit representing two binary digits (plus one bit of the next significant byte, if any). Octal representation of a 16-bit word requires 6 digits, but the most significant octal digit represents (quite inelegantly) only one bit (0 or 1). This representation offers no way to easily read the most significant byte, because it's smeared over four octal digits. Therefore, hexadecimal is more commonly used in programming languages today, since two hexadecimal digits exactly specify one byte. Some platforms with a power-of-two word size still have instruction subwords that are more easily understood if displayed in octal; this includes the PDP-11 and Motorola 68000 family. The modern-day ubiquitous x86 architecture belongs to this category as well, but octal is rarely used on this platform, although certain properties of the binary encoding of opcodes become more readily apparent when displayed in octal, e.g. the ModRM byte, which is divided into fields of 2, 3, and 3 bits, so octal can be useful in describing these encodings.
Octal is sometimes used in computing instead of hexadecimal, perhaps most often in modern times in conjunction with file permissions under Unix systems (see chmod). It has the advantage of not requiring any extra symbols as digits (the hexadecimal system is base-16 and therefore needs six additional symbols beyond 0–9). It is also used for digital displays.
In programming languages, octal literals are typically identified with a variety of prefixes, including the digit 0, the letters o or q, or the digit–letter combination 0o. In Motorola convention, octal numbers are prefixed with @, whereas a small letter o is added as a postfix following the Intel convention. DR-DOS DEBUG uses \ to prefix octal numbers.
For example, the literal 73 (base 8) might be represented as 073, o73, q73, 0o73, \73, @73 or 73o in various languages.
Newer languages have been abandoning the prefix 0, as decimal numbers are often represented with leading zeroes. The prefix q was introduced to avoid the prefix o being mistaken for a zero, while the prefix 0o was introduced to avoid starting a numerical literal with an alphabetic character (like o or q), since these might cause the literal to be confused with a variable name. The prefix 0o also follows the model set by the prefix 0x used for hexadecimal literals in the C language; it is supported by Haskell, OCaml, Perl 6, Python as of version 3.0, Ruby, Tcl as of version 9, and it is intended to be supported by ECMAScript 6 (the prefix 0 has been discouraged in ECMAScript 3 and dropped in ECMAScript 5).
Octal numbers that are used in some programming languages (C, Perl, PostScript…) for textual/graphical representations of byte strings when some byte values (unrepresented in a code page, non-graphical, having special meaning in current context or otherwise undesired) have to be to escaped as \nnn. Octal representation of non-ASCII bytes may be particularly handy with UTF-8, where any start byte has octal value \3nn and any continuation byte has octal value \2nn.
To convert integer decimals to octal, divide the original number by the largest possible power of 8 and divide the remainders by successively smaller powers of 8 until the power is 1. The octal representation is formed by the quotients, written in the order generated by the algorithm. For example, to convert 12510 to octal:
Therefore, 12510 = 1758.
Therefore, 90010 = 16048.
To convert a decimal fraction to octal, multiply by 8; the integer part of the result is the first digit of the octal fraction. Repeat the process with the fractional part of the result, until it is null or within acceptable error bounds.
Example: Convert 0.1640625 to octal:
Therefore, 0.164062510 = 0.1248.
These two methods can be combined to handle decimal numbers with both integer and fractional parts, using the first on the integer part and the second on the fractional part.
To convert a number k to decimal, use the formula that defines its base-8 representation:
In this formula, ai is an individual octal digit being converted, where i is the position of the digit (counting from 0 for the right-most digit).
Example: Convert 7648 to decimal:
For double-digit octal numbers this method amounts to multiplying the lead digit by 8 and adding the second digit to get the total.
Example: 658 = 6 × 8 + 5 = 5310
To convert octal to binary, replace each octal digit by its binary representation.
Example: Convert 518 to binary:
Therefore, 518 = 101 0012.
The process is the reverse of the previous algorithm. The binary digits are grouped by threes, starting from the least significant bit and proceeding to the left and to the right. Add leading 0s (or trailing zeros to the right of decimal point) to fill out the last group of three if necessary. Then replace each trio with the equivalent octal digit.
For instance, convert binary 1010111100 to octal:
Therefore, 10101111002 = 12748.
Convert binary 11100.01001 to octal:
Therefore, 11100.010012 = 34.228.
The conversion is made in two steps using binary as an intermediate base. Octal is converted to binary and then binary to hexadecimal, grouping digits by fours, which correspond each to a hexadecimal digit.
For instance, convert octal 1057 to hexadecimal:
Therefore, 10578 = 22F16.
Hexadecimal to octal conversion proceeds by first converting the hexadecimal digits to 4-bit binary values, then regrouping the binary bits into 3-bit octal digits.
For example, to convert 3FA516:
Therefore, 3FA516 = 376458.