countOneBits
Function
Counts the set bits in 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:
bitCount - HLSL:
countbits
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 1countOneBits(0)
- Result
0 = 0x00000000
0Example 2countOneBits(16)
- Result
1 = 0x00000001
16Example 3countOneBits(181)
- Result
5 = 0x00000005
181The 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 population counts and compact flag summaries.
Inputs and limits
Zero has no set bits. The operation counts the full word, including the sign bit for signed input.
For shader input rules and precision, consult WGSL and GLSL ES 3.00.
Related functions
- WGSL
countOneBits(a)- GLSL ES 3.00
_popcnt(a)
The signature, the sentence and any message are the compiler's own text at commit af54ddad5.
The number of 1 bits in each component of a u32 or i32 (or vector of them).
Syntax
declare function countOneBits<V extends vec2i | vec3i | vec4i | vec2u | vec3u | vec4u>(a0: V): Vdeclare function countOneBits<P0 extends number>(a0: P0): ScalarOf<P0>declare function countOneBits<T extends Numeric>(a0: T): TA 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.
ScalarOftype 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>Numerictype 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.
-
a0VP0T
Return value
VScalarOf<P0>T
Description
A _popcnt helper on GLSL ES 3.00, which has no bit builtins.
Targets
- WGSL
-
countOneBits(a) - GLSL ES 3.00
-
_popcnt(a)