Programming Number Formats

How Languages Handle Numbers

Understand Number Formats

Every programming language needs ways to represent numbers. 理解する integer types, floating-point formats, and number base notations helps you write correct code and avoid subtle bugs related to precision, overflow, and representation.

Integer Types

Fixed-Size Integers

TypeBitsSigned RangeUnsigned Range
int88-128 to 1270 to 255
int1616-32,768 to 32,7670 to 65,535
int3232-2.1B to 2.1B0 to 4.3B
int6464±9.2 quintillion0 to 18.4 quintillion

Language Examples

  • C/C++: int, long, int32_t, uint64_t
  • Java: byte, short, int, long
  • Rust: i8, i16, i32, i64, u8, u16, u32, u64
  • Go: int8, int16, int32, int64

Floating-Point Numbers

IEEE 754 Standard

  • float (32-bit): ~7 decimal digits precision
  • double (64-bit): ~15-16 decimal digits precision

Structure

  • Sign bit (1 bit)
  • Exponent (8 or 11 bits)
  • Mantissa/significand (23 or 52 bits)

The Famous Problem

0.1 + 0.2 = 0.30000000000000004

This happens because 0.1 and 0.2 can't be represented exactly in binary floating-point.

Number Base Notation by Language

LanguageBinaryOctalHex
JavaScript0b10100o120x0A
Python 30b10100o120x0A
C/C++/Java0b1010*0120x0A
C#0b10100x0A
Ruby0b10100o120x0A
Go0b10100o120x0A
Rust0b10100o120x0A

*C: Binary literals added in C23; C++14 and Java 7+ support them.

JavaScript Numbers

Characteristics

  • All numbers are 64-bit floating-point (IEEE 754)
  • Safe integer range: ±9,007,199,254,740,991 (2⁵³-1)
  • BigInt for larger integers (ES2020+)

Number Literals

let dec = 255;        // Decimal
let hex = 0xFF;       // Hexadecimal
let bin = 0b11111111; // Binary
let oct = 0o377;      // Octal
let big = 9007199254740992n; // BigInt

Gotchas

  • 0.1 + 0.2 !== 0.3 (floating-point)
  • parseInt('08') === 8 (fixed in modern JS)

Python Numbers

Characteristics

  • Integers have arbitrary precision (no overflow)
  • Floats are 64-bit IEEE 754
  • Complex numbers built-in

Number Literals

dec = 255          # Decimal
hex_num = 0xFF     # Hexadecimal  
bin_num = 0b11111111  # Binary
oct_num = 0o377    # Octal
complex_num = 3+4j # Complex

Conversions

bin(255)  # '0b11111111'
hex(255)  # '0xff'
oct(255)  # '0o377'
int('FF', 16)  # 255

Arbitrary Precision Numbers

When Needed

  • Cryptography (large keys)
  • Financial calculations (exact decimal)
  • Scientific computing (extreme precision)

Language Support

  • Python: int (always arbitrary precision)
  • JavaScript: BigInt (for integers)
  • Java: BigInteger, BigDecimal
  • C#: BigInteger, decimal

Example: JavaScript BigInt

const big = 9007199254740993n;
const result = big * 2n;  // Must use n suffix

Formatted Output

JavaScript

num.toString(2)   // Binary string
num.toString(16)  // Hex string
num.toFixed(2)    // 2 decimal places

Python

f"{255:b}"    # '11111111' (binary)
f"{255:x}"    # 'ff' (hex)
f"{255:08b}"  # '11111111' (padded)

C/C++

printf("%d", num);   // Decimal
printf("%x", num);   // Hex (lowercase)
printf("%X", num);   // Hex (uppercase)
printf("%o", num);   // Octal

Bitwise Operations

Available in most languages for integer types:

OperationSymbolExample
AND&5 & 3 = 1
OR|5 | 3 = 7
XOR^5 ^ 3 = 6
NOT~~5 = -6
Left shift<<5 << 1 = 10
Right shift>>5 >> 1 = 2

まとめ

Programming languages handle numbers through various types: fixed-size integers (int32, int64), floating-point (float, double), and arbitrary precision types (BigInt, BigDecimal). 理解する base notation (0b, 0o, 0x prefixes) and the limitations of each type—especially floating-point precision issues—helps avoid subtle bugs. When exact decimal arithmetic matters (finance, currencies), use specialized decimal types rather than floating-point.

関連記事

Programming Number Formats: Language Guide | YounitConverter