Menu

Binary

Binary and Hexadecimal Basics

Contents

  1. Binary
  2. Number of States in a Bit Pattern
  3. Unsigned and Signed Binary Numbers
  4. Hexadecimal

Binary is the number system that computers use to store and process data. It is a base-2 system, so it uses only two digits: 0 and 1. Denary, the number system you use every day, is base 10 and uses the ten digits 0 to 9.

Why computers use binary

Digital circuits use components that can reliably hold one of two stable states: a high voltage or a low voltage, which you can think of as on or off. These two states are easy to tell apart even when the voltage wobbles slightly, so the machine rarely misreads a value. The states are written as 1 and 0. If a circuit had to distinguish ten different voltage levels, a small change would be enough to turn one digit into another and the stored data would become unreliable.

Bits, nibbles and bytes

A bit is a single binary digit: one 0 or one 1. Groups of bits are given their own names:

  • A nibble is 4 bits.
  • A byte is 8 bits, which is exactly two nibbles.

Before a computer can work on anything, it must be converted into binary. Numbers, text, sound, graphics and the program instructions themselves are all stored as patterns of bits, so a piece of music and a photograph are handled in the same way once they are in memory.

Place values

Denary place values grow by powers of 10 from right to left: 1, 10, 100 and so on. Binary place values grow by powers of 2 from right to left: 1, 2, 4, 8, 16, because every step to the left doubles the value. To read a pattern, add the place values wherever there is a 1 and ignore the places holding a 0.

Place value8421
Bits1011
Value8021

Adding the values gives 8 + 2 + 1 = 11, so the nibble 1011 is 11 in denary.

A bit is one binary digit, and a bit pattern is a row of bits. With n bits you can make 2 to the power of n different patterns, written 2 ** n. Each pattern is a separate state, so each one can be given a different meaning or value.

Working it out

Two bits give four patterns: 00, 01, 10 and 11, so 2 ** 2 is 4. Five bits give 2 ** 5 = 32 patterns, and ten bits give 2 ** 10 = 1024. That is enough for every pupil in a very large school to have their own different ten-bit code.

Why one extra bit doubles the count

Start with the four two-bit patterns and put a 0 on the front of each one: 000, 001, 010 and 011. Now repeat with a 1 on the front: 100, 101, 110 and 111. You now have eight three-bit patterns, twice as many as before, because every old pattern produced two new ones. Add a fourth bit and the count doubles again to 16.

A tree splits into 0 and 1 at each level: 1 bit gives 2 patterns, 2 bits give 4 and 3 bits give 8, from 000 to 111. Each extra bit doubles the count.

The all-zero pattern counts as one of the states, so count every pattern, not just the ones with a 1 in them.

An unsigned binary pattern uses every bit for place value. No bit is set aside to signal a minus sign, so an unsigned pattern always stands for zero or a positive number.

States and the largest value

A pattern's states are the different combinations of bits it can show. A 3-bit pattern has 8 states, running from 000 up to 111, and each state stands for one value. The first of those values is zero.

PatternValue it stands for
0000
0011
0102
0113
1004
1015
1106
1117

Because zero takes up one of the states, the largest value an unsigned pattern can hold is always one less than the number of states. Those 8 states reach only as far as 7. A 4-bit pattern has 16 states, so its top value is 15 — saying 16 would describe the number of states, not the largest value.

The most and least significant bits

The most significant bit (MSB) is the leftmost bit of the pattern. It holds the biggest place value, so changing it alters the number far more than changing any other bit. The least significant bit (LSB) is the rightmost bit. Its place value is always 1, so flipping it changes the value by just 1.

In the pattern 10101 the MSB is the leftmost 1, worth 16, and the LSB is the final 1, worth 1. Reading the place values from left to right gives 16, 8, 4, 2 and 1, so the value is 1×16 + 0×8 + 1×4 + 0×2 + 1×1 = 21. Only that leftmost bit is worth 16, which is exactly why it earns the name “most significant”.

Hexadecimal is a base-16 number system, so it is built from 16 different symbols. Ten of them are the digits 0 to 9, standing for the values zero to nine. The other six are the letters A to F, standing for the values ten to fifteen. Once you reach F you have used every symbol, so you carry into the next column, just as 9 rolls over to 10 in denary.

Four bits per digit

The reason hexadecimal suits computing so well is that each hexadecimal digit represents exactly 4 bits, which is one nibble. There are 16 possible patterns of 4 bits and 16 hexadecimal symbols, so the two line up one-to-one and conversion is quick in both directions.

  • Binary to hexadecimal: split the bits into groups of four starting from the right, pad the leftmost group with leading zeros if it is short, then swap each group for its hexadecimal digit.
  • Hexadecimal to binary: swap each hexadecimal digit for its 4-bit pattern, keeping any leading zeros inside each group.

For example, take 1011010111. Grouping from the right gives 10 1101 0111, and padding the short group gives 0010 1101 0111. Now 0010 is 2, 1101 is D and 0111 is 7, so the pattern is 2D7. Going back, 2D7 becomes 0010 1101 0111, and dropping the leading zeros returns 1011010111.

Binary 1011010111 split into groups of four from the right (10, 1101, 0111), padded to 0010 1101 0111, with each group swapped for a hexadecimal digit to give 2D7.

Why programmers use it

Hexadecimal is a compact way of writing binary. One byte is eight bits, so it needs exactly two hexadecimal digits, and a long run of 0s and 1s can be copied, compared and checked as a much shorter string that is easier to read, easier to remember and less likely to be mistyped. Programmers meet it wherever binary values are shown or written, such as memory addresses, colour settings and error codes.

Hexadecimal does not reduce storage space: the computer stores exactly the same binary value, and only the written version gets shorter. It is a human-friendly notation, so any hexadecimal you type is converted to binary before the processor uses it.

Also useful