Alternative Radices: Introduction

Value vs. representation and notation.

Overview

The topic of alternative number bases (i.e. bases other than the usual 10) is one that can be very intimidating at first. Many of us first encountered alternate number bases in middle school, felt uncomfortable with the idea at the time, and promptly forgot about it after finishing that chapter of our Mathematics textbook. However, like set theory and the modulo operation, alternative number bases (2, 8, and 16, in particular) turn out to be very important in programming.

Notes

Value vs. representation

One important fact to keep in mind is that changing the number base changes the representation of a value, but not the value itself. The quantity we usually refer to as 42 (for example) doesn’t change, no matter what radix we use; it’s only the representation that changes. If we have 42 fish in a tank, it’s still the same quantity of fish even if we call it 0x2A (hexidecimal, or base 16), 052 (octal, base 8), or 0b101010 (binary, base 2). (The literals shown are expressed in the forms most widely recognized by compilers. See the following section, “Notation”, for more information.)

Notation

When using radices other than 10 in Java numeric literals, the prefixes 0x, 0, and 0b (the case of x or b is irrelevant) aren’t part of the values themselves; they simply cue the compiler to use the corresponding number base when interpreting a numeric literal: 0x for 16, 0 for 8, and 0b for 2.

When parsing source code, the Java compiler recognizes bases 2, 8, 10, 16 in integer literals, but only bases 10 and 16 for floaing-point literals.1

If we’re not writing code, we might use conventional mathematics notation, and indicate the radix in a subscript. For example, $42_{10}$, $\text{2A}_{16}$, $52_8$, and $101010_2$ all represent the same quantity. (We conventionally leave out the subscript for base-10 quantities—just as the Java compiler recognizes base-10 literals without any special prefix.)

  1. Actually, the hexadecimal format for floating-point literals may be more accurately referred to as a decimal/hexadecimal hybrid format, with a base-16 part expressing the mantissa, and a base-10 portion expressing the exponent. Not surprisingly, this format—part of the legacy Java inherited from C—isn’t used very often.