Computer Science

Number Bases: Binary Numbers, Hexadecimal Numbers

You are certainly already familiar with the "base-10" number system that we all use, with its "ones place," its "tens place," its "hundreds place," and so on. You probably don't think about it all the time because it's been long time since you had to learn that, but back in the day, here's how it was explained to you:

Base 10 = "Decimal"

Decimal numbers in base-10, have 10 symbols (0-9), and are written and thought of in terms of "places":

106105104103102101100
millionshundred thousandsten thousandsthousandshundredstensones

We're already familiar with base-10 so there's not much explaining that needs to happen, although it's helpful to note that the number 43710 is:

(4 × 100) + (3 × 10) + (7 × 1) = 
   400    +    30    +    7    =     437

This same process is how numbers in other bases can be analyzed.

Base 2 = "Binary"

The same system works for base-2 (binary), with two symbols (0-1), and written and thought of in terms of places:

26252423222120
64s32ssixteenseightsfourstwosones

So, 101000112 =>

(1 × 128) + (1 × 32) + (1 × 2) + (1 × 1) = 
   128    +    32    +    2    +    1    =     163

What is Binary used for?

These 0s and 1s are used to operate the digital hardware, the memory locations and the logic gates in a digital computer that solve problems and produce output. While these digits are hidden ("encapsulated") well beneath the keyboards, mice, and monitors that we use to interact with our computers, ultimately, everything that a computer does is represented by and manipulated by the binary numbers that encode those processes

In fact, before we had keyboards, mice, and monitors, the personal computer was simply a set of switches with lights above them. Computer code was entered using the individual switches—a long, painful, error-prone process—and output was read from the computer as a series of flashing lights on a display panel.

Why do I need to know Binary?

Most people who use a computer don't need to know the details of machine language, and how specifically the 0s and 1s—those "binary digits," or bits—are used to manipulate those machines. But some people do. Some data is managed as a series of bits, and "bit-wise operations" can be use to process certain types of data very efficiently.

If you use Python and want to see binary numbers, use the bin() function to convert an integer to binary:

print(bin(13))
'0b1101'

The 0b in front of the number indicates that you're looking at a binary value. The 1101 indicates the binary value equivalent of the decimal number 13.

For everyone, there's something to be said for understanding on a conceptual level how digital computers work. Here's a programmer entering a program by hand onto such a machine. The resulting program displays (in binary, of course!) the prime numbers between 2 and 255.

Base-16 : Hexadecimal

You should also know about base-16 (hexadecimal), with 16 symbols (0-9,a-f), and written and thought of in terms of places:

163162161160
4096s256ssixteensones

So af316 =

(10 × 256) + (15 × 16) + (3 × 1) = 
   2560    +    240    +    3    =     2803

Examples:

What is hexadecimal used for?

Hexadecimal (base-16) values are used for a number of purposes in computers. Eight bits (one "byte" were commonly used to represent values, memory addresses, and instruction codes at one point, and 8 bits (representing 256 values, from 0-255) are easily represented by a 2-digit hexadecimal value.

One of the most common places you can see hexadecimal values still used is in defining HTML color codes in a web browser.

The color #ff7f00 is a six-character hexadecimal value that, when split into 2-digit combination, represents the red-green-blue values of a color on a webpage:

Those colors, when combined, produce the color ff7f00

You can play with various colors and hexadecimal color codes online at websites like this one.

Why do I need to know hexadecimal?

You may not. The use of hexadecimals in computer programming became popular because a binary value like 10011011 (in base-2) was easier to read as 9b (base-16), and converting between base-2 and base-16 was relatively easy to do: split the binary up into 4-bit groups to get 1001 (= '9' in hex) and 1011 (= 11 in decimal, or 'b' in hex).

But hex values do show up all sorts of places in computers, and you may find need to refer to them.