Number Literal Syntax by Language
A cross-language matrix for writing and parsing binary, octal, decimal, and hexadecimal integers - plus the precision traps each language hides.
| Feature | JavaScript / TypeScript | Python | Go | Rust | C / C++ | Java | Shell (bash) |
|---|---|---|---|---|---|---|---|
| Binary literal | 0b11111111 | 0b11111111 | 0b11111111 | 0b1111_1111 | 0b11111111 (C23 / C++14) | 0b11111111 | — (use $((2#11111111))) |
| Octal literal | 0o755 | 0o755 | 0o755 (or 0755) | 0o755 | 0755 (leading zero!) | 0755 (leading zero!) | $((0755)) or $((8#755)) |
| Hex literal | 0xFF | 0xFF | 0xFF | 0xFF | 0xFF | 0xFF | $((0xFF)) or $((16#FF)) |
| Arbitrary precision integer | 123n (BigInt literal) | native (all ints) | math/big.Int | num-bigint crate | external (GMP) | java.math.BigInteger | bc / python -c |
| Digit separator | 1_000_000 | 1_000_000 | 1_000_000 | 1_000_000 | 1'000'000 (C++14) | 1_000_000 | — |
| Parse string in base N | parseInt("ff", 16) / BigInt("0xff") | int("ff", 16) | strconv.ParseInt("ff", 16, 64) | i64::from_str_radix("ff", 16) | strtol("ff", NULL, 16) | Integer.parseInt("ff", 16) | $((16#ff)) or bc |
| Format value in base N | (255).toString(16) | hex(255) / f"{255:x}" | strconv.FormatInt(255, 16) | format!("{:x}", 255) | printf("%x", 255) | Integer.toHexString(255) | printf "%x\n" 255 / bc |
Traps worth memorizing
The leading-zero octal trap
In C, shell arithmetic, and pre-ES5 JavaScript, a bare leading zero means octal: 0755 is 493, and 089 is invalid or silently decimal depending on the parser. Modern languages moved to the explicit 0o prefix. Never pad decimal literals with zeros.
Java's leading-zero octal
Java supports octal literals with a leading zero: 0755 is 493. The 0o prefix is not supported. For a string, use Integer.parseInt("755", 8); never pad a decimal literal with zeros.
parseInt without a radix
JavaScript's parseInt("08") historically parsed as octal in some engines. Always pass the radix: parseInt("08", 10). BigInt("0o17")-style strings are prefix-safe by construction.
The 2^53 wall
JavaScript numbers are IEEE-754 doubles: integers above 9007199254740991 (2^53 - 1) round silently. Use BigInt (123n or BigInt("...")) for anything larger - IDs, hashes, byte counters.
bash base#digits
bash arithmetic accepts base#digits for bases 2-64: $((16#FF)) is 255. Digits above 9 use lowercase letters, then uppercase, then @ and _ for the highest bases.
Convert between these bases
The universal base converter handles any base 2-36 with arbitrary precision, the octal to decimal converter goes deeper on octal, and the test vectors page gives you verified values to check your own parser against.