Foundations

Integer

01. What is a Integer?

An Integer represents a whole number that can be positive, negative, or zero. Unlike real numbers (which include decimals or fractions), integers represent countable values and are fundamental to iteration counters, arrays, array indexing, and numeric identifiers.

In computer architecture, integers are represented using binary bits. They form the bedrock of low-level computational logic and memory addressing models.

02. Key Characteristics

  • Whole Numbers Only: Positive, negative, or zero (e.g., -5, 0, 42). No decimals or fractions.
  • Representation Models: Computers represent positive numbers in straight binary. Negative numbers are represented using the Two's Complement representation system, which allows addition and subtraction to use the same logic circuits.
  • Boundaries & Limits: Unlike abstract mathematics where integers are infinite, computer systems represent integers with a fixed number of bits (like 8, 16, 32, or 64 bits), which limits the values they can hold.

03. How Languages Represent It

Computers represent integer primitives using fixed binary widths:

  • 8-bit integer: Holds 256 unique values (signed: -128 to 127).
  • 16-bit integer: Holds 65,536 values (signed: -32,768 to 32,767).
  • 32-bit integer: Holds ~4.29 billion values (signed: -2,147,483,648 to 2,147,483,647). This is the default size on most computers.
  • 64-bit integer: Holds ~18.4 billion billion values (signed: -9.22 × 10^18 to 9.22 × 10^18).

When an arithmetic operation exceeds these bounds, it causes an Integer Overflow. In signed arithmetic, this typically causes a positive number to wrap around and become a negative number.

04. Language Comparison

How different languages approach integer primitives:

  • Python: Uses arbitrary-precision integers by default. An integer is an object whose memory footprint grows dynamically. You will not face integer overflows in Python.
  • Java: Strictly typed with fixed size boundaries. Primitives include byte (8-bit), short (16-bit), int (32-bit), and long (64-bit).
  • JavaScript: Traditionally uses double-precision floats (64-bit) for all numbers, limiting safe whole number ranges to -(2^53 - 1) to 2^53 - 1. Introduces BigInt for arbitrary precision.
  • C / C++: Platform-dependent primitive sizing. To ensure portability, C++11 introduced standardized fixed-width primitives like std::int32_t.

05. Code Examples

Select a tab below to see code declarations and usage:

python
# In Python, integers have arbitrary precision (they can grow as large as memory allows)
x = 42
y = -100
large_num = 123456789012345678901234567890

print(type(x))          # Output: <class 'int'>
print(large_num)        # Output: 123456789012345678901234567890
print(type(large_num))  # Output: <class 'int'>

06. Key Differences

Key comparative differences between static fixed-width types and dynamic types:

FeatureC / C++ / JavaPython
Memory AllocationFixed (e.g. 4 bytes for 32-bit int)Dynamic (variable object size)
Overflow RiskYes (wraps around silently or throws exception)No (automatically scales as needed)
Execution SpeedFast (mapped directly to CPU registers)Slower (requires allocation checks and object wrapping)

07. Common Mistakes

1. Silent Integer Overflow

Adding to the maximum value of a fixed-width type wraps around. For example, in Java/C: 2,147,483,647 + 1 results in -2,147,483,648. This leads to subtle logic bugs.

2. Division vs. Truncation

Dividing two integers in static languages yields an integer, truncating the remainder. For instance, in C/Java: 5 / 2 is 2, not 2.5. In Python: 5 / 2 yields 2.5 (float), while 5 // 2 performs floor division.

3. JavaScript Decimal Precision Loss

In JavaScript, standard numbers are floating-point values. Safe operations are guaranteed up to Number.MAX_SAFE_INTEGER (9,007,199,254,740,991). Exceeding this boundary without using BigInt leads to loss of precision (e.g. 9007199254740991 + 2 === 9007199254740992).