Lesson 15 of 15

4-Bit Binary Counter

4-Bit Binary Counter

A binary counter increments its stored value by 1 on each clock edge. A 4-bit counter counts from 0 to 15 (0000 to 1111 in binary), then wraps back to 0.

This is the fundamental building block for:

  • Timing circuits (counting clock pulses)
  • Address generators (for memory access)
  • Frequency dividers (output bit N toggles every 2^N clocks)
  • PWM generators

The 4-bit counter state transitions:

0 → 1 → 2 → 3 → 4 → 5 → 6 → 7 → 8 → 9 → 10 → 11 → 12 → 13 → 14 → 15 → 0 → ...

To convert the state to a 4-bit binary array (MSB first):

function toBits(n) {
  return [(n>>3)&1, (n>>2)&1, (n>>1)&1, n&1];
}

Your Task

Implement:

  • nextCount(state): returns (state + 1) % 16
  • countSequence(n): returns array of n+1 states starting at 0

Print the first 8 states as 4-bit binary strings.

JavaScript loading...
Loading...
Click "Run" to execute your code.