e01.org

ctz: Wasm numeric instruction

The ctz numeric instruction, short for count trailing zeros, is used to count the number of zeros at the end of the numbers binary representation.

The **`ctz`** [numeric instruction](/docs/WebAssembly/Reference/Numeric), short for _count trailing zeros_, is used to count the number of zeros at the end of the numbers binary representation.

tabbed-taller

```wat interactive-example
(module

(func (export "trailing0") (param $num i32) (result i32)
;; load number onto the stack
local.get $num

;; check how many trailing zeros and return the result
i32.ctz
)

)
```

```js interactive-example
const url = "{%wasm-url%}";
await WebAssembly.instantiateStreaming(fetch(url), { console }).then(
(result) => {
const trailing0 = result.instance.exports.trailing0;

console.log(
`Trailing zeros: ${trailing0(0b00000000_10000000_00000000_00000000)}`,
);
// Expected output: "Trailing zeros: 23"
},
);
```

## Syntax

```wat
;; load a number onto the stack
i32.const 8388608 ;; 00000000_10000000_00000000_00000000

;; count trailing zeros
i32.ctz

;; the top item on the stack will now be 23
```

| Instruction | Binary opcode |
| ----------- | ------------- |
| `i32.ctz` | `0x68` |
| `i64.ctz` | `0x7a` |

## Specifications

## Browser compatibility

source: MDN Web Docs mirror · page

Cite this

"ctz: Wasm numeric instruction". MDN Web Docs. CC BY-SA 2.5 (MDN). https://developer.mozilla.org/en-US/docs/WebAssembly/Reference/Numeric/ctz

JSON record · API collection · citation policy