39

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?

Petruza
  • 1,008
  • 1
  • 8
  • 14
  • 18
    Option A: Blame yourself. Option B: Blame the world. Option C: chuckle as you have learned something. – Job Aug 04 '11 at 18:50
  • 13
    @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. –  Aug 04 '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 06 '11 at 06:49
  • 2
    @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](http://programmers.stackexchange.com/faq#editing) 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](http://meta.programmers.stackexchange.com), not in your question. –  Aug 06 '11 at 07:50
  • 11
    Option D: "blame PHP" is always the right choice, regardless of the problem – Cephalopod Jan 19 '14 at 07:49
  • @Arian and C, C++ and many other languages that use the 0 notation – Petruza Jan 20 '14 at 14:50
  • PS: Someone edited my question from "who uses octal?", which is pretty different from what's now the question. (this was edited out from my original question) – Petruza Jan 20 '14 at 14:52
  • 1
    Octal is sometimes useful, but I find myself totally flummoxed as to why anyone desinging a *new* language would use 031 as a notation for twenty-five, rather than e.g. 31q, 0q31, @31 [if @ has no other meaning], etc. – supercat Sep 14 '15 at 22:21
  • 2
    @Steve314 I totally agree. Another noteworthy example is Python where they have the `0o` prefix (which is consistent with `0x` and `0b` but perhaps a little hard to distinguish from `00`). And since Python 3, extra leading zeros a re a syntax error to prevent this kind of bugs. – 5gon12eder Sep 15 '15 at 04:34
  • @5gon12eder: IMHO, the C Standard should long ago have deifned a standard macro which, when given a string of digits 0-7 with or without a leading zero, would regard it as an octal number, and also defined either 0q123 or 8x123 as a syntax for writing octal constants ("q" instead of "o" is a convention used in many assemblers because "q" is visually distinct from "o"; the latter could easily be adapted for any base up to 36). Use of leading zeroes for any non-zero integer literals could then be deprecated, with compilers allowing such usage if necessary to support old code, but... – supercat May 04 '17 at 16:42
  • 1
    ...also being allowed to reject it in favor of the new usages when compatibility with old code was not required. Had C89 done that, later standards could have added `#pragma` directives to specify whether numbers with leading zeroes should be interpreted as octal, decimal, or "error" [with the latter being the default] and such usage would have made it possible to use token pasting to assemble strings of digits into a form that could be either processed as a decimal literal or fed to the stringize operator. – supercat May 04 '17 at 16:43

11 Answers11

45

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 downright ugly in octal.

Current uses, however, include:

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.

Abel
  • 912
  • 8
  • 13
  • 1
    On that note, watch out for Javascript when handling user input. `parseInt(011)==9` – Darien Aug 05 '11 at 00:01
  • 3
    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](http://i.imgur.com/Jy50Z.jpg) – ocodo Aug 05 '11 at 01:28
  • @Slomojo - you can [count to 99 on your fingers](http://www.wikihow.com/Count-to-99-on-Your-Fingers), in fact... – detly Aug 05 '11 at 02:02
  • @detly - simulating an abacus on your fingers is a little more complex. – ocodo Aug 05 '11 at 02:22
  • 3
    +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 05 '11 at 03:47
  • @Slomojo - no, simulating an abacus with your *toes* is complex. – detly Aug 05 '11 at 04:32
  • 15
    @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 05 '11 at 13:32
  • 1
    @Darien For posterity, this isn't going to be an issue if you're parsing user input from strings: `parseInt('011')` still returns `11`. `parseInt(011)` returns `9` trivially because the interpreter processes the octal `011` into the integer `9` first, then hands the integer to the `parseInt()` function, which just returns its input when handed an integer. – Ken Bellows Dec 13 '19 at 15:15
17

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.

tcrosley
  • 9,541
  • 1
  • 25
  • 41
  • 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 '14 at 05: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 '14 at 05:23
  • So DEC worked in OCT. Should've just used multiples of 3.32 bits to avoid confusion. – user234461 Aug 14 '17 at 12:37
  • It’s funny how I never considered the Unix permissions to be an octal number, I always thought of it as three individual digits written together, each representing the three rwx bits, so they could be octal, decimal, or even hex. – axl Nov 17 '17 at 05:33
11

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.

Robert Harvey
  • 198,589
  • 55
  • 464
  • 673
7

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

tylerl
  • 4,850
  • 21
  • 32
  • I know this is old, but why would using only numeric digits be preferable to also using letters? – Petruza Apr 02 '19 at 12:50
  • 2
    E.g. one can tell instantly that 376 is larger than 375. With hex, one might need a second or two (depending on tiredness) to process whether FE is greater than FD – Magnus Jan 02 '20 at 04:23
  • 1
    It's useful because everyone knows that digits are numbers. "FADE" may be a verb, or an abbreviation or code-name, or it could be 64222. But 0755 is definitely a number. You may guess the wrong base, but you have no confusion about what box it goes in; you can be dead-certain it's a number. Aircraft all over the world still today use octal (4-digit / 12-bit) transponder codes for interacting with ATC because it's less error-prone than trying to do the same thing in hex. – tylerl Jan 02 '20 at 06:48
  • Numeric digits also have the advantage of being case-insensitive, so if you encode file names in octal (typically after hashing the original file name) then you can reference the file without issue on both case-sensitive and case-insensitive file systems. – Eric Mutta Apr 21 '23 at 11:18
5

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
Blrfl
  • 20,235
  • 2
  • 49
  • 75
  • 12
    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 04 '11 at 18:51
  • 2
    The first line states the obvious. The second one makes a poor analogy. – back2dos Aug 04 '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 04 '11 at 19:25
  • 6
    a typical hex string is obviously a hex string. octal isn't. – Paul Nathan Aug 04 '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 04 '11 at 20:04
  • I listed a few other current uses of octal in my answer. The canonical example is an excellent use for octal, and it's one that's widely used. But I guess if there's only that one, we should just get rid of it. Speaking of poor analogies, what does doing calculation speed have to do with it? There's nothing magical about decimal that makes doing mental computations in it go faster. The only reason we do it with the speed we do is that we learned them by rote starting when we were kids. Had humans been endowed with eight fingers instead of ten, our familiar radix would have been eight. – Blrfl Aug 04 '11 at 21:22
  • @Blrfl: Thank you :) About the analogy: it is poor, because it simply doesn't take reality into account. Yes, we *could* have 8 fingers and bytes *could* be 6 bits long. Let's just say, it is *very* rare. We *could* also be writing Egyptian. By that logic compilers that compile stone tablets with hieroglyphs on them are just as useful as the GCC, if you see where I'm going :D – back2dos Aug 05 '11 at 09:32
5

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

Majenko
  • 151
  • 2
5

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

Jerry Coffin
  • 44,385
  • 5
  • 89
  • 162
3

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.

Caleb
  • 38,959
  • 8
  • 94
  • 152
3

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.

back2dos
  • 29,980
  • 3
  • 73
  • 114
2

A bit of an aside, but a public use is on the London Underground. Each train has both a physical identity, and a separate identity in the schedules which is displayed when in service - see here: https://upload.wikimedia.org/wikipedia/commons/4/49/LUL-S-Stock-at-Watford.jpg (note the 402 at the top of the destination display). Although the electronics for the displays are bog standard decimal-based, you never see digits 8 or 9 because they are tied to legacy octal-based hardware in the signalling system.

Julia Hayward
  • 2,872
  • 2
  • 15
  • 12
2

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)
Ed S.
  • 2,758
  • 2
  • 21
  • 24
  • 7
    This is obvious. The question is about when this is actually *used*. – back2dos Aug 04 '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 04 '11 at 19:12
  • 5
    @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 http://meta.programmers.stackexchange.com/questions/1968/do-significant-claims-require-evidence/1986#1986 for more. – btilly Aug 05 '11 at 00: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. – ocodo Aug 05 '11 at 00:49
  • 2
    @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?" – Nicole Aug 05 '11 at 00:52
  • @Renesis - a quick glance at back2dos' actual answer might shed some light on my motivation to interject. – ocodo Aug 05 '11 at 01:32
  • @Slomojo: `The question is, does anyone use octals for anything other than file permissions?`. How can it be any clearer? There has been a number of insightful, non-trivial, on-topic answers here. Also, I wonder, what does this answer have to do with my answer? Is it's quality conditional on the existence of my answer or what? It doesn't answer the question, full stop. If you have anything to amend to my answer, comment it accordingly. – back2dos Aug 05 '11 at 09:56
  • I believe Ed here is suggesting that converting from binary to decimal is a possible use for octals, which makes it an on-topic answer to this question. See discussion [here](http://meta.programmers.stackexchange.com/questions/2068/why-was-this-response-deleted) on Meta. – Adam Lear Aug 05 '11 at 16:39