insertBits
On this page

insertBits

Function

Replaces a contiguous bit field with low bits from another 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: bitfieldInsert

Targets

Compare examples

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

Model assumptionse: u32; offset = 4; count = 4; newbits = 3

Example 1insertBits(0, 3, 4, 4)
Result
48 = 0x00000030
Word in binary: 0
Example 2insertBits(16, 3, 4, 4)
Result
48 = 0x00000030
Word in binary: 16
Example 3insertBits(181, 3, 4, 4)
Result
53 = 0x00000035
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 updating a field without disturbing neighbouring flags.

Inputs and limits

Only the selected field changes. Bits above the inserted field are discarded; offset and count are limited by the word width.

For shader input rules and precision, consult WGSL and GLSL ES 3.00.

WGSL
insertBits(a, b, c, d)
GLSL ES 3.00
_ibits(a, b, c, d)

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

Inserts the low count bits of newbits into e at bit offset; the offset and count are clamped to the 32 bits as WGSL specifies.

Syntax

declare function insertBits<V extends vec2i | vec3i | vec4i | vec2u | vec3u | vec4u>(a0: V, a1: V, a2: u32, a3: u32): V
declare function insertBits<P0 extends number, P1 extends number>(a0: P0, a1: P1, a2: u32, a3: u32): ScalarOf<P0, P1>
declare function insertBits<T extends Numeric>(e: T, newbits: 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 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 VP0T
  • newbits VP1T
  • offset u32number
  • count u32number

Return value

VScalarOf<P0, P1>T

Description

A _ibits helper on GLSL ES 3.00.

Targets

WGSL
insertBits(a, b, c, d)
GLSL ES 3.00
_ibits(a, b, c, d)

Examples

See also

Edit this page Report a problem