Skip to content
Velloreth
Programming 5 min read

Binary and Hex Explained for Developers

Binary is base 2 and hex is base 16. Learn the conversions, the bit operations, and why hex appears everywhere in programming.

By

Bits and bytes

A bit is a 0 or a 1. A byte is 8 bits.

The place values in a byte, from the right:

Position76543210
Value1286432168421

A byte holds 0 to 255, which is 2^8 values.

A nibble is 4 bits, which is one hex digit.

Binary to decimal

Multiply each bit by its place value and add.

1011
= 1×8 + 0×4 + 1×2 + 1×1
= 8 + 0 + 2 + 1
= 11

Decimal to binary: subtract the largest power of two that fits, and repeat.

13
13 - 8 = 5   -> 1
 5 - 4 = 1   -> 1
 1 - 2 < 0   -> 0
 1 - 1 = 0   -> 1
= 1101

Hex

Hex is base 16, using 0-9 and a-f.

HexDecimalBinary
000000
110001
770111
881000
a101010
f151111

One hex digit is exactly four bits. This is the property that makes hex useful.

A byte is two hex digits.

0xff   = 1111 1111 = 255
0x0a   = 0000 1010 = 10
0x80   = 1000 0000 = 128

The 0x prefix marks a hex literal in most languages.

Hex to decimal: multiply each digit by 16 to its position.

0x2a
= 2×16 + 10×1
= 32 + 10
= 42

Where hex appears

A colour. #ff8800 is red 255, green 136, blue 0.

A memory address. 0x7ffee4b2c9a0.

A hash. A SHA-256 digest is 32 bytes, written as 64 hex digits.

A Unicode code point. U+1F600 is the grinning face emoji.

A byte in a file. A PNG starts with 89 50 4e 47, which is the magic number.

A permission mask. 0o755 is octal, which is base 8, and each digit is three bits.

Bitwise operations

OperatorNameEffect
&AND1 where both are 1
|OR1 where either is 1
^XOR1 where they differ
~NOTflips every bit
<<left shiftmultiply by 2 per position
>>right shiftdivide by 2 per position

AND masks a value.

const FLAG_READ = 0b001;
const FLAG_WRITE = 0b010;
const FLAG_EXEC = 0b100;

let perms = FLAG_READ | FLAG_WRITE;   // 0b011
const canRead = (perms & FLAG_READ) !== 0;  // true

OR sets a flag.

perms |= FLAG_EXEC;   // 0b111

XOR toggles a flag.

perms ^= FLAG_WRITE;  // 0b101

A shift packs and unpacks.

// Pack two 8-bit values into one 16-bit value
const packed = (high << 8) | low;

// Unpack
const high = (packed >> 8) & 0xff;
const low = packed & 0xff;

A shift is a fast multiply or divide by a power of two.

x << 1   // x * 2
x >> 1   // x / 2, rounded down

A common trick: test whether a number is even.

const isEven = (n & 1) === 0;

Two’s complement

A signed integer uses two’s complement, where the most significant bit has a negative weight.

In 8 bits:

01111111 = 127
00000001 = 1
00000000 = 0
11111111 = -1
10000000 = -128

The top bit is -128, so 11111111 is -128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 = -1.

Why it is used: addition and subtraction work the same for signed and unsigned values, so the hardware needs one circuit.

The range of an 8-bit signed value is -128 to 127, and of an unsigned value 0 to 255.

The overflow wraps: 127 + 1 is -128 in an 8-bit signed value.

Floating point

A float is not a decimal. It is a sign, an exponent and a mantissa in base 2.

The consequence: 0.1 and 0.2 cannot be represented exactly, so 0.1 + 0.2 !== 0.3.

0.1 + 0.2   // 0.30000000000000004

The fix for money: use an integer of the smallest unit, such as cents, or a decimal library.

const total = 10 + 20;  // cents
const display = (total / 100).toFixed(2);

Common mistakes

Confusing a bit and a byte. A byte is 8 bits, and a network speed in bits is 8 times slower than the same number in bytes.

Assuming a float is exact. It is not, and a comparison of two floats needs a tolerance.

A shift that overflows. A left shift past the width of the type discards the high bits.

A signed right shift on a negative number. >> keeps the sign and >>> fills with zeros, and the difference matters.

Reading hex as decimal. 0x10 is 16, not 10.

A bitmask that is not a power of two. A flag must be a single bit, or the test is ambiguous.

Forgetting the operator precedence. & binds tighter than == in some languages, so a & b === c is not what it looks like. Use parentheses.

A quotable summary

A byte is 8 bits, and each bit is a power of two, so a byte holds 0 to 255. Hex is base 16, and one hex digit is exactly four bits, so a byte is exactly two hex digits, which is why hex is used for a colour, a hash and a memory address. A bitwise operation works on the individual bits, and two’s complement is how a signed integer represents a negative number, with the top bit carrying a negative weight.

For the related topics, see bitwise-operators-guide and floating-point-precision. For the broader context, unicode-and-encoding and hashing-vs-encryption.

Frequently asked questions

Why is hexadecimal used instead of binary?
Because one hex digit is exactly four bits, so a byte is exactly two hex digits. Binary is verbose and error-prone to read, and decimal does not align with the byte boundary. Hex is a compact, exact representation of the bits.
How do I convert binary to decimal?
Multiply each bit by its place value and add. The place values from the right are 1, 2, 4, 8, 16, 32, 64, 128. So 1011 is 8 + 0 + 2 + 1 = 11.
What is two's complement?
It is the representation of a signed integer where the most significant bit has a negative weight. In an 8-bit value, the top bit is -128, so 11111111 is -1. It is used because addition and subtraction work the same for signed and unsigned values.
What does a bitwise AND do?
It compares each bit of two values and produces a 1 only where both are 1. It is used to test a flag, to mask a value, and to check whether a number is even, because the low bit of an even number is 0.
What is a bit shift?
A left shift moves the bits to the left and fills with zeros, which multiplies by two for each position. A right shift moves them to the right, which divides by two. A shift is faster than a multiply in some contexts, and it is used to pack and unpack values.
Why does a colour use hex?
A colour is three bytes, one each for red, green and blue. Each byte is two hex digits, so #ff8800 is red 255, green 136, blue 0. The hex form is a compact and exact way to write the three bytes.