52

I have never used octal numbers in my code nor come across any code that used it (hexadecimal and bit twiddling notwithstanding).

I started programming in C/C++ about 1994 so maybe I'm too young for this? Does older code use octal? C includes support for these by prepending a 0, but where is the code that uses these base 8 number literals?

vonbrand
  • 11,412
  • 8
  • 32
  • 52
Jared Updike
  • 7,165
  • 8
  • 46
  • 72
  • 31
    I use them (in the form of octal zeroes) all the time: `if (b < 0) b = 0;` – pmg Oct 16 '09 at 17:00
  • 2
    @pmg: I wonder if there would be any difficulty with defining 0q1234 as being the "proper" format for non-zero octal constants, and explicitly depricating 01234 with the recommendation that compilers produce a non-fatal diagnostic? Actually, what I'd like to see would be a general bit-based format 0[[x[0-9A-Fa-f]+]|[q[0-7]+]|[w[0-3]+]|[v[0-1]+]]+ to allow a mix of 1-4 bit fields, since 3-bit fields aren't always aligned a multiple of 3 bits from the right. – supercat Aug 01 '11 at 15:40
  • By some coincidence, this question ended up showing on SO's front page today (someone added an answer) and Raymond Chen happened to post an article about the octal mess today as well: [The curse of the leading zero](http://blogs.msdn.com/b/oldnewthing/archive/2014/01/16/10490109.aspx) – Michael Burr Jan 17 '14 at 01:24
  • The only use I have seen is when defining file open() flags and permissions as they typically are copied from standards references. But even for those cases they are not much more helpful than decimal or even binary representation for integer literals. – eckes Feb 18 '15 at 05:26
  • 1
    @supercat Python 3 does so - octal constants have the form `0o1234`, while the old `01234` form gives a syntax error. – glglgl Dec 03 '15 at 13:41
  • and this: https://esdiscuss.org/topic/octal-literals-have-their-uses-you-unix-haters-skip-this-one – Jerry Jeremiah Aug 15 '16 at 21:39
  • @eckes In C `0` by itself is octal. _An octal constant consists of the prefix 0 **optionally** followed by a sequence of the digits 0 through 7 only._ Section 6.4.4.1 Integer constants. The grammar in that section also shows it. – Pryftan Apr 19 '23 at 16:14
  • The literal 0 is however not a ambiguity problem. Non zero octal literals are however used for example for API constants in unix calls (like 0777 unmask or O_APPEND 02000). – eckes May 08 '23 at 07:08

13 Answers13

54

I recently had to write network protocol code that accesses 3-bit fields. Octal comes in handy when you want to debug that.

Just for effect, can you tell me what the 3-bit fields of this are?

0x492492

On the other hand, this same number in octal:

022222222

Now, finally, in binary (in groups of 3):

010 010 010 010 010 010 010 010
Ben Collins
  • 20,538
  • 18
  • 127
  • 187
  • I mentioned your answer in a similar question at Programmers Exchange and expanded on it, perhaps of interest to others who are curious about octals: http://programmers.stackexchange.com/questions/98692/where-are-octals-useful/98733#98733 – Abel Jan 17 '14 at 22:28
  • 1
    Right, but they are normally used only for existing protocols that use 3-bit groups, or multiples thereof. Any new code and protocols normally uses nybbles or multiples thereof, usually denoted in hex. – mirabilos May 31 '14 at 19:02
  • 3
    @mirabilos that's a pretty broad statement. There's no reason to be locked into fields lengths as multiples of 4 or 8 bits - in fact, ruling out other lengths can cause a lot of overhead in the protocol. Perhaps for some applications that doesn't matter, but what about protocols that are supporting an application for which performance is truly important? In those cases you wouldn't choose a 4-bit field when a 3-bit field will do. – Ben Collins Aug 07 '14 at 15:11
  • @BenCollins “normally” does not mean “always, at any cost”. I assume sensible thinking on the side of the protocol developer. Still, modern things (for the last few decades…) tend to use hex over octal. – mirabilos Aug 08 '14 at 13:29
37

The only place I come across octal literals these days is when dealing with the permission bits on files in Linux, which are normally represented as 3 octal digits, where each digit represents the permissions for the file owner, group and other users respectively.

e.g. 0755 (also just 755 with most command line tools) means the file owner has full permissions (read, write, execute), and the group and other users just have read and execute permissions.

Representing these bits in octal makes it easier to figure out what permissions are set. You can tell at a glance what 0755 means, but not 493 or 0x1ed.

Chris AtLee
  • 7,798
  • 3
  • 28
  • 27
  • Also IPC resources like semaphores and shared memory. http://uw714doc.sco.com/en/SDK_sysprog/_Getting_Semaphores.html#ipc_i8 – An̲̳̳drew Nov 26 '08 at 15:09
  • Of course this is better handled with u=rwx,go=rx, which has been in posix for around 30 years. This means (u)ser gets read, write, and eXecutable, while (g)roup and (o)ther get read and execute. No octal thinking required. – jrodman Jan 20 '23 at 01:56
  • You come across it with just 0. Yes it's octal by itself. Read the standard section 6.4.4.1 Integer constants. – Pryftan Apr 19 '23 at 16:15
30

From Wikipedia

At the time when octal originally became widely used in computing, systems such as the IBM mainframes employed 24-bit (or 36-bit) words. Octal was an ideal abbreviation of binary for these machines because eight (or twelve) digits could concisely display an entire machine word (each octal digit covering three binary digits). 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 radixes, and hexadecimal displays needed to display letters.

All modern computing platforms, however, use 16-, 32-, or 64-bit words, with eight bits making up a byte. On such systems three octal digits would be required, with the most significant octal digit inelegantly representing only two binary digits (and in a series the same octal digit would represent one binary digit from the next byte). Hence hexadecimal is more commonly used in programming languages today, since a hexadecimal digit covers four binary digits and all modern computing platforms have machine words that are evenly divisible by four. 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. The modern-day ubiquitous x86 architecture belongs to this category as well, but octal is almost never used on this platform.

-Adam

Adam Davis
  • 91,931
  • 60
  • 264
  • 330
27

I have never used octal numbers in my code nor come across any code that used it.

I bet you have. According to the standard, numeric literals which start with zero are octal. This includes, trivially, 0. Every time you have used or seen a literal zero, this has been octal. Strange but true. :-)

Tim
  • 9,171
  • 33
  • 51
  • 10
    I think you might be mistaken. Section 6.4.4.1 of ISO/IEC 9899:201x specifies the form literal constants can take, and `0` is explicitly listed as an octal constant. Additionally, decimal constants cannot start with a zero. You can see a draft of this standard online [here](http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf). – Tim Nov 29 '16 at 22:48
  • 5
    @LucBloom `0` factually **is** octal. See [Is 0 a decimal literal or an octal literal?](https://stackoverflow.com/questions/6895522/is-0-a-decimal-literal-or-an-octal-literal) – underscore_d Aug 02 '17 at 09:35
  • 3
    We could say that the literal 0 is only octal per a lexical definition, but in earnest the number 0 is expressed in all possible bases at the same time. Or maybe none of it. – Petruza Apr 02 '19 at 12:59
  • @Petruza _all_ numbers are only in a specific base in their given lexical presentation. Once it has been parsed it has no base at all. – Alnitak Oct 20 '20 at 15:07
  • 1
    Except 0, which has the exact same representation no matter the base. – Petruza Oct 20 '20 at 19:37
  • 1
    @LucBloom Reread the C standard. It most certainly IS octal. Check section 6.4.4.1 Integer constants for starters. _An octal constant consists of the prefix 0 **optionally** followed by a sequence of the digits 0 through 7 only._ So factually you're completely mistaken. – Pryftan Apr 19 '23 at 16:11
9

Commercial Aviation uses octal "labels" (basically message type ids) in the venerable Arinc 429 bus standard. So being able to specify label values in octal when writing code for avionics applications is nice...

Samuel Lee
  • 91
  • 1
  • 1
7

I have also seen octal used in aircraft transponders. A mode-3a transponder code is a 12-bit number that everyone deals with as 4 octal numbers. There is a bit more information on Wikipedia. I know it's not generally computer related, but the FAA uses computers too :).

John Meagher
  • 22,808
  • 14
  • 54
  • 57
  • 2
    I think that was done for the benefit of decades-ago avionics where you put the code in on four thumbwheels, which directly encode the 12-bit value into simple digital logic for sequential transmission. No microprocessor was involved, since they didn't have them (or didn't want the complexity in avionics app). – greggo Dec 07 '10 at 19:26
6

It's useful for the chmod and mkdir functions in Unix land, but aside from that I can't think of any other common uses.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
4

There are still a bunch of old Process Control Systems (Honeywell H4400, H45000, etc) out there from the late 60s and 70s which are arranged to use 24-bit words with octal addressing. Think about when the last nuclear power plants were constructed in the United States as one example.

Replacing these industrial systems is a pretty major undertaking so you may just be lucky enough to encounter one in the wild before they go extinct and gape in awe at their magnificent custom floating point formats!

nvuono
  • 3,323
  • 26
  • 27
4

tar files store information as an octal integer value string

Mandrake
  • 363
  • 1
  • 3
  • 11
4

There is no earthly reason to modify a standard that goes back to the birth of the language and which exists in untold numbers of programs. I still remember ASCII characters by their octal values, would have to think to come up with the hex value of A, but it is 101 in octal; numeric 0 is 060... ^C is 003...

That is to say, I often use the octal representation.

Now if you really want to bend your mine, take a look at the word format for the PDP-10...

Dale Amon
  • 51
  • 1
4

I came into contact with Octal through PDP-11, and so, apparently, did the C language :)

  • 2
    Ah, PDP11; I can still remember much of the encoding: 010446 = MOV R4,-(SP) since 01xxxx = mov; 04=R4; 46 = -(SP). – greggo Dec 07 '10 at 19:28
3

Anyone who learned to program on a PDP-8 has a warm spot in his heart for octal numbers. Word size was 12 bits divided into 4 groups of 3 bits each, so -1 was 7777 octal. This scheme was perpetuated in the PDP-11 which had 16 bit words but still used octal representation for various things, hence the *NIX file permission scheme which lives to this day.

PrgTrdr
  • 316
  • 4
  • 13
1

Octal is and was most useful with the first available display hardware (7-segment displays). These original displays did not have the decoders available later.

Thus the digital register outputs were grouped to fit the available display which was capable of only displaying eight(8) symbols: 0,1,2 3,4,5,6,7 .

Also the first CRT display tubes were raster scan displays and simplest character-symbol generators were equivalent to the 7-segment displays.

The motivating driver was, as always, the least expensive display possible.

JAMES
  • 11
  • 1
  • To clarify: certainly you can display hex digits on a 7-segment display; are you saying the issue was to keep the complexity of the encoders for them down? Was adding more digits really less expensive than adding more logic? – rakslice Nov 09 '13 at 23:13