Hex seems convenient for bytes, while octal takes more digits and doesn't line up as neatly. Are there practical cases where octal is easier to work with?
When is octal actually useful in programming?
Answers
Octal is handy when the bits come in groups of three. Each octal digit maps directly to three bits, just as each hex digit maps to four.
File permissions are a good example. The digits in 755 represent the read, write and execute bits for the owner, group and others respectively. A 7 means all three permissions, while a 5 means read and execute. Hex would represent the same bits, but its digit boundaries wouldn't match those permission groups.
Reply to this answer
Hex wins for most byte-oriented work because two hex digits fit exactly into eight bits. Octal digits cross byte boundaries, so a byte's maximum is 377 rather than a tidy string of identical digits.
That doesn't make the stored number less efficient. The notation is mainly a way for people to read and write the value. Pick the one that makes the relevant bit boundaries obvious.
Reply to this answer
There's some history behind it too. Older machines didn't all use eight-bit bytes. With an 18-bit or 36-bit word, octal divides the word neatly into six or twelve digits. Some instruction encodings also have three-bit fields, which can make octal useful when inspecting them.
Reply to this answer
Hardware registers can have three-bit fields too. If you're looking at a register organized that way, octal can show the field values directly instead of making you mentally split up hex digits.
Reply to this answer
One smaller use is a numeric-only display. Octal uses digits 0 through 7, so a display that can't show hex letters can still present bit fields with each digit corresponding to three bits.
Reply to this answer
Add your answer
Answers are reviewed before publication.
d64+paperhook_309:
Also, octal digits only run from 0 through 7. A permission value containing a 9 isn't octal.