Number Systems: Binary, Hex and Two’s Complement

Every digital system stores and moves information as bits — two-state signals that are either 0 or 1. To design or read hardware you have to be fluent in how numbers are represented in binary, hexadecimal, and signed form. This chapter is that foundation.

Positional number systems

In any base b, a digit at position i (counting from 0 at the right) carries weight bi. Decimal uses base 10; digital logic uses binary (base 2) because a wire naturally holds two levels. Hexadecimal (base 16) is shorthand: one hex digit packs exactly four bits, so 8'hA5 is far easier to read than 8'b10100101.

DecimalBinaryHex
101010A
1651010 0101A5
2551111 1111FF

Signed numbers: two’s complement

Hardware represents negative integers in two’s complement: to negate a number, invert every bit and add 1. The top bit becomes a sign bit, and — crucially — the same adder works for signed and unsigned values, which is why every CPU uses it. In an n-bit field the range is −2n-1 to +2n-1−1.

The example below lets the simulator prove the rule: it prints an 8-bit value, its bitwise inverse, and the two’s-complement negation, and checks that x + (−x) = 0.

module tb;
  reg  signed [7:0] x;
  wire signed [7:0] neg = -x;          // two's complement negate
  integer i;
  initial begin
    for (i = 0; i < 3; i = i + 1) begin
      x = (i==0)? 8'd5 : (i==1)? -8'd5 : 8'sd127;
      $display("x=%0d  bin=%b  ~x=%b  -x=%0d  x+(-x)=%0d",
               x, x, ~x, neg, x + neg);
    end
    $display("two's complement verified: x + (-x) == 0 for all x");
    $finish;
  end
endmodule
x=5  bin=00000101  ~x=11111010  -x=-5  x+(-x)=0
x=-5  bin=11111011  ~x=00000100  -x=5  x+(-x)=0
x=127  bin=01111111  ~x=10000000  -x=-127  x+(-x)=0
two's complement verified: x + (-x) == 0 for all x

Check yourself

Why does digital hardware prefer two’s complement over sign-magnitude?

Because one ordinary binary adder then handles both signed and unsigned addition and subtraction — there is no separate hardware for the sign. Sign-magnitude needs special-case logic for the sign bit and has two representations of zero.

How many bits are needed to represent the unsigned value 255, and what is it in hex?

8 bits (2^8 = 256 values, 0–255), which is 0xFF.

Have questions about this topic?
Share your question in comments or talk to our mentor team for batch guidance.
▶ Try it in the ChipXpert Lab

Open this design in the free in-browser simulator — edit, compile and see the waveform/log instantly.

Run it in the Lab

Learn Verilog with mentors, labs and placement support

This free series is maintained by the ChipXpert training team in Hyderabad & Bengaluru. Go deeper with instructor-led courses on real EDA tools.

Explore VLSI Courses
Next batch starts 18th November 2026
Start your VLSI career with ChipXpert

Live online and classroom batches in Hyderabad & Bengaluru. Fill in your details and a counsellor will call you back.

  • Real EDA tools in your browser: industry-standard EDAReal EDA tools in your browser
  • Recorded sessions on the elearn portalRecorded sessions
  • Placement assistance: resume, mock interviewsPlacement assistance
  • Merit scholarship up to 60% · EMI optionsScholarship up to 60%
Hi! Ask me about courses, fees, batches or discounts. ×
BENGALURU
HYDERABAD