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 Velloreth
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:
| Position | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
|---|---|---|---|---|---|---|---|---|
| Value | 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
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.
| Hex | Decimal | Binary |
|---|---|---|
| 0 | 0 | 0000 |
| 1 | 1 | 0001 |
| 7 | 7 | 0111 |
| 8 | 8 | 1000 |
| a | 10 | 1010 |
| f | 15 | 1111 |
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
| Operator | Name | Effect |
|---|---|---|
& | AND | 1 where both are 1 |
| | OR | 1 where either is 1 |
^ | XOR | 1 where they differ |
~ | NOT | flips every bit |
<< | left shift | multiply by 2 per position |
>> | right shift | divide 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.
Related reading
For the related topics, see bitwise-operators-guide and floating-point-precision. For the broader context, unicode-and-encoding and hashing-vs-encryption.