Trailing Zeros Counter (CTZ)

Count trailing zero bits in an integer (CTZ operation)

Count the trailing zero bits in an integer — the CTZ operation built into modern CPUs. Enter a value in decimal, hex, or binary and see the trailing zero count, the largest power of two dividing it, and the lowest set-bit position. Runs in your browser. It runs free in your browser on Gera Tools, with nothing uploaded.

Last updated Source: Gera Tools

What does CTZ count?

CTZ, or Count Trailing Zeros, returns the number of consecutive zero bits starting from the least significant bit up to the first 1 bit. The value 48 is 110000 in binary, so it has four trailing zeros.

Count Trailing Zeros (CTZ) tells you how many zero bits sit below the lowest set bit of a number. Unlike leading zeros it is independent of register width, and it equals the exponent of the largest power of two dividing the value. This tool computes it for any non-negative integer in decimal, hex, or binary.

How it works

The tool isolates the lowest set bit by shifting the value right while the bottom bit is zero, counting the shifts:

count = 0
while (value & 1) == 0:
    value >>= 1
    count += 1

That count is the position of the lowest set bit (0-indexed from the right) and the exponent k such that 2^k divides the original number but 2^(k+1) does not. Trailing zeros of zero are undefined because no set bit exists to halt the loop, so the tool flags that case rather than looping forever.

The CTZ and lowest-set-bit relationship

CTZ and the lowest set bit are two ways of saying the same thing:

  • CTZ gives the position of the lowest set bit (0-indexed from the right)
  • The lowest set bit value is 2^CTZ
  • You can isolate the lowest set bit in constant time with x & (-x) — this is the bit manipulation idiom used in Fenwick trees and flag processing

For example: 48 in binary is 110000. CTZ = 4. The lowest set bit is at position 4, and 2^4 = 16. The expression 48 & (-48) = 16 confirms it.

Where CTZ is used in practice

Alignment checking: Memory allocators need to confirm that a pointer is aligned to a power of two boundary. An address that is 16-byte aligned has at least 4 trailing zero bits. CTZ gives the alignment in one operation.

Binary GCD (Stein’s algorithm): The binary GCD algorithm removes common factors of two by counting trailing zeros on both inputs, right-shifting them away, and applying odd GCD logic. It avoids division entirely and is the basis of optimized GCD implementations on modern CPUs.

Bitmask schedulers: When a set of ready tasks is represented as a bitmask (bit N = task N is ready), finding the lowest-numbered ready task is exactly CTZ of the bitmask. This is the inner loop of many O(1) scheduler implementations.

Factoring powers of two: CTZ immediately answers “what is the largest power of two dividing this number?” — useful in number theory and when decomposing multiplications/divisions that can be replaced with cheaper bit shifts.

Hardware instructions

CTZ is so commonly needed that modern CPUs expose it as a single instruction:

  • x86/x64: TZCNT (requires BMI1) or BSF (Bit Scan Forward, slightly different semantics for zero input)
  • ARM/AArch64: RBIT followed by CLZ (reverse bits, then count leading zeros)
  • C/C++: __builtin_ctz(x) in GCC/Clang, _BitScanForward on MSVC

This tool uses JavaScript BigInt so it can handle integers of arbitrary size — not just 32 or 64 bits.

Example and notes

The decimal value 48 is 110000 in binary, with four trailing zeros, so CTZ is 4 and 2^4 = 16 is the largest power of two dividing it. CTZ drives the binary GCD (Stein’s) algorithm, lets allocators verify pointer alignment, and finds the next ready task in a bitmask scheduler. On x86 it is the TZCNT instruction; on ARM it is computed by bit-reversing and applying CLZ. This calculator uses BigInt so the input can be arbitrarily large.