Bitwise Operations on i16vec2 in Explicit Integer GLSL
Explicit integer GLSL enables 16-bit integer types and their
corresponding vector forms, such as i16vec2, via extensions
like GL_EXT_shader_explicit_arithmetic_types_int16. This
article covers all bitwise operators supported on signed 16-bit
two-component integer vectors (i16vec2), how they evaluate
across vector components, and the rules governing bitwise shifts and
assignments.
Standard Bitwise Operators
In GLSL shaders with explicit 16-bit arithmetic enabled,
i16vec2 supports the complete standard set of C-style
bitwise operators. These operations execute component-wise across both
components (x and y):
- Bitwise NOT (
~): A unary operator that flips all bits for each component of the vector independently. - Bitwise AND (
&): Computes the bitwise conjunction between two operands component by component. - Bitwise OR (
|): Computes the bitwise inclusive disjunction between two operands component by component. - Bitwise XOR (
^): Computes the bitwise exclusive disjunction between two operands component by component.
Bitwise Shift Operators
Bitwise shift operators are fully supported on
i16vec2:
- Bitwise Left Shift (
<<): Shifts the bits of each 16-bit component to the left by the specified shift count, filling vacated lower bits with zeros. - Bitwise Right Shift (
>>): Performs an arithmetic right shift becausei16vec2represents signed integers. Vacated high bits are filled by replicating the sign bit (most significant bit).
The right-hand operand (shift amount) can be:
- An integer scalar (e.g.,
int,uint,int16_t, oruint16_t), which shifts both components of thei16vec2by the same value. - A two-component vector of matching dimensions (such as
u16vec2ori16vec2), shifting each component independently by its respective shift value.
Shifting by a negative value or by an amount greater than or equal to
16 bits (the bit-width of int16_t) produces undefined
results according to the GLSL specification.
Compound Bitwise Assignment Operators
GLSL supports compound bitwise assignment operators on
i16vec2 variables:
- AND-assignment (
&=) - OR-assignment (
|=) - XOR-assignment (
^=) - Left-shift assignment (
<<=) - Right-shift assignment
(
>>=)
Vector and Scalar Operands
Bitwise binary operators (&, |,
^) accept two i16vec2 operands or a
combination of an i16vec2 and a scalar
int16_t. When a scalar int16_t is mixed with
an i16vec2, the scalar is automatically broadcast to both
components before the bitwise operation takes place. Both operands must
have matching explicit precision and signedness; mixing signed
i16vec2 with unsigned types requires an explicit type cast
(such as u16vec2(...) or i16vec2(...)).