CCA Guide
Number Systems

Number Systems

Curated by Last modified: 3 min read

View Markdown

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

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:

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

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.

Bits and bytes

UnitSize
BitOne binary digit, 0 or 1
Byte8 bits
Kibibyte (KiB)1,024 bytes
Mebibyte (MiB)1,024 KiB
Gibibyte (GiB)1,024 MiB

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.

Base 8: octal

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

int x = 034; // Decimal 28

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.

Recursive decimal-to-octal exercise

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:

int x = 0x4CB3;

Recursive decimal-to-hexadecimal exercise

Original digit mapping and recursive helper:

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);
}

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.

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 · Type casting · Variables

Linked to this page