---
title: "countLeadingZeros: WGSL and GLSL function guide"
description: "Counts zeros before the highest set bit. Use it for estimating integer magnitude. Zero returns the word width. Signed inputs are treated as bit patterns."
url: https://typeshade.dev/reference/functions/countLeadingZeros/
language: en
---

# `countLeadingZeros`

Function

Counts zeros before the highest set bit.

## Compare examples

Compare zero, a sparse word, and a word with several set bits. Read the full binary representation.

**Model assumptions**`e: u32; offset = 4; count = 4; newbits = 3`

**Example 1`countLeadingZeros(0)`**

**Result**

`32 = 0x00000020`

Word in binary: `0`

**Example 2`countLeadingZeros(16)`**

**Result**

`27 = 0x0000001b`

Word in binary: `16`

**Example 3`countLeadingZeros(181)`**

**Result**

`24 = 0x00000018`

Word in binary: `181`

The examples are calculation models and do not run GPU work. The reference below gives the complete call signatures. Decimal results are rounded for display. The stated assumptions determine each example; actual shader results follow the target precision and execution rules.

## When to use it

Use it for estimating integer magnitude.

## Inputs and limits

Zero returns the word width. Signed inputs are treated as bit patterns.

For shader input rules and precision, consult [WGSL](https://www.w3.org/TR/WGSL/) and [GLSL ES 3.00](https://registry.khronos.org/OpenGL/specs/es/3.0/GLSL_ES_Specification_3.00.pdf).

## Related functions

- [`countOneBits`](https://typeshade.dev/reference/functions/countOneBits/)
- [`reverseBits`](https://typeshade.dev/reference/functions/reverseBits/)
- [`countTrailingZeros`](https://typeshade.dev/reference/functions/countTrailingZeros/)
- [`firstLeadingBit`](https://typeshade.dev/reference/functions/firstLeadingBit/)

**WGSL**

`countLeadingZeros(a)`

**GLSL ES 3.00**

`_clz(a)`

The signature, the sentence and any message are the compiler's own text at commit af54ddad5.

The number of leading 0 bits in each component of a `u32` or `i32` (32 for zero).

## Syntax

```ts
declare function countLeadingZeros<V extends vec2i | vec3i | vec4i | vec2u | vec3u | vec4u>(a0: V): V
declare function countLeadingZeros<P0 extends number>(a0: P0): ScalarOf<P0>
declare function countLeadingZeros<T extends Numeric>(a0: T): T
```

A `number` in a parameter or a return position is the scalar slot. The ambient file declares a GPU scalar as a branded number whose brand is optional, so a literal and any branded scalar both fill it.

`type f32 = number & { readonly [f32Tag]?: true }`

A `number` you write as a type is inferred from its uses, and `f32`, `i32` and `u32` fix a width; [the TypeScript constructs page](https://typeshade.dev/guide/language/from-typescript/) carries that row with the compiler's emit.

A signature may name a type the ambient file declares for its own use. These are those declarations.

**`ScalarOf`**

`type ScalarOf<A, B = unknown, C = unknown> = unknown extends A ? number : [A] extends [boolean] ? bool : typeof f32Tag extends keyof A ? f32 : typeof i32Tag extends keyof A ? i32 : typeof u32Tag extends keyof A ? u32 : typeof f64Tag extends keyof A ? f64 : ScalarOf<B, C>`

**`Numeric`**

`type Numeric = number | vec2 | vec3 | vec4 | vec2i | vec3i | vec4i | vec2u | vec3u | vec4u`

## Parameters

A parameter named by position (`a0`, `a1`) carries a name the ambient file generates. A call passes its arguments in order and names none of them.

- `a0` `V``P0``T`

## Return value

`V``ScalarOf<P0>``T`

## Description

A `_clz` helper on GLSL ES 3.00.

## Targets

**WGSL**

`countLeadingZeros(a)`

**GLSL ES 3.00**

`_clz(a)`

## See also

- [Functions](https://typeshade.dev/reference/functions/)
- [Builtin table](https://typeshade.dev/guide/language/builtins/#bits)
