# Does the GLSL bit shift operator implicit convert type when two operands type mismatch?

From GLSL spec that describes shift operators:

The shift operators (<<) and (>>). For both operators, the operands must be signed or unsigned integers or integer vectors. One operand can be signed while the other is unsigned. In all cases, the resulting type will be the same type as the left operand. If the first operand is a scalar, the second operand has to be a scalar as well. If the first operand is a vector, the second operand must be a scalar or a vector with the same size as the first operand, and the result is computed component-wise. The result is undefined if the right operand is negative, or greater than or equal to the number of bits in the left expression’s base type. The value of E1 << E2 is E1 (interpreted as a bit pattern) left-shifted by E2 bits. The value of E1 >> E2 is E1 right-shifted by E2 bit positions. If E1 is a signed integer, the right-shift will extend the sign bit. If E1 is an unsigned integer, the right-shift will zero-extend.

I check other binary operators (+, -, … %) descriptions, these all include the same pattern – “if two operands don’t mismatch, then will be an implicit conversion”, only the bit shift operator doesn’t have this.

The spec says the only valid condition is that the type is either integer scalar or integer vector. In some cases, such as two operands are both scalars but one is signed and the other is unsigned or one is int64_t and the other is int, how the shift operator works, and will it perform the implicit conversion in the right side operands?

What you quoted tells you everything you need to know about the behavior of these operands. No conversion is needed.

The result type of the expression is well-defined: “In all cases, the resulting type will be the same type as the left operand.” So there is no “conversion” for E1.

E2 can be a signed type, but the value of it cannot be negative (“The result is undefined if the right operand is negative”). And since GLSL defines signed integers to be two’s complement, this means that E2 will have the exact same bit representation for all valid values, whether it is signed or not.

There is no “conversion” for E2.

So there are no conversions. Which is why it doesn’t speak of any.

1 Like

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.