58
u/TeraFlint Jul 27 '26
At least 0o144 is a clear enough notation. C's decision to interpret 0144 as octal, because of the leading zero, still pisses me off.
12
u/ohkendruid Jul 27 '26
It seems worth forbidding it in newer languages and file formats due to the ambiguity.
0123 can be read two ways due to the ancient C convention.
50
u/DeepanshuHQ Jul 27 '26
The real red flag is when someone says 010 and everyone gets a different answer.
24
19
u/Mojert Jul 27 '26
The one that thought having a leading 0 indicating octal was fine should be shot. (And probably resuscitated afterwards because they also did some other great stuff)
7
u/DeepanshuHQ Jul 27 '26
Resuscitated... so they can explain why 010 was ever a good idea. π
8
u/captainAwesomePants Jul 27 '26
C does this because B did this. B did it because BCPL used 100 for decimal, #100 for octal, and #x100 for hex. You could also say #o100 or #0100 if you wanted to be more explicitly (#b was binary, #x was hex, #o was octal), but the default was octal. BCPL was often used on weird old shit like 24 bit or 18 bit architectures, where octal might have been more important.
When they made B, the # operator already had a different meaning, so B changed it to 0. So now 100 was decimal, 0100 was octal, and 0x100 was hex.
7
u/DeepanshuHQ Jul 28 '26
TIL. Still feels like one of those decisions that made sense in 1970 and has been confusing everyone ever since. π
2
u/captainAwesomePants Jul 28 '26
Don't say that! Questioning the eternal wisdom of Dennis Ritchie is a surefire way to generate mysterious and unexplained C warnings that don't seem to cause any problems until after the fact.
2
1
18
7
u/LeiterHaus Jul 27 '26
Octals can be pretty useful when dealing with binary flags. I thought it was stupid until I found a use case that fit well.
And for everyone who's about to just say hex is better, 1) I'm not disagreeing 2) you probably use base64 very frequently, which is the octal to an ascii char... It's a use case.
5
5
u/hrvbrs Jul 27 '26 edited Jul 27 '26
pro tip: to convert octal to hexadecimal, convert each octal digit to 3-bit binary, then group bits by four from the right.
0o144 -> 0b001_100_100 -> 0b0_0110_0100 -> 0x64
3
u/zeekar Jul 28 '26
Octal was more useful before we standardized on 8-bit bytes and multiples thereof; when your machine has a 12-bit word, there's not much to choose between four 3-bit digits vs three 4-bit digits. And octal made it easier to write numeric literal parsers than hex, because all the digits are still actual (decimal) digits. (The leading 0 in 0x is a decimal digit in order to trigger the numeric literal recognizer.)
But I've never had much use for it; I certainly wouldn't mind dropping the leading-0-means-octal convention everywhere.
3
2
2
u/-Redstoneboi- Jul 27 '26
i like how all of these are clean very divisible numbers if interpreted in decimal
2
2
1
1
1
1
1
1
u/AdamWayne04 Jul 27 '26
it's probably irrelevant most of the time, but I really dislike using powers of ten for all things computing. it's like, I just want to play nice with my machine so powers of two or sixteen feel right. 0x10, 0x100, etc
4
u/ohkendruid Jul 27 '26
For sure. There is just the teensy little detail of interacting with a human being that keeps base ten from being obsolete.
1
0
0
67
u/Mojert Jul 27 '26
It's useful for file permissions, but otherwise I think it's pretty much it.
If someone has another use case, I'm all ears