---
title: "extractBits / bitfieldExtract: WGSL and GLSL function guide"
description: "Extracts a contiguous field from an integer word. Use it for reading compact flags or binary fields. The examples use unsigned input."
url: https://typeshade.dev/reference/functions/extractBits/
language: en
---

# `extractBits`

Function

Extracts a contiguous field from an integer word.

**Related names in other languages**

These related operations can differ in arguments, support, and semantics. The foreign names are not accepted TypeShade aliases. Check the target forms and restrictions.

- GLSL: `bitfieldExtract`

[Targets](https://typeshade.dev/reference/functions/extractBits/#targets)

## 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`extractBits(0, 4, 4)`**

**Result**

`0 = 0x00000000`

Word in binary: `0`

**Example 2`extractBits(16, 4, 4)`**

**Result**

`1 = 0x00000001`

Word in binary: `16`

**Example 3`extractBits(181, 4, 4)`**

**Result**

`11 = 0x0000000b`

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 reading compact flags or binary fields.

## Inputs and limits

The examples use unsigned input. Signed extraction sign-extends; offset and count are limited by the word width.

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/)
- [`countLeadingZeros`](https://typeshade.dev/reference/functions/countLeadingZeros/)
- [`countTrailingZeros`](https://typeshade.dev/reference/functions/countTrailingZeros/)

**WGSL**

`extractBits(a, b, c)`

**GLSL ES 3.00**

`_xbits(a, b, c)`

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

Extracts `count` bits of `e` starting at bit `offset`, sign-extended for an `i32`; the offset and count are clamped to the 32 bits as WGSL specifies.

## Syntax

```ts
declare function extractBits<V extends vec2i | vec3i | vec4i | vec2u | vec3u | vec4u>(a0: V, a1: u32, a2: u32): V
declare function extractBits<P0 extends number>(a0: P0, a1: u32, a2: u32): ScalarOf<P0>
declare function extractBits<T extends Numeric>(e: T, offset: number, count: number): 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

- `e` `V``P0``T`
- `offset` `u32``number`
- `count` `u32``number`

## Return value

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

## Description

A `_xbits` helper on GLSL ES 3.00.

## Targets

**WGSL**

`extractBits(a, b, c)`

**GLSL ES 3.00**

`_xbits(a, b, c)`

## Examples

- [`gallery/bit-bump/bit-bump.shade.ts`](https://typeshade.dev/guide/examples/bit-bump/)
- [`gallery/packing-bitcast/packing-bitcast.shade.ts`](https://typeshade.dev/guide/examples/packing-bitcast/)

## See also

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