Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I just banged my head against the table for some 20 minutes looking at a totally weird bug in PHP, and then I realized there's octal. The <%(*&#> octal.

In short, I padded some literals with zeros so the code would be aligned, I know, big mistake. Forgot about octals.

The question is, does anyone use octals for anything other than file permissions? (I personally prefer chmod ugo+rwx but I understand that if they have to be programmaticaly generated, it's useful to use octals.) But are they useful in any other situation?

share|improve this question
9  
Option A: Blame yourself. Option B: Blame the world. Option C: chuckle as you have learned something. –  Job Aug 4 '11 at 18:50
5  
@Petrozza - IMO the C-style use of a leading zero to indicate octal was never a good choice, but it's history - probably a bit of C heritage, though it may be even older. I think Ada has one of the better conventions - an octal number would be written 8#1234567#, a hex number 16#abcdef#, etc and leading zeros are just leading zeros - any base from 2 to 16 (at least) is valid, and all are visually obvious and easy to understand. –  Steve314 Aug 4 '11 at 22:16
    
@job: I blame myself 100%. I know octal notation and should have realised. But still, who uses octals anymore? –  Petruza Aug 6 '11 at 6:49
1  
@Petruza Your question in its original form was not constructive for this site, and it was improved by the community so that it could remain open: please see the FAQ for more information about why people do this and why we encourage it. If you have any additional questions or concerns, please raise them on our meta-discussion site, not in your question. –  user8 Aug 6 '11 at 7:50
    
Option D: "blame PHP" is always the right choice, regardless of the problem –  Arian Jan 19 at 7:49

10 Answers 10

up vote 26 down vote accepted

According to Wikipedia, octals aren't as common as they used to be. As others have already mentioned, in the past, systems used to have a 12/24/36-bit word, which is more easily represented in octal than hexadecimal, but currently, the x86 and i64 architectures use a 16/32/64 bit word, which is more easily represented in hexadecimal and downward ugly in octal.

Current uses, however, include:

  • "Real" real-world use: the Yuki people and in the native Mexican Pamean languages use octal counting because they count the spaces between their fingers (see here)
  • Historically in the 1950's, one of the oldest debuggers, UT-3 for the TX-0 computer at MIT (an 18-bit system), could only be operated by using commands written in octal notation (see page 20 of Hackers, Heroes of the Computer Revolution).
  • Representation of UTF8 numbers (any start byte is \3nn and any continuation byte \2nn)
  • Representation of rights in Unix-like systems (chmod)
  • TAR files store some information in octal representation according to this.
  • Representation of IP addresses (rare, sometimes used by spammers to obscure addresses)
  • Sometimes fields are naturally divided into three or six bits, octal representation comes in handy then, see here.
  • From the same link, you find that the FAA uses octals in transponders and in the venerable Arinc 429 bus standard.
  • A whole lot of legacy (Honeywell, PDP etc)
  • In 1971, octal numbers were proposed to replace the decimal system (see here).
  • And finally, trivially: you use it almost everyday when you write down the number 0 in some programming language that supports octals, as this user wrote.

Note that modern programming languages sometimes don't support octals anymore because of the lack of use-cases and the potential for bugs. C# is an example, as Eric Lippert wrote this in the TCPL 3rd and 4th edition:

C# does not support octal literals, for two reasons. First, hardly anyone uses octal literals these days. Second, if C# supported octal in the standard "leading zero means octal" format, then it would be a potential source of errors. Consider this code:

    FlightNumber = 0541;

Clearly this expression is intended as a decimal literal not an octal literal.

Related, and just for reference and curiosity, Code, The Hidden Language of Computer Hardware has an excellent easy-to-follow explanation on octal and other number systems on page 55 - 63.

share|improve this answer
    
On that note, watch out for Javascript when handling user input. parseInt(011)==9 –  Darien Aug 5 '11 at 0:01
2  
That's really interesting about the Yuki and Pamean. It just occurred to me that hexadecimal finger counting is reasonably easy, if you use the inside of each finger, using some variation, like this –  Slomojo Aug 5 '11 at 1:28
    
@Slomojo - you can count to 99 on your fingers, in fact... –  detly Aug 5 '11 at 2:02
2  
+1 for a very comprehensive answer. Grace Hopper once said she had troubles balancing her checkbook, because she had become so proficient at adding in octal. –  Macneil Aug 5 '11 at 3:47
10  
@detly, if I know I need to count large numbers I'll use my 10 digits as bits and count to 1024 with just my fingers (just be careful who you show #4, #128 & #132 to). –  zzzzBov Aug 5 '11 at 13:32

Like hexadecimal, octal notation is sometimes used to specify character codes. It can be handy for the same reason that hexadecimal is: it's more compact than binary, but very easy to translate to binary in your head.

share|improve this answer

Anybody who needs to express combinations of bits grouped in threes.

Why does anyone use hex?


Things off the top of my head that are expressed as octal numbers:

  • Codes squawked by Mode 3A transponders in airplanes
  • Certain values found on the ARINC 429 avionics bus
  • SCSI addresses
  • Key codes in the ncurses library
share|improve this answer
8  
The difference is, hex is "natural" as half a byte. Two hex digits = 1 byte. But you can't divide bytes up into octal digits. –  Mason Wheeler Aug 4 '11 at 18:51
1  
The first line states the obvious. The second one makes a poor analogy. –  back2dos Aug 4 '11 at 19:07
3  
And hex is completely unnatural for anything that needs to be expressed in groups of three bits like the aforementioned Unix file modes. There's no law that says a particular numbering system's digits have to line up with bytes. If there was, expressing anything to a computer as base 10 would be worse practice than base 8, which at least aligns itself with groups of bits. –  Blrfl Aug 4 '11 at 19:25
5  
a typical hex string is obviously a hex string. octal isn't. –  Paul Nathan Aug 4 '11 at 20:00
1  
@Blrfl: So how many of these mentioned "[things that] need to be expressed in groups of three bits" are there apart from the canonical example? That's the actual question, which you left unanswered thus far. Instead you make yet another poor analogy. There is actual use for decimal representation, because we simply learned to handle it. Which one will you calculate faster? 7 * 6 or 07 * 06? We use decimal to represent numbers as we can handle them best and hex to represent numbers very close to how the machine stores them. And we do this a lot. This site is full with evidence of it. –  back2dos Aug 4 '11 at 20:04

Like Hex, Octal numbers are easy to convert from binary. Just group the binary digits in groups of three, starting at the right.

10010101110  // binary

// conversion
(010)(010)(101)(110)
// so, in octal we have...
(2)(2)(5)(6)
2256

10010101110 (base 2) == 2256 (base 8)
share|improve this answer
7  
This is obvious. The question is about when this is actually used. –  back2dos Aug 4 '11 at 19:09
2  
@back2dos: then please feel free to post an answer instead of leaving the same comment multiple times. –  Ed S. Aug 4 '11 at 19:12
4  
@Ed S. You didn't even try to answer the question. You said something obvious that the original poster almost certainly already knew the answer to. As the moderators are fond of saying, this isn't what we are shooting for. See meta.programmers.stackexchange.com/questions/1968/… for more. –  btilly Aug 5 '11 at 0:33
    
@back2dos, btilly - Answers on Stackexhange sites aren't just for the OP, they become a persistent resource for any passing reader. All obvious facts, aren't necessarily obvious to everyone, if this answer helps the penny-drop for someone it has value. –  Slomojo Aug 5 '11 at 0:49
1  
@Slomojo though by that argument, every question is eligible to become a wiki for every related aspect of a topic. The point is that this doesn't answer the question. This answer can certainly be valid somewhere else, to "How to quickly mentally convert to octal?" –  NickC Aug 5 '11 at 0:52

Heathkit used them for the front panel of their H-8, and for assembly language listings of their front-panel software (called PAM-8). Dating myself, I know.

enter image description here

Nowadays octals are very rare; everyone seems to use hex. But numbers still look like numbers in octal, and you don't need an additional row of keys for the digits A through F.

share|improve this answer
2  
+1 for the H-8. Despite being octal on the outside, the CPU was an 8080A, which was all bytes. Go figger. –  Blrfl Aug 4 '11 at 19:42
    
I'd guess they used octal for commonality with the H-11 (which had a PDP-11 CPU). –  Jerry Coffin Aug 4 '11 at 23:03
2  
The 8080 had 8-bit bytes, but the instruction set made more sense if looked at in octal than in hex. –  RBerteig Aug 5 '11 at 2:28

I use octal every day. Every time I need to chmod a file I use octal (I pre-date the u+r notation...). Every time I want to embed a control character inside a string I use octal... (Yes, I could use hex, but octal is what I use).

share|improve this answer
    
+1 for "but octal is what I use" :) –  JoelFan Aug 4 '11 at 23:28

Nope. Never used them. Not even for setting file permissions, because I do it so rarely I find it hard to read.

So yes, there is the obvious benefit of conversion from and to binary.
However, as already pointed out by @Mason, hex is far more "natural", because 2 hex digits make one byte. Comes in handy to represent 24/32 bit RGB/ARGB colors, IP addresses, byte masks etc. Octal on the other side is not really practical, because you rarely have scenarios where you work in terms of 3-bit groups.

File permissions are in fact the only thing I could think about. And if there's no area of use as of now, I doubt there will ever be one. Since the last 20 years space has become so cheap, it has become very uncommon to pack information so tightly, at the cost of alignment, ease of use and extensibility.

share|improve this answer

Octal was widely used some 50 years ago by Digital Equipment Corp. (DEC) and other companies that had computers with a 12-bit word (e.g. the PDP-8) or other multiples of six, such as 18 and 36 (e.g. UNIVAC 1108). I used both the PDP-8 and UNIVAC 1108 in grad school. Characters in both machines typically used six bits, not 8.

PDP-8 instruction format

PDP-8 instruction format -- note the bits are numbered 0 -> 11. Bit 0 was the MSB (most significant bit).

When DEC came out with the 16-bit PDP-11, they continued to use octal in their documentation instead of hexadecimal as used by the other minicomputer manufacturers coming out with 16-bit machines at the time. This was probably because of the multiple 3-bit fields within the PDP-11 instruction formats like Register, Mode and Src/Dest which lent themselves to be decoded as an octal digit. (Thanks to John Strohm for pointing this out.)

Interestingly, when Motorola came out with their 68000 microprocessor, which was heavily influenced by the PDP-11 and had the same 3-bit Mode and Register fields in the instructions, they chose to use only hexadecimal in their documentation.

Because the PDP-11 used octal notation, the original permission codes for Unix, which first appeared on the PDP-11, also used octal. This legacy persists in Linux, where the chmod command still uses octal to specify each of the three bit 'rwx' fields.

Some more trivia -- CompuServe, which was a widely used dial-up online service in the 1980's and early 1990's (before being overshadowed by AOL), ran on DEC minicomputers, at least originally. All user ID's were numeric, and at some point I made the observation that they never included any 8's or 9's in them, thus they were in octal. My old CIS ID was 70205.

share|improve this answer
1  
Your comment about PDP-11 documentation is incorrect. The PDP-11 had 8 registers. R0-R5 were general purpose registers. R6 was the stack pointer, and R7 was the PC. The MOV instruction took two 3-bit register numbers and two 3-bit addressing modes. Octal captures the register and mode fields PERFECTLY. (I strongly doubt that this was an accident.) Using hex would have confused everything. PUSH and POP were just addressing modes that used R6. Immediate operand was an addressing mode that used R7. –  John R. Strohm Jan 18 at 5:53
    
@JohnR.Strohm Thanks for the comment re the 3-bit registers. I agree with your analysis and have updated my answer. –  tcrosley Jan 19 at 5:23

Back when I worked on CDC mainframes (with a 60-bit word, 12-bit word for the PPU), all the core dumps and such were done in octal (20 octal digits per word, 4 per PPU word).

share|improve this answer

One of the reasons why octal sees use is because it's the largest base that is a power of 2 for which you don't need any special characters -- that is, all digits can be numeric. Base 16 (the next one up from base 8) requires non-numeric digits (usually A,B,C,D,E,F).

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.