Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are leading zeroes used to represent octal numbers?

I've always wondered why leading zeroes (0) are used to represent octal numbers, instead of — for example — 0o. The use of 0o would be just as helpful, but would not cause as many problems as leading 0es (e.g. parseInt('08'); in JavaScript). What are the reason(s) behind this design choice?

like image 723
Hauleth Avatar asked Jul 14 '12 11:07

Hauleth


People also ask

What is the purpose of leading zeros?

Leading zeros are used to make ascending order of numbers correspond with alphabetical order: e.g., 11 comes alphabetically before 2, but after 02. (See, e.g., ISO 8601.)

How are octal numbers represented?

The octal numbers, in the number system, are usually represented by binary numbers when they are grouped in pairs of three. For example, an octal number 128 is expressed as 0010102 in the binary system, where 1 is equivalent to 001 and 2 is equivalent to 010.

Do octal numbers start with 0?

An integer literal that starts with 0 is an octal number, much like a number starting with 0x is a hexadecimal number.

Why use octal instead of decimal?

The main advantage of using Octal numbers is that it uses less digits than decimal and Hexadecimal number system. So, it has fewer computations and less computational errors. It uses only 3 bits to represent any digit in binary and easy to convert from octal to binary and vice-versa.


1 Answers

All modern languages import this convention from C, which imported it from B, which imported it from BCPL.

Except BCPL used #1234 for octal and #x1234 for hexadecimal. B has departed from this convention because # was an unary operator in B (integer to floating point conversion), so #1234 could not be used, and # as a base indicator was replaced with 0.

The designers of B tried to make the syntax very compact. I guess this is the reason they did not use a two-character prefix.

like image 189
n. 1.8e9-where's-my-share m. Avatar answered Oct 12 '22 03:10

n. 1.8e9-where's-my-share m.