Wize Electricity and Magnetism Textbook > Electronics

Digital Electronics and Binary Representation

Bits, Bytes and What a Pattern Means

A digital circuit does not store numbers. It stores voltages that a receiving circuit agrees to read as one of two symbols, written 0 and 1. One such symbol is a bit; eight bits handled as a unit are a byte.


Everything else in this topic, how large a value fits, whether it can be negative, how many distinct things can be labelled, follows from counting those symbols.

Positional notation is the same idea in every base

In base 10 the digit string 407 means 4×102 + 0×101 + 7×100. Binary uses the same rule with base 2, and hexadecimal with base 16, where the digits A through F stand for ten through fifteen. Reading the pattern 1011 0101 as unsigned binary:


1×128 + 0×64 + 1×32 + 1×16 + 0×8 + 1×4 + 0×2 + 1×1 = 128 + 32 + 16 + 4 + 1 = 181.


Hexadecimal is not a separate conversion. Because 16 = 24, each hex digit is exactly four bits, so you group the bits in fours and translate each group on sight:


1011 0101 → 1011 = B, 0101 = 5 → 0xB5.


Checking against base 16 directly: 11×16 + 5 = 176 + 5 = 181, the same value. That four-bit grouping is the whole reason hexadecimal is used at all: it is a compact way to write a bit pattern without changing it, and going back is equally mechanical.

How much fits in n bits

n bits form 2n distinct patterns, no more and no fewer. If those patterns are used as unsigned integers starting at zero, the values run from 0 to 2n − 1. The −1 is not a fudge: zero consumes one of the patterns, so the largest value is one below the count.


An eight-bit unsigned byte therefore spans 0 through 255, not 0 through 256.

Representing negative values

Nothing in a bit pattern is inherently negative. A sign convention is a decision the reading circuit makes. Two conventions are worth knowing, and only one is in general use.


Sign–magnitude reserves the leading bit as a sign flag and reads the rest as a magnitude. It is easy to describe and awkward to build: it produces two encodings of zero, and adding the raw patterns is not reliable when signs differ; sign-aware addition is generally needed.


Two's complement instead gives the leading bit a negative weight. In eight bits the leading position is worth −128 rather than +128, so the representable range is −128 through +127, asymmetric, because zero sits on the non-negative side.


To encode a negative value, write its magnitude in binary, invert every bit, and add one. For −75 in eight bits:


  • 75 = 0100 1011 (64 + 8 + 2 + 1 = 75)
  • invert: 1011 0100
  • add one: 1011 0101


That is the same pattern read as 181 above. This is the central point of the topic: the bits 1011 0101 are 181 to a circuit told to read them as unsigned and −75 to a circuit told to read them as eight-bit two's complement.


Both readings are correct; the pattern alone does not choose between them. The shortcut worth remembering is that for an n-bit pattern the signed reading is the unsigned reading minus 2n whenever the leading bit is 1: 181 − 256 = −75.


Two's complement is used because ordinary binary addition then works unchanged for both signs. Subtraction can reuse that adder after two's-complement negation of the subtrahend (invert its bits and add one).


The cost is that a result can leave the signed range while still fitting the register, which is what signed overflow means: the stored bits are a legitimate pattern, just not the intended value.

Encoding limits

When the bits label things rather than count them, the same arithmetic runs backwards: to give N distinct items unique codes you need the smallest n with 2n ≥ N.


For 40 items, 25 = 32 is too few and 26 = 64 suffices, so six bits are required and 64 − 40 = 24 codes go unused.


The unused codes are not wasted effort, they are often reserved for error states, but they are unavoidable whenever N is not itself a power of two.


Read in the other direction, a fixed width sets a hard ceiling: a twelve-bit field offers 212 = 4096 codes no matter what they are used for. Widening a field by one bit doubles the count; the growth is exponential in width, which is why modest word sizes cover enormous ranges.

Habits that prevent the usual errors

  • State the width and the convention before quoting a value. "1011 0101" is not a number until you say how many bits and whether it is signed.
  • Count patterns, then subtract one for the top of an unsigned range. Reaching for 2n directly is the most common slip.
  • Convert hexadecimal by four-bit grouping rather than by repeated division; it is faster and it makes the bit-level structure visible.
  • After any two's-complement encoding, verify by adding the result to the positive magnitude: the sum must be zero in the chosen width.

A pressure sensor transmits each reading as an unsigned 10-bit integer. Assuming every bit pattern is used and the count starts at zero, what is the largest reading the sensor can transmit?

Choose an answer

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

Mark Yourself Question

  1. Grab a piece of paper and try this problem yourself.
  2. When you're done, check the "I have answered this question" box below.
  3. View the solution and report whether you got it right or wrong.

A device register is documented with the value 0x2F7.

  1. Write this value in binary.
  2. Write it in decimal.
  3. If it is stored unsigned, what is the smallest number of whole bytes that can hold it, and how many bits of that storage are actually needed?

Show the grouping you used rather than only the final numbers.

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

An eight-bit register holds the pattern 1110 0110. A datasheet notes that the same register is read as an unsigned integer by one peripheral and as an eight-bit two's-complement signed integer by another.

What value does each peripheral see?

Choose an answer

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors