Intrinsics1 min read

bitfield_extract

Extracts a contiguous range of bits from an integer.

Reading Time
1 min
Word Count
122
Sections
7
Try It Live

Test bitfield_extract in a live shader

Open the playground, start from a visual preset, and wire bitfield_extract into the fragment stage to see how it behaves with real values.

Open Playground

The bitfield_extract function reads a bit field from an integer value. The offset selects the least-significant bit of the field, and bits selects how many bits to read.

Live Demo

Signature

bwsl
bitfield_extract :: (int value, int offset, int bits) -> int
bitfield_extract :: (uint value, int offset, int bits) -> uint

Parameters

ParameterTypeDescription
valueint or uintSource integer
offsetintIndex of the least-significant bit to extract
bitsintNumber of bits to extract

Return Value

Returns the extracted field. Signed extraction sign-extends the result; unsigned extraction zero-extends it.

Example

bwsl
uint rgba = 0xff804020u;
uint green = bitfield_extract(rgba, 8, 8);

Compiled Output

When compiled to GLSL:

glsl
bitfieldExtract(value, offset, bits)

When compiled to HLSL:

hlsl
// lowered to shifts and masks

When compiled to Metal:

metal
extract_bits(value, offset, bits)

See Also