Skip to content
code0.HDL

Bits and numbers

Bits as the binary model of digital values, buses that group bits into signals, binary place value, hexadecimal notation, and how width determines the unsigned range.

In this chapter

  • Read and write a value of a given width in binary and hexadecimal
  • Compute a bit pattern's value from the place value of each bit
  • State the range a width can hold and the width a range needs
Concepts in this chapter
  • Bit
  • Bus and width
  • Binary place value
  • Hexadecimal
  • Width and representable range
Prerequisite concepts
No earlier concepts are needed.

1. Bits represent digital values

In the binary model used here, a digital circuit carries, stores and computes bits. Each bit is 0 or 1. A physical signal represents those values with low and high voltage ranges, not exactly two voltages. A number such as 2, a letter or a fraction needs an encoding, usually across several bits.

The experiments in this chapter use only 0 and 1. Verilog simulation also uses x for an unknown logic value and z for high impedance. These are simulation states, not two additional binary digits.

A group of wires named and handled as one signal is a bus, and the number of wires in it is its width. Width is fixed when the signal is declared and does not change while the circuit runs: an 8-bit bus is eight wires whether the value on it is 0 or 255.

In this chapter's bit patterns, positions are numbered from the right: bit 0 is the least significant bit and bit n−1 the most significant bit of an n-bit pattern. HDL declarations can use other index ranges; the diagrams here use this convention consistently.

2. Each bit position is worth a power of two

A bus holds a number the way a decimal numeral does, except that each position is worth a power of two: bit 0 is worth 1, bit 1 is worth 2, bit 2 is worth 4, and bit i is worth 2^i. The value of the bus is the sum of the place values under the bits that are 1.

Take 1011. Bit 3 is 1 and worth 8, bit 2 is 0, bit 1 is 1 and worth 2, bit 0 is 1 and worth 1. 8 + 2 + 1 = 11, so 1011 is eleven.

Going the other way, subtract the largest place value that still fits. For 13 in four bits: 8 fits and leaves 5; 4 fits and leaves 1; 2 does not fit; 1 fits and leaves 0. The bits taken are 8, 4 and 1, so 13 is 1101.

In the experiment, press a bit to flip it. The place values stand above the bits, and the unsigned readout is the sum under the 1s. Set 1011 and confirm the sum, then find the pattern that reads 13.

Concept experiment · 4-bit

A browser model of the circuit, not a compiled design. The real toolchain runs in the examples.

Bit3210Place value8421Inputs
Unsigned
0
Hex
0x0

Check

What unsigned value does the 4-bit pattern 0110 hold?

Check

Write 13 as a 4-bit pattern.

3. One hexadecimal digit names four bits

Long patterns are hard to read and easy to miscopy. Hexadecimal groups the bits in fours from the right and writes each group as one digit from 0 to F. Four bits take 16 values and hexadecimal has 16 digits, so the match is exact and needs no arithmetic. Hex is a notation for bits, not a different kind of number inside the circuit.

BitsHexBitsHexBitsHexBitsHex
0000001004100081100C
0001101015100191101D
00102011061010A1110E
00113011171011B1111F

The 8-bit pattern 10110110 splits into 1011 and 0110, which are B and 6: 0xB6. The prefix 0x marks the digits as hexadecimal; without it B6 could be a name. Reading back uses the same table the other way: 0x3A is 0011 1010.

Check

Write the 4-bit pattern 1110 as one hexadecimal digit.

4. Width fixes the range

An n-bit bus has 2^n patterns and no more. Read unsigned, all zeros is 0 and all ones is 2^n − 1. A value outside that range has no pattern on that bus and cannot be stored on it.

WidthPatternsUnsigned range
120 to 1
4160 to 15
82560 to 255
1665 5360 to 65 535
n2^n0 to 2^n − 1

Choosing a width starts from the range the signal has to hold. A counter that counts from 0 to 200 needs 201 patterns. Seven bits give 128, not enough; eight give 256, enough. The width is 8, and the patterns for 201 to 255 go unused, which is normal.

Caution. The width is chosen for the largest value, not the typical one. A 7-bit bus chosen because the count is usually under 100 has no pattern for 200 and shows something else when the count gets there. What it shows is a later unit's subject.

Check

A signal must hold every value from 0 to 200. What is the smallest width that works?

Check

What is the largest unsigned value a 10-bit bus can hold?

Your answer

5. Variation: eight bits, read three ways

The experiment below is 8 bits wide and starts at 190. As place values, 128 + 32 + 16 + 8 + 4 + 2 = 190, so the pattern is 10111110. As hex, 1011 is B and 1110 is E: 0xBE. One pattern, three ways of writing it down.

Concept experiment · 8-bit

A browser model of the circuit, not a compiled design. The real toolchain runs in the examples.

Bit76543210Place value1286432168421Inputs
Unsigned
190
Hex
0xBE

Now flip bit 6 and bit 0 on and bits 5, 4, 3 and 2 off, and watch the three readouts move together. The pattern you reach is 0xC3.

Check

What unsigned value is 0xC3?

6. Summary

  • A bit is 0 or 1. A bus is several bits named as one signal, numbered from 0 at the right.
  • Bit i is worth 2^i. The value of a bus is the sum of the place values under its 1s.
  • One hex digit names exactly four bits; 0x marks the notation.
  • n bits hold 2^n patterns, 0 to 2^n − 1 unsigned. The width is chosen for the largest value.

The next chapter takes single bits and asks what a gate does with them: NOT, AND, OR and XOR, and the truth table that describes each one completely.

Checkpoint

Answer the checks in this chapter