Programmers move between number systems constantly: decimal for people, binary for bits, and hexadecimal as a compact way to write binary. This guide shows how to convert between all three by hand, using the number 46 as a running example, plus the shortcuts that make it fast.

Positional Number Systems in One Paragraph

In any positional system, each digit is multiplied by the base raised to its position, counting from 0 on the right. Decimal 46 is 4×10¹ + 6×10⁰. Binary uses base 2 and hexadecimal uses base 16, but the rule is identical. Only the base changes.

SystemBaseDigits46 is written
Binary20–1101110
Octal80–756
Decimal100–946
Hexadecimal160–9, A–F2E

Binary to Decimal

Write the place values (powers of two) above each bit and add the ones that have a 1:

place:  32  16   8   4   2   1
bit:     1   0   1   1   1   0
sum:    32 + 8 + 4 + 2 = 46

Memorising the powers of two up to 2¹⁶ (65,536) makes this very quick: 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536.

Decimal to Binary

Method 1, repeated division by 2. Divide, write down the remainder, and repeat with the quotient. Read the remainders from bottom to top.

46 ÷ 2 = 23  r 0
23 ÷ 2 = 11  r 1
11 ÷ 2 =  5  r 1
 5 ÷ 2 =  2  r 1
 2 ÷ 2 =  1  r 0
 1 ÷ 2 =  0  r 1
reading upward: 101110

Method 2, subtract powers of two. Take the largest power of two that fits, subtract it, and repeat. 46 − 32 = 14, 14 − 8 = 6, 6 − 4 = 2, 2 − 2 = 0. The powers used are 32, 8, 4, and 2, so bits 5, 3, 2, and 1 are set: 101110.

The second method is faster in your head once you know the powers of two.

Binary to Hex and Back

This is the easiest conversion, because one hex digit is exactly four bits. Group the binary number into fours from the right, pad the leftmost group with zeros, and replace each group:

101110  →  0010 1110  →  2 E  →  0x2E

Going the other way, expand each hex digit to four bits: 0x4A → 0100 1010.

HexBinaryHexBinary
0000081000
1000191001
20010A1010
30011B1011
40100C1100
50101D1101
60110E1110
70111F1111

Octal works the same way with groups of three: 101 110 → 5 6 → 0o56. That is why Unix permissions, which come in groups of three bits, use octal. See Unix File Permissions in Binary.

Hex to Decimal and Decimal to Hex

Hex to decimal: multiply each digit by its power of 16. 0x2E = 2×16 + 14 = 46. For a longer example, 0x1F4 = 1×256 + 15×16 + 4 = 500.

Decimal to hex: divide repeatedly by 16 and read the remainders upward, or convert to binary first and then group by four. 500 ÷ 16 = 31 r 4, 31 ÷ 16 = 1 r 15 (F), 1 ÷ 16 = 0 r 1, giving 0x1F4.

Quick Reference: Values Worth Memorising

DecimalBinaryHexWhy it matters
1511110xFone nibble, all ones
1270111 11110x7Flargest signed byte
1281000 00000x80sign bit of a byte
2551111 11110xFFlargest unsigned byte
1023ten ones0x3FF10-bit maximum (common ADC range)
65535sixteen ones0xFFFFlargest unsigned 16-bit
21474836470 + 31 ones0x7FFFFFFFlargest signed 32-bit

Negative Numbers

All the methods above are for non-negative values. Negative integers are stored in two’s complement, so −46 in 8 bits is 11010010 (0xD2), not −101110. Converting those is covered in Two’s Complement Explained.

Converting in Code

bin(46), oct(46), hex(46)       # ('0b101110', '0o56', '0x2e')
format(46, "08b")               # '00101110'
int("101110", 2), int("2E", 16), int("56", 8)   # (46, 46, 46)
(46).toString(2);               // "101110"
(46).toString(16).toUpperCase(); // "2E"
parseInt("101110", 2);          // 46
Number("0x2E");                 // 46
#include <stdio.h>
#include <stdlib.h>
printf("%X %o\n", 46, 46);      /* 2E 56 */
long n = strtol("101110", NULL, 2);   /* 46 */

Practice

The fastest way to build fluency is to predict an answer and then check it. Type a value into the Bitwise Calculator in one base and read the result in the other two, or use the Bitwise Hex Calculator to see each hex digit expanded into its four bits.