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.

The Byte Behind the Dump
27
26
25
24
23
22
21
20

Weights show bit magnitudes. For signed values, the leftmost bit has a negative weight.

Unsigned
16
Hexadecimal
0x10

  1. The dump. Only the bit with the weight 16 is set. The hex view shows 10 and the unsigned view shows 16.
  2. 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 asRadixDigit times weightValue
165101 x 100 + 6 x 10 + 5 x 1165
101001012128 + 32 + 4 + 1165
24582 x 64 + 4 x 8 + 5 x 1165
a51610 x 16 + 5 x 1165

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.

BitsDifferent valuesLargest value
121
41615
8256255
101,0241,023
1665,53665,535
324,294,967,2964,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.

42 in binary
DivideQuotientRemainderBit position
42 / 22100 (LSB)
21 / 21011
10 / 2502
5 / 2213
2 / 2104
1 / 2015 (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.

0.625 (5/8) in binary
FractionTimes 2BitFraction left
5/810/812/8
2/84/804/8
4/88/810/8

The fraction left reaches 0 after three steps. Read the bits from the top down: 0.101.

0.1 (1/10) in binary
FractionTimes 2BitFraction left
1/102/1002/10
2/104/1004/10
4/108/1008/10
8/1016/1016/10
6/1012/1012/10
2/104/1004/10
4/108/1008/10
8/1016/1016/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.

One Byte, Grouped Two WaysArrows show the same eight bits being split into groups. Each group becomes one digit.
  • Neutral
  • Data
One byte, grouped two waysArrows show the same eight bits being split into groups. Each group becomes one digit. neutral 10100101 the byte, in binary neutral 1010 0101 groups of four neutral 10 100 101 groups of three neutral a5 hex: a = 1010, 5 =0101 neutral 245 octal: 2, 4, 5 split from the rightone digit eachsplit from the rightone digit each
  1. neutral 10100101the byte, in binary
    • split from the right → 1010 0101
    • split from the right → 10 100 101
  2. neutral 1010 0101groups of four
    • one digit each → a5
  3. neutral a5hex: a = 1010, 5 = 0101
  4. neutral 10 100 101groups of three
    • one digit each → 245
  5. neutral 245octal: 2, 4, 5
  1. Groups of four

    Start at the LSB and cut the pattern into nibbles: 1010 and 0101.

  2. Hex digits

    The nibble 1010 is ten, the digit a. The nibble 0101 is 5. The byte is a5.

  3. 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.

  4. Octal digits

    The groups are 2, 4 and 5, so the byte is 245 in 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.

HexBitsHexBitsHexBitsHexBits
000004010081000c1100
100015010191001d1101
2001060110a1010e1110
3001170111b1011f1111

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.

LiteralWidthBits it holdsWhat happened
8'hA5810100101the digits fit exactly
4'b101041010the digits fit exactly
12'h3C12000000111100padded with zeros on the left
4'd4241010cut: only the lowest four bits stay
423226 zeros, then 101010no 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
  1. Convert 0xA5 to decimal in your head.

    Take each hex digit times its weight: a is 10, so 10 x 16 is 160. Add the last digit: 160 + 5 is 165.

  2. 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.

  3. 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.

RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: binary 10100101 octal 245 hex a5 decimal 165

Check Yourself

Predict the outputPredict the exact line this program prints.
module tb;
  initial begin
    $display("%h", 8'b1010_0101);
  end
endmodule
Predict the outputThe dump of the hook, as a program. What exact line does it print?
module tb;
  initial begin
    $display("%0d", 8'h10);
  end
endmodule
Spot the bugEach option stores a value in the 8-bit variable v. The bench wants decimal 165. Which option fails?
ConceptWhich literal is exactly 12 bits wide?
ConceptWhat is the binary fraction 0.011 in decimal?
ConceptYou convert 42 to binary by repeated division by 2. Which bit does the first remainder give?

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.

Before
delay = 8'h10;   // "ten cycles"
After
delay = 8'd10;   // ten cycles
delay = 8'h0a;   // also ten

Wrong 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.

Before
y = {4'b1010, 5};      // error
After
y = {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.

Practice

Report a problem with this page