Home / Learn Verilog / Binary Arithmetic: Add, Subtract, Overflow

Binary Arithmetic: Add, Subtract, Overflow

Add bit by bit with carry, and see what happens when a result does not fit.

The short versionAdd bit by bit with carry, and see what happens when a result does not fit.

Theory in plain words

4-bit addition1011 (11)+ 0110 (6)carry chain1 0001 (17)
4-bit addition

What it is

Binary addition works like decimal addition with only digits 0 and 1: 1 + 1 = 10 (write 0, carry 1). A result that needs more bits than available overflows and loses the top bit.

Why it matters

Hardware registers have a fixed width, so you must know when a result will not fit.

Mechanically speaking

An N-bit adder gives an N-bit sum plus a carry-out. Subtraction is addition of the two's complement. A wider result variable keeps the carry.

Shows up in

  • ALU design
  • Counters
  • Address calculation

Watch overflow happen

8-bit add: 200 + 100Verilog: edit and run

300 needs 9 bits. In 8 bits the value wraps to 300 - 256 = 44.

Check yourself

1. 200 + 100 in 8 bits gives...

2. To keep the carry you should...