# Integers (Signed vs Unsigned)

An **Integer** is a datum of integral data type, a data type that represents some range of mathematical integers. Integral data types may be of different sizes and may or may not contain negative values.

## 1. Signed vs. Unsigned

In computer science, the distinction between signed and unsigned integers determines whether the variable can represent negative numbers.

### Signed Integers
Can represent both positive and negative numbers.
*   **Mechanism:** Usually uses the **Two's Complement** representation. The most significant bit (MSB) is the "sign bit".
    *   `0` = Positive
    *   `1` = Negative
*   **Range (for $n$ bits):** $-2^{n-1}$ to $2^{n-1} - 1$.
*   **Example (8-bit):** -128 to 127.

### Unsigned Integers
Can only represent non-negative numbers (zero and positive).
*   **Mechanism:** All bits are used to store the magnitude of the number.
*   **Range (for $n$ bits):** $0$ to $2^n - 1$.
*   **Example (8-bit):** 0 to 255.

## 2. Common Bit Sizes

| Bits | Name (C/C++) | Signed Range | Unsigned Range |
| :--- | :--- | :--- | :--- |
| **8** | `char` / `int8` | -128 to 127 | 0 to 255 |
| **16** | `short` / `int16` | -32,768 to 32,767 | 0 to 65,535 |
| **32** | `int` / `int32` | -2 billion to 2 billion | 0 to 4 billion |
| **64** | `long` / `int64` | $-9 \times 10^{18}$ to $9 \times 10^{18}$ | 0 to $1.8 \times 10^{19}$ |

## 3. Integer Overflow

Overflow occurs when an arithmetic operation attempts to create a numeric value that is outside of the range that can be represented with a given number of bits.

### Unsigned Overflow (Wraparound)
Usually defined behavior. If you add 1 to the maximum value, it wraps around to 0.
*   `uint8_t x = 255;`
*   `x = x + 1;` -> `x` becomes `0`.

### Signed Overflow
In languages like C/C++, signed integer overflow is **Undefined Behavior** (UB), meaning anything can happen (crashes, security vulnerabilities). In Java, it wraps around (e.g., `MAX_INT + 1` becomes `MIN_INT`).

## 4. Two's Complement

The standard way computers represent negative integers. To get `-x`:
1.  Invert all bits of `x` (One's Complement).
2.  Add 1.

*   **Example:** `5` in 4-bit binary is `0101`.
    1.  Invert: `1010`
    2.  Add 1: `1011` (-5)

## 5. BigInt (Arbitrary-Precision Integers)

Standard integers (like `int32` or `int64`) have a fixed size and can overflow. **BigInt** allows you to store integers whose size is limited only by the available memory of the system.

*   **How it works:** The number is stored as an array of digits (or smaller integers) in memory. Arithmetic operations are performed using software algorithms rather than direct CPU instructions.
*   **Pros:** No overflow. Can represent numbers larger than the universe.
*   **Cons:** Significantly slower than fixed-width integers (CPU cannot process them in a single cycle).

### Language Support
*   **Python:** All integers are BigInts by default (since Python 3).
*   **JavaScript:** Use the `BigInt` type (append `n` to the number).
    ```javascript
    const huge = 9007199254740991n; 
    ```
*   **Java:** Use `java.math.BigInteger`.
*   **C#:** Use `System.Numerics.BigInteger`.

[[programming/common-syntax]]
[[programming/bit-manipulation]]
[[programming/floating-point-numbers]]