Understand binary, decimal, octal and hexadecimal bases used in digital electronics.
The base changes how you write a number, rather than its value. Binary describes circuit bits; hexadecimal and octal group those bits to make them easier to read.
Base
Digits
Common use
2, binary
0, 1
Real value seen by hardware
10, decimal
0 to 9
Human reading, everyday calculations
8, octal
0 to 7
Groups of 3 bits
16, hexadecimal
0 to 9, A to F
Groups of 4 bits, registers, buses
Convert an integer
Each position has a weight. Binary weights are 1, 2, 4, 8, 16 and so on. Add the weights of the bits set to 1 to read 101101₂:
Bit
1
0
1
1
0
1
Weight
32
16
8
4
2
1
101101₂ = 32 + 8 + 4 + 1 = 45₁₀
The same method reads a bus value, address or counter. To go the other way with 45₁₀, repeatedly divide by two and read the remainders from bottom to top:
Division
Quotient
Remainder
45 / 2
22
1
22 / 2
11
0
11 / 2
5
1
5 / 2
2
1
2 / 2
1
0
1 / 2
0
1
45₁₀ = 101101₂
The rightmost bit, LSB (least significant bit), has the lowest weight. The leftmost bit, MSB (most significant bit), has the highest weight.
Group the bits
One hexadecimal digit represents four bits. Grouping them makes wide buses easier to read:
Hex
Binary
Decimal
0
0000
0
1
0001
1
2
0010
2
3
0011
3
4
0100
4
5
0101
5
6
0110
6
7
0111
7
8
1000
8
9
1001
9
A
1010
10
B
1011
11
C
1100
12
D
1101
13
E
1110
14
F
1111
15
0x2D = 0010 1101₂ = 45₁₀
A 32-bit value can therefore be written with eight hexadecimal digits:
unsigned represents a non-negative integer, including zero. signed represents a two's-complement integer. std_logic_vector describes bits whose meaning comes from context.
The same bits can represent different values under different types. Check base, width and interpretation before comparisons, additions or width extensions.