Positional Numbers and Base Conversion
Read and write any value in binary, octal, hex and decimal, convert between them by hand, and size a literal correctly.
You Will Be Able To
- Read a binary, octal or hex pattern as a sum of place values.
- Convert a whole number by repeated division and a fraction by repeated multiplication.
- Group bits by three and by four to write octal and hex.
- Write a sized literal and predict what an unsized one becomes.
- Lesson
- G1.1
- Time
- 25 min
- Runs on
- Your browser
- Check
- 6 questions
A register dump shows 10 for a motor delay. The engineer reads ten cycles. The dump is in hex, so the delay is sixteen cycles and the motor starts late. The digits were read correctly, in the wrong base.
Weights show bit magnitudes. For signed values, the leftmost bit has a negative weight.
- Unsigned
- 16
- Hexadecimal
- 0x10
- The dump. Only the bit with the weight 16 is set. The hex view shows 10 and the unsigned view shows 16.
- What the engineer wanted. Ten is 8 + 2. In hex that byte is written 0a.
Click a bit to flip it. One pattern has one value, and each base writes that value in its own digits.
Place Value in Any Radix
A number system has a radix, or base: the count of different digits. Decimal has ten digits, binary has two, octal has eight and hex has sixteen.
Each position has a weight. The rightmost weight is 1, and each step to the left multiplies the weight by the radix. The value is the sum of digit times weight.
| Written as | Radix | Digit times weight | Value |
|---|---|---|---|
165 | 10 | 1 x 100 + 6 x 10 + 5 x 1 | 165 |
10100101 | 2 | 128 + 32 + 4 + 1 | 165 |
245 | 8 | 2 x 64 + 4 x 8 + 5 x 1 | 165 |
a5 | 16 | 10 x 16 + 5 x 1 | 165 |
Four ways to write one value. The Try it below prints all four from one variable.
A binary digit is a bit. Four bits are a nibble and eight bits are a byte. The leftmost bit is the most significant bit (MSB). The rightmost bit is the least significant bit (LSB), and its position is 0.
| Bits | Different values | Largest value |
|---|---|---|
| 1 | 2 | 1 |
| 4 | 16 | 15 |
| 8 | 256 | 255 |
| 10 | 1,024 | 1,023 |
| 16 | 65,536 | 65,535 |
| 32 | 4,294,967,296 | 4,294,967,295 |
N bits hold 2^N values, from 0 to 2^N - 1.
Whole Numbers by Repeated Division
To write a decimal number in binary, divide it by 2 again and again. Each remainder is one bit. The first remainder is the LSB.
| Divide | Quotient | Remainder | Bit position |
|---|---|---|---|
| 42 / 2 | 21 | 0 | 0 (LSB) |
| 21 / 2 | 10 | 1 | 1 |
| 10 / 2 | 5 | 0 | 2 |
| 5 / 2 | 2 | 1 | 3 |
| 2 / 2 | 1 | 0 | 4 |
| 1 / 2 | 0 | 1 | 5 (MSB) |
Stop when the quotient is 0. Read the remainders from the bottom up: 101010.
The method works for any radix. Divide 165 by 16: the quotient is 10 and the remainder is 5. Divide 10 by 16: the quotient is 0 and the remainder is 10, the digit a. So 165 is a5 in hex.
Check the result by going back: 32 + 8 + 2 is 42.
Fractions by Repeated Multiplication
Bits to the right of the binary point have the weights 1/2, 1/4, 1/8 and so on. To convert a decimal fraction, multiply it by 2 again and again. The whole part of each product is one bit, and the first bit is next to the point.
| Fraction | Times 2 | Bit | Fraction left |
|---|---|---|---|
| 5/8 | 10/8 | 1 | 2/8 |
| 2/8 | 4/8 | 0 | 4/8 |
| 4/8 | 8/8 | 1 | 0/8 |
The fraction left reaches 0 after three steps. Read the bits from the top down: 0.101.
| Fraction | Times 2 | Bit | Fraction left |
|---|---|---|---|
| 1/10 | 2/10 | 0 | 2/10 |
| 2/10 | 4/10 | 0 | 4/10 |
| 4/10 | 8/10 | 0 | 8/10 |
| 8/10 | 16/10 | 1 | 6/10 |
| 6/10 | 12/10 | 1 | 2/10 |
| 2/10 | 4/10 | 0 | 4/10 |
| 4/10 | 8/10 | 0 | 8/10 |
| 8/10 | 16/10 | 1 | 6/10 |
The fraction left comes back to 2/10, so the bits 0011 repeat for ever: 0.00011001...
So 0.1 has no exact binary form. Eight fraction bits keep 0.00011001, which is 25/256, or 0.09765625. A fraction is exact in binary only when its denominator is a power of 2.
Octal and Hex as Groups of Bits
Long binary patterns are hard to read. Sixteen is 2^4, so one hex digit stands for exactly four bits. Eight is 2^3, so one octal digit stands for three bits.
- Neutral
- Data
- Groups of four
Start at the LSB and cut the pattern into nibbles: 1010 and 0101.
- Hex digits
The nibble 1010 is ten, the digit
a. The nibble 0101 is 5. The byte isa5. - Groups of three
Start at the LSB again and cut by three: 101, 100 and the short group 10. Read the short group as 010.
- Octal digits
The groups are 2, 4 and 5, so the byte is
245in octal.
Read the diagram as text
- 10100101: the byte, in binary
- 1010 0101: groups of four
- 10 100 101: groups of three
- a5: hex: a = 1010, 5 = 0101
- 245: octal: 2, 4, 5
- 10100101 → 1010 0101: split from the right
- 1010 0101 → a5: one digit each
- 10100101 → 10 100 101: split from the right
- 10 100 101 → 245: one digit each
Always group from the right. A short group on the left gets leading zeros.
| Hex | Bits | Hex | Bits | Hex | Bits | Hex | Bits |
|---|---|---|---|---|---|---|---|
0 | 0000 | 4 | 0100 | 8 | 1000 | c | 1100 |
1 | 0001 | 5 | 0101 | 9 | 1001 | d | 1101 |
2 | 0010 | 6 | 0110 | a | 1010 | e | 1110 |
3 | 0011 | 7 | 0111 | b | 1011 | f | 1111 |
The sixteen hex digits. The digits a to f stand for ten to fifteen.
To go back, write four bits for each hex digit. 3c is 0011 and 1100, the byte 00111100.
Sized Literals and the 32-Bit Trap
In SystemVerilog a number is written as a literal. A sized literal has three parts: the width in bits, a base letter and the digits. 8'hA5 is eight bits wide, in hex. An underscore between digits is ignored.
| Literal | Width | Bits it holds | What happened |
|---|---|---|---|
8'hA5 | 8 | 10100101 | the digits fit exactly |
4'b1010 | 4 | 1010 | the digits fit exactly |
12'h3C | 12 | 000000111100 | padded with zeros on the left |
4'd42 | 4 | 1010 | cut: only the lowest four bits stay |
42 | 32 | 26 zeros, then 101010 | no size given, so 32 bits |
Measured on the simulator with $bits and %b.
A literal that is too wide for its size loses its top bits without an error. 4'd42 keeps 1010, which is ten.
A literal with no size is at least 32 bits wide, and exactly 32 on every common simulator. That is harmless in v = 42, where the top bits are cut. It is an error inside a concatenation. The simulator refuses {4'b1010, 5} with the message has indefinite width.
Asked in interviews
Convert
0xA5to decimal in your head.Take each hex digit times its weight:
ais 10, so 10 x 16 is 160. Add the last digit: 160 + 5 is 165.How many bits does a counter need to count to 999?
Ten. Nine bits end at 511 and ten bits end at 1,023. In general, find the smallest N with 2^N above the largest value.
Why can 0.1 not be stored exactly in binary?
A binary fraction is a sum of halves, quarters, eighths and so on. Its denominator is a power of 2. One tenth has a factor of 5 in its denominator, so its bits repeat for ever.
Try It
The variable holds one pattern of eight bits. Each format letter writes that pattern in a different base: %b, %o, %h and %d.
Change the literal to 8'h10 and run. The decimal line shows 16, the value behind the register dump of the hook.
Now try 8'd300. The value needs nine bits, so the top bit is cut and the program prints 44. Then try plain 42: the 32-bit literal is cut to eight bits and prints 00101010.
binary 10100101 octal 245 hex a5 decimal 165Check Yourself
Common Mistakes
Wrong Read the hex digits 10 as ten.
The second digit from the right has the weight 16 in hex, so 10 is sixteen.
Fix Name the base first. Then add digit times weight for each position.
delay = 8'h10; // "ten cycles"delay = 8'd10; // ten cycles
delay = 8'h0a; // also tenWrong Count bit positions from 1.
The rightmost bit has the weight 2^0, so its position is 0 and a byte ends at bit 7.
Fix Number the bits from 0 on the right. Bit n has the weight 2^n.
Wrong Write a literal with no size, such as 42, and expect it to be as wide as its digits.
An unsized literal is 32 bits wide on every common simulator, and a concatenation refuses it.
Fix Give every literal in a design its width: 8'd42.
y = {4'b1010, 5}; // errory = {4'b1010, 4'd5};Wrong Drop the leading zeros of a pattern, as in 101 for a byte.
The zeros carry no value, but they show the width, and hardware has a fixed width.
Fix Write every bit of the width: 0000_0101 for the byte that holds five.
Remember
- A value is the sum of digit times weight, and each weight is a power of the radix.
- N bits hold 2^N values, from 0 to 2^N - 1; bit positions start at 0 on the right.
- Repeated division gives the bits of a whole number from the LSB up; repeated multiplication gives fraction bits from the point down.
- One hex digit is four bits and one octal digit is three bits, grouped from the right.
- Give every literal a size: an unsized literal is 32 bits, and a literal that is too wide loses its top bits.