4

Many languages (dating back at least to C. I am not familiar with older ones) seem to have the convention the integer literals can be written in on of 3 bases with the following notation:

  • 0x or 0X prefixes a base-16 (hex) number
  • 0 prefixes a base-8 (octal) number
  • No prefix means base-10 (decimal) number

I understand the use of hex and decimal. They are used all over the place, have distinct use cases where they make the code more readable (a bitwise and with 0x20000 is much clearer than with 131072), and cannot be confused (a number with an 'x' or 'X' init does not make sense as a decimal number).

However, the octal literals seem like a mistake to me. First of all, I have never seen them used, and can't seem to think of a single use-case where it would make the code more readable to use them over hex or decimal. In addition, they can potentially be confused with decimal numbers (010 is a perfectly leagal decimal number outside of programming language domain).

So, do octal literals have a use, are they some form of historical leftover baggage from some time when they were useful, or are they just a mistake?

Baruch
  • 20,590
  • 28
  • 126
  • 201
  • You use them in linux for commands like `chmod` – Mr. E Dec 03 '15 at 13:38
  • 1
    There is no valid reason to use them today, just historical baggage. `chmod` and alike might use them but there is no reason why they couldn't have chosen to use hex numbers instead. Maybe they just wanted to save typing an extra `x`. – user694733 Dec 03 '15 at 13:48
  • 2
    A more general answer is that they are used in the field of micro controller http://www.plcdev.com/octal_what_the_hex. And from wikipedia: "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 " – terence hill Dec 03 '15 at 13:51
  • 1
    (`/me` Strokes greying beard...) There was a time not that long ago, around the birth of C, when octal was more widely used than hexadecimal. If you read papers from the sixties and seventies, you'll find a lot of octality. There is no point in removing them just because they're confusing the youngsters. – molbdnilo Dec 03 '15 at 13:53
  • @user694733 chmod uses them because in chmod you're working with 3 bits at a time. Not to avoid typing x. – Art Dec 03 '15 at 13:53
  • 1
    Octal characters are easily detected and converted to their binary equivalent (ASCII, EBCDIC) - simple mask operations. Hexadecimal not so. In early computing this simplification was valuable. It may seem trivial today, but the cost of computing was much higher then. – chux - Reinstate Monica Dec 03 '15 at 13:54
  • 1
    @molbdnilo I would upvote your comment as an answer. – chux - Reinstate Monica Dec 03 '15 at 13:57
  • 1
    @user694733 Not so obvious use case: `int x = 0;` `x` takes on the value of an octal integer constant. ;-) – chux - Reinstate Monica Dec 03 '15 at 14:02
  • @Art Point I was making that, ignoring any technical limitations at the time, `chmod` could have used hex numbers and it would have worked equally well. So when the question is *"What's the point in octals?"*, valid answer is **not** *"chmod uses it!"* because it completely misses the point. – user694733 Dec 03 '15 at 14:09
  • @user694733 And it could have used decimal numbers. And any other of the endless possibilities to convert strings of characters into a number. Why do we have decimal numbers? Because it's more convenient in some situations. Why do we have hexadecimal numbers? Because it's more convenient in certain situations. Why do we have octal numbers? Because it's more convenient in certain situations. Probably not hugely useful today anymore, but saying "they just wanted to save typing an extra x" is ignorant. – Art Dec 03 '15 at 14:19
  • @Art Yes, but when you think about the *interface* of the `chmod` all the convenience octals offer is the `x` (compare `0755` vs `0x755`). And my comment about saving typing was a dry joke, but actually pretty fitting when you think about horrible early function naming in C standard library and Unix land. – user694733 Dec 03 '15 at 14:28
  • What if your machine has 6-bit byte? – user3528438 Dec 03 '15 at 15:06

2 Answers2

4

A use case that comes to mind is file permissions, e.g.:

int retval = mkdir("/tmp/example", 0755);

See http://linux.die.net/man/2/mkdir

File permissions in Unix are expressed in three groups of three bits:

  • user: read, write, execute
  • group: read, write, execute
  • other: read, write, execute

755 represents user: read, write, execute ; group: read, execute ; others: read, execute.

Coincidentally, Base 8 digits can be expressed using exactly three bits:

0 --> 000
1 --> 001
2 --> 010
3 --> 011
4 --> 100
5 --> 101
6 --> 110
7 --> 111

So octal base is a very convenient notation for file permissions.

If base 10 was used to specify modes, 0755 would actually become 493, which would not map clearly to a given mode, just like in hexadecimal 0x1ED.

Here, in base 8 each digit is exactly a single group of three bits.

SirDarius
  • 41,440
  • 8
  • 86
  • 100
2

The most common use of octal literals is when specifying file permissions on UNIX-like systems.

The common file permissions are read, write, and execute, and these can be applied to the file's user, the file's group, or to other.

Here's an example of a directory listing:

-rw-------  1 dbush      dbush      14982 Dec  2 14:23 test.txt

In this example, the user has read and write permissions, while the group and "other" have no permissions.

If you were to create a file with these permissions using the open function, you would do it like this:

int fd = open("test.txt", O_WRONLY | O_CREAT, 0600);

The file mode bits are specified as follows:

0400 : read for owner
0200 : write for owner
0100 : execute for owner
0040 : read for group
0020 : write for group
0010 : execute for group
0004 : read for other
0002 : write for other
0001 : execute for other

Because the permissions bits are in groups of 3, octal notation makes it more clear what the permissions are.

dbush
  • 205,898
  • 23
  • 218
  • 273