GLSL位移运算符规则

位移操作符(<< 和 >>)

  位移操作符的两个操作数必须是有符号或无符号的整型或者整型数组. 一个操作数可以是有符号的, 而另一个是无符号的.
  在所有的情况里, 结果的类型都会与左操作符一致. 如果第一个操作数是标量, 那么第二个操作数也必须是标量. 如果第一个操作数是向量, 那么第二个操作数必须是一个标量或者是一个和左操作数长度相同的向量, 并且操作会对向量的每一个分量分别计算.
  如果右操作数是负数或者超出了左操作数的bit数, 结果将会是未定义的.
  E1 << E2 的结果是 E1 (按bit表示) 左移 E2 位.
  E1 >> E2 的结果是 E1 (按bit表示) 右移 E2 位.
  如果 E1 是一个有符号数, 右移的时候将会填充符号位; 如果 E1 是一个无符号数, 右移的时候会用 0 来填充.

原文
The OpenGL@ Shading Language, Version 4.60.5. Chapter 5.9 Expressions
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.

猜你喜欢

转载自blog.csdn.net/u010333737/article/details/83242068