# Number Systems

Source: https://cca.twango.dev/AP-Computer-Science/number-systems

# Number Systems

## Base 10: decimal

Digits: `0` through `9`. This is the familiar mathematical notation and the usual notation for integer literals in Java.

## Base 2: binary

Digits: `0` and `1`.

### Binary literals in Java

```java
int x = 0b100110; // Decimal 38
```

A prefix chooses the notation of the literal; the resulting variable still stores an `int` value.

### Recursive decimal-to-binary exercise

Original algorithm, for nonnegative inputs small enough to avoid `int` overflow:

```java
int decimalToBinary(int decimal) {
    if (decimal == 0) return 0;
    return decimalToBinary(decimal / 2) * 10 + decimal % 2;
}
```

> [!note] Review clarification
> This constructs a **decimal integer whose digits spell the binary representation**, not a different kind of integer storage. For example, input `6` yields the decimal integer `110`. Many larger results overflow `int`. For a representation as text, Java provides `Integer.toBinaryString(decimal)`.

Source: [Integer.toBinaryString](https://docs.oracle.com/javase/8/docs/api/java/lang/Integer.html#toBinaryString-int-).

### Bits and bytes

| Unit | Size |
| --- | --- |
| Bit | One binary digit, `0` or `1` |
| Byte | 8 bits |
| Kibibyte (KiB) | 1,024 bytes |
| Mebibyte (MiB) | 1,024 KiB |
| Gibibyte (GiB) | 1,024 MiB |

> [!note] Correction to the original unit labels
> The original notes called these binary multiples KB, MB, and GB. Their unambiguous names are KiB, MiB, and GiB. Decimal kB, MB, and GB use factors of 1,000 instead.

Source: [NIST: binary prefixes](https://physics.nist.gov/cuu/Units/binary.html).

## Base 8: octal

Digits: `0` through `7`. A leading zero indicates an octal integer literal in Java:

```java
int x = 034; // Decimal 28
```

> [!warning] Correction to the original example
> The original literal `0384` is invalid because octal has no digit `8`. The replacement above demonstrates a valid octal literal.

Source: [Integer literals §3.10.1](https://docs.oracle.com/javase/specs/jls/se17/html/jls-3.html#jls-3.10.1).

### Recursive decimal-to-octal exercise

```java
int decimalToOctal(int decimal) {
    if (decimal == 0) return 0;
    return decimalToOctal(decimal / 8) * 10 + decimal % 8;
}
```

As with the binary exercise, this constructs a decimal digit encoding, is limited by `int` overflow, and assumes a nonnegative input. `Integer.toOctalString(decimal)` produces text instead.

## Base 16: hexadecimal

Digits: `0` through `9`, then `A` through `F` for values 10 through 15. The Java prefix is `0x`:

```java
int x = 0x4CB3;
```

### Recursive decimal-to-hexadecimal exercise

Original digit mapping and recursive helper:

```java
String getHexString(int digit) {
    switch (digit) {
        case 10: return "A";
        case 11: return "B";
        case 12: return "C";
        case 13: return "D";
        case 14: return "E";
        case 15: return "F";
        default: return Integer.toString(digit);
    }
}

String decimalToHex(int decimal) {
    if (decimal == 0) return "";
    return decimalToHex(decimal / 16) + getHexString(decimal % 16);
}
```

> [!warning] Original edge case and corrected entry point
> The original notes correctly observed that `decimalToHex(0)` returns an empty string. Keep that empty string as the recursive stopping value, but use a wrapper to return `"0"` for the actual input zero. This exercise assumes nonnegative integers.

```java
String nonnegativeDecimalToHex(int decimal) {
    if (decimal < 0) {
        throw new IllegalArgumentException("Expected a nonnegative integer");
    }
    return decimal == 0 ? "0" : decimalToHex(decimal);
}
```

Java also provides `Integer.toHexString(decimal)`.

Related: [[recursion|Recursion]] · [[type-casting|Type casting]] · [[variables|Variables]]
