---
eip: 7939
label: EIP-7939
title: Count leading zeros (CLZ) opcode
status: Final
type: Standards Track
category: Core
created: 2025-04-28
official: https://eips.ethereum.org/EIPS/eip-7939
source: https://github.com/ethereum/EIPs/blob/master/EIPS/eip-7939.md
canonical: https://eips.huatuo.xyz/eip-7939/
lang: en
tldr: Adds a CLZ opcode (0x1e) that returns the number of leading zero bits in a 256-bit word for 5 gas, replacing a ~184-gas Solidity routine used in math, bitmaps and compression.
tags: [evm, opcode, core, math, fusaka]
---

# EIP-7939: Count leading zeros (CLZ) opcode

> Opcode to count the number of leading zero bits in a 256-bit word

**TL;DR:** Adds a CLZ opcode (0x1e) that returns the number of leading zero bits in a 256-bit word for 5 gas, replacing a ~184-gas Solidity routine used in math, bitmaps and compression.

## What problem does it solve?

Counting leading zeros ("where is the highest set bit?") is a building block for fixed-point logarithms and powers, square and cube roots, bitmaps that search for the next set bit, byte-string comparison and calldata compression. Many processor architectures, including ARM, have a native instruction for it.

The EVM did not. The fastest known Solidity version takes about 184 gas, uses large magic constants that bloat bytecode, and relies on several dynamic right shifts, which are expensive inside ZK provers. EIP-7939 adds the operation directly.

## How it works

- New opcode **`CLZ` = `0x1e`**.
- Pops one value `x`, pushes the number of zero bits before the most significant `1` bit.
- If `x == 0`, it pushes **256**.
- Gas cost: **5**, the same as `MUL`.

Examples from the spec's test cases:

| Input | Output |
| --- | --- |
| `0x00…00` | 256 |
| `0x80…00` (top bit set) | 0 |
| `0x40…00` | 1 |
| `0x7fff…ff` | 1 |
| `0x00…01` | 255 |

Why 256 for zero? It is the next value after 255, so comparisons stay cheap, and `256 >> 3 = 32` gives the number of bytes to skip for an all-zero word. Why CLZ rather than count-trailing-zeros? CTZ can be derived from CLZ by isolating the lowest bit (`x & -x`), but not the other way round.

## Sample code

Reference behaviour in Python, from the spec:

```python
def clz(x: int) -> int:
    assert 0 <= x < 2**256
    if x == 0:
        return 256
    return 256 - x.bit_length()

assert clz(0) == 256
assert clz(1) == 255
assert clz(1 << 255) == 0
```

Raw bytecode that computes `clz(1)`:

```text
6001      PUSH1 0x01
1e        CLZ          ; stack: 0xff (255)
```

A common use: index of the highest set bit (floor of log2) is `255 - clz(x)` for non-zero `x`.

## Things to know

- **Status: Final.** It is listed in the Fusaka hard fork meta EIP ([EIP-7607](/eip-7607/)), so it only works on chains that have activated that fork; other EVM chains may lag.
- Contracts that use `CLZ` will revert with an invalid opcode on chains without it. Check the target chain and your compiler's EVM version setting before relying on it.
- The authors benchmarked it at roughly the cost of `ADD` in compute; the 5 gas price was set to match `MUL` to stay clear of under-pricing.
- It complements the shift opcodes from [EIP-145](/eip-145/).


---

This is an unofficial plain-language summary. Read the full specification: https://eips.ethereum.org/EIPS/eip-7939
