The Ups and Downs of Binary and Hexadecimal

Practicalities of the base-2 and base-16 number system in computing

Jack Gifford
3CS Blog
6 min readJan 31, 2017

--

The decimal numerical system, is the numerical base we most often associate with mathematics, and representing numbers. Computers represent numbers with a different value system, one that makes mathetmatical operations compatible with the discrete electronics that power it.

What’s binary?

Binary (base-2) is a numerical system that comprises of just two symbols; 0 and 1. As this system is simply one of many ways of representing numbers we can perform the same mathematical operations that we could with any other system. Computers use binary as it’s the simplest method to implement. As a computer is just a series of electrical circuits, it’s capable of only two states; on or off. This provides two states to perform calculations, and run processes. It’s much easier to model this with a binary system.

Mathematical Theory

A number system is simply a way of representing numbers, using different symbols to represent the same thing; numbers. As humans we’re used to the decimal (base-10) system, it’s believed cultures adopted this system due to the ten fingers on our hands that makes it extremely easy to visualise. With each finger to the right representing a value 10 times larger than the one that came before it. So with one digit we can represent 1⁰¹ values, with two we represent 10² or 100 values, 3 represents 10³ or 1000 values, 4 represent 10⁴ or 10 000 values and so on. We can apply this same logic to the binary system. A single digit can represent ²¹ values, with two values we represent ²² or 4 values, 3 represents ²³ or 8 values, 4 represents ²⁴ or 16 values.

From this we’ve established a pattern; the amount of values we can represent with digits is given by (base value)^(number of digits)

It’s important to note numeral systems are just systems for expressing numbers.

What’s hexadecimal?

Hexadecimal is a base-16 numerical system, as such it’s contains 16 symbols. In computing we use it to express binary representations of numbers in a format that’s much easier to read.

Despite there being an overlap in characters between these number systems they are not the interchangable! 10 in binary is not the same as 10 in decimal, and 10 in hex!

Advantages of Hex

Whilst computers don’t directly use hex, we use it for representation a lot. As it helps humans more easily interpret bytes of information.

A byte is 8 bits of data grouped together, they’re capable of storing 256 different values, ranging from 0–255 or 0000 0000–1111 1111 through decimal and binary respectively. Whilst this works great for machines, people are used to working with a decimal system and as such binary is quite an abstract concept, one that’s extremely far removed from our association with numeracy and takes a lot of time to interpret, especially when dealing with larger values.

Since a byte is 8 bits (8 binary digits) we can logically divide it into two groups of four bits. Since 4 bits can represent a total of 16 values we can model those four bits with a single hexadecimal digit; which also contains 16 values.

When we break a bit into two groups of four, each group can represent 2⁴ or 16 numerical values with four digits. Comparitively a single digit of hex can represent 16¹ different numerical values, and two can store 16² or 256 different numerical values. Meaning we only need two hex digits to represent a byte of data.

Why is a byte 8 bits?

It’s mostly historical reasoning. As computers can only process binary a system was developed so they could produce text on a screen. The character set was ASCII, which contained a total of 128 values, the upper, and lower case english alphabet (A-Z & a-z), special characters ({ , . > ? ), and 33 reserved non-printing characters (ESC, Space). 128 values requires 7 bits of binary (2⁷ = 128), as it makes sense to keep everything even, an additonal bit was added to the end called a parity bit; a crude form of error checking. This left 8 bits being the smallest addressable unit of memory. From there we’ve moved onto character sets that contain thousands of different characters that add support for the scripts of many different languages, mathematics scripts, and emojis, and yet a byte being 8 bits has stuck around for backwards compatibility in older software.

Binary is ommited for readability

Converting Between Decimal, Hex, and Binary

Another great aspect of hex, is how quick and easy it is to translate between hex, and binary compared to decimal. There’s no mathematical calculation required either.

Binary to Hex

  • Split the data into groups of 4.
  • Using the table provided higher up on the post, find the equivalent hexadecimal representation
  • Add 0x to the front to indicate it’s hex.

Example — 00101010

  • 0010 1010
  • 2 A
  • 0x2A

Hex to Binary

  • For each hex digit use the table to find the equivalent four bits of binary.

Example — 0x2A

  • 2 = 0010, A = 1010
  • 0010 1010

Decimal to Binary

  • Write down the decimal value and divide by two
  • If the value is even, then the there is no remainder (0). If it’s odd then the result will not divide cleanly, and we’ll be left with a remainder (1).
  • Repeat this until you reach zero. The most signifcant bit will occur at the bottom.

Example — 42

  • 42/2 => result = 21 remainder = 0 (Least Significant Bit)
  • 21/2 => result = 10 remainder = 1
  • 10/2 => result = 5 remainder = 0
  • 5/2 => result = 2 remainder = 1
  • 2/2 => result = 1 remainder = 0
  • 1/2 => result = 0 remainder = 1 (Most Significant Bit)
  • (00)10 1010 (Added two leading zeros for storing as a byte)

Binary to Decimal

  • Beneath each digit write down its equivalent power of two.
  • Sum the powers that correspond to each value that’s a one.

Example — 00101010

  • 0 => 2⁸ = 128
  • 0 => 2⁷ = 64
  • 1 => 2⁶ = 32
  • 0 => 2⁵ = 16
  • 1 => 2⁴ = 8
  • 0 => 2³ = 4
  • 1 => 2² = 2
  • 0 => 2¹ = 1
  • 0 + 0 + 32 + 0 + 8 + 0 + 2 + 0 = 42

Hex to Decimal

  • Get the decimal equivalent for each digit from the table above.
  • Multiply every digit with 16^to-the-digit-position
  • Sum the results

Example — 0x2A

  • (2 * 16¹) + (10 * 16⁰)
  • 32 + 10
  • 42

Decimal to Hex

  • Divide the decimal value by 16
  • If the value is even, then the there is no remainder (0). If it’s odd then the result will not divide cleanly, and we’ll be left with a remainder (This is the value we find the hex equivalent for).
  • Repeat this until you reach zero. The most signifcant bit will occur at the bottom.

Example — 42

  • 42/16 => Result = 2 remainder = 10 (A)
  • 2/16 => Result = 0 remainder = 2 (2)
  • 0x2A

Summary

This information might not seem to be very relevant to modern computing, as much almost all of this information is nothing more than trivia, the operations of a computer are abstracted from users, and now programmers with the adoption of higher level languages. Regardless of this it’s important to appreciate the fundamental logic, and history behind the tools most prevalent and influential on our modern society.

--

--