← Circuit Toolkit

Digital Number Systems Lab

Convert instantly — or learn every step by hand. Toggle bits, see weighted sums, trace repeated division, and explore BCD and Gray code with step-by-step explanations.

Examples:
human-readable value
used by digital logic and memory
compact shorthand for binary bytes
used in UNIX file permissions
used in seven-segment displays
1
7
3
Grayused in rotary encoders
11111011
Click any cell to toggle that bit
1286432168421
128 + 32 + 8 + 4 + 1 = 173

Number bases in electronics

Digital systems use binary (base-2) because transistors have two stable states: on and off. Hexadecimal (base-16) is the standard shorthand — each hex digit maps exactly to 4 binary bits, making register values and memory addresses compact and readable.

Quick conversion reference

  • Binary → Hex: group binary digits into nibbles (4 bits) from the right. Each nibble = one hex digit.
  • Hex → Binary: expand each hex digit to its 4-bit binary equivalent.
  • Decimal → Binary: repeatedly divide by 2, collect remainders from bottom up.

Two's complement (signed integers)

Microcontrollers represent negative integers in two's complement: invert all bits then add 1. For an 8-bit register, −1 = 0xFF, −128 = 0x80. The MSB (bit 7) is the sign bit — 1 means negative.

Learn more → Number Systems — Learn

Quick experiments

  • Watch a nibble become one hex digit. Type 255 in decimal. Binary shows 11111111 and hex shows FF — two hex digits for eight bits. Now try 4095: hex is FFF, exactly three digits for twelve bits. That 4-bits-per-digit alignment is the whole reason hex is used.
  • Find where an 8-bit register overflows. Enter 255, then 256. An 8-bit value tops out at 255 (2⁸ − 1), so 256 needs a ninth bit. This is the exact point an 8-bit counter wraps back to 0.
  • Make the sign bit flip. Switch on signed two's complement at 8 bits and enter 127, then 128. At 128 the MSB turns on and the value reads −128 — the moment a signed byte wraps from its largest positive to its most negative value.
  • Compare BCD against plain binary. Enter 99. Plain binary is 1100011 (7 bits), while BCD is 1001 1001 (8 bits). BCD costs an extra bit here but each nibble drives one seven-segment digit directly.
  • Count in Gray code and watch one bit move. Step through 0, 1, 2, 3 with Gray code shown: 000, 001, 011, 010. Exactly one bit changes each step, which is what keeps a rotary encoder from misreading mid-transition.

Formula reference

Largest value in n bits (unsigned)
Vmax=2n1V_{\max} = 2^{n} - 1

8 bits → 255, 10 bits → 1023, 16 bits → 65 535.

Signed range in n bits (two's complement)
2n1    V    2n11-2^{\,n-1} \;\le\; V \;\le\; 2^{\,n-1} - 1

8 bits → −128 to +127. The range is asymmetric because zero occupies one of the positive slots.

Two's complement negation
V=V+1-V = \overline{V} + 1

Invert every bit, then add one. For 8-bit 1: 00000001 → 11111110 → 11111111 = −1.

Binary to Gray code
G=B(B1)G = B \oplus (B \gg 1)

XOR the value with itself shifted right one place.

SymbolMeaningUnit
nnWord width in bitsbits
VmaxV_{\max}Largest representable unsigned value
\oplusBitwise XOR
\ggLogical right shift

Common mistakes

  • Reading division remainders top-to-bottom.

    The first remainder is the least significant bit, so the answer reads bottom-to-top. Converting 13 gives remainders 1, 0, 1, 1 in that order, which is 1101 — not 1011.

  • Grouping binary into nibbles from the left.

    Always group from the right and pad the leftmost group with zeros. 110101 is 0011 0101 = 0x35. Grouping from the left gives 1101 01 and the wrong digit.

  • Treating the MSB as a value bit in signed mode.

    In two's complement the MSB carries a negative weight. For 8 bits, 10000000 is not 128 — it is −128, because that bit is worth −2⁷.

  • Assuming 0x10 equals 10.

    0x10 is 16 decimal, and 0b10 is 2. Always state the base: the same digits mean different values in each one.

  • Expecting BCD to be a compact encoding.

    BCD spends four bits per decimal digit regardless of value, so 9999 needs 16 bits where plain binary needs 14. It is chosen for display and decimal arithmetic, never for density.

Frequently asked questions

How do I convert decimal to binary by hand?

Divide the number by 2 and write down the remainder, then keep dividing the quotient by 2 until it reaches 0. Read the remainders bottom-to-top. For 13: 13/2 = 6 r1, 6/2 = 3 r0, 3/2 = 1 r1, 1/2 = 0 r1, so 13 decimal is 1101 binary.

Why do programmers use hexadecimal instead of binary?

One hex digit represents exactly four bits, so a byte is always two hex digits. That makes hex a compact shorthand for binary that stays aligned to byte boundaries, which is why memory addresses, colour codes and register values are written in hex.

What is two's complement and why is it used for negative numbers?

Two's complement stores a negative number as the bitwise inversion of its magnitude plus one. It is used because addition and subtraction then work with the same adder circuit, and there is only one representation of zero, unlike sign-magnitude which has both +0 and -0.

What is the difference between BCD and plain binary?

Plain binary encodes the whole number, so 25 decimal becomes 11001. BCD encodes each decimal digit separately in four bits, so 25 becomes 0010 0101. BCD wastes bits but makes driving seven-segment displays and decimal arithmetic much simpler.

Why is Gray code useful for rotary encoders?

Only one bit changes between consecutive Gray code values. On a rotary encoder that means a misread during a transition is off by at most one step, whereas plain binary can change several bits at once and produce a wildly wrong reading mid-transition.

Related tools

Browse the full circuit toolkit or start a guided lesson in topics.

Share