Programming Number Formats

How Languages Handle Numbers

Understand Number Formats

Every programming language needs maneras un/una represent numeros. Understanding integer types, floating-point formats, y numero base notations helps tu write correct code y avoid subtle bugs related un/una precision, overflow, y representation.

Integer Types

Fixed-Size Integers

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

Language Ejemplos

  • 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 o 11 bits)
  • Mantissa/significand (23 o 52 bits)

El/La Famous Problem

0.1 + 0.2 = 0.30000000000000004

This happens porque 0.1 y 0.2 puede't be represented exactamente in binary floating-point.

Number Base Notation by Language

LanguageBinaryOctalHex
JavaScript0b10100o120x0Un/Una
Python 30b10100o120x0Un/Una
C/C++/Java0b1010*0120x0Un/Una
C#0b10100x0Un/Una
Ruby0b10100o120x0Un/Una
Go0b10100o120x0Un/Una
Rust0b10100o120x0Un/Una

*C: Binary literals added in C23; C++14 y Java 7+ soportar them.

JavaScript Numbers

Characteristics

  • All numeros son 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 son 64-bit IEEE 754
  • Complex numeros 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 calculos (exacto decimal)
  • Scientific computing (extreme precision)

Language Support

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

Ejemplo: JavaScript BigInt

const big = 9007199254740993n;
const resultado = big * 2n;  // Must usar 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 la mayoria languages for integer types:

OperationSymbolEjemplo
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

Conclusion

Programming languages handle numeros through various types: fixed-size integers (int32, int64), floating-point (float, double), y arbitrary precision types (BigInt, BigDecimal). Understanding base notation (0b, 0o, 0x prefixes) y el/la limitations of cada type—especially floating-point precision issues—helps avoid subtle bugs. When exacto decimal arithmetic matters (finance, currencies), usar especializado decimal types rather than floating-point.

Articulos relacionados

Programming Number Formats: Language Guide | YounitConverter