View Full Version : Bit-Wise Shift

09-01-2011, 08:04 AM
Using only GLSL 1.20.8, how can I perform an unsigned bitwise shift ot an integer?

I used something like this to do left shift by m bits:

int i = 0x4CA7; // some value...

int s = i * int(pow(2, m));


int r = s / int(pow(2, n));

to perform right-shift by n-bits

The result was not what I wanted, and I believe it's because the signed computations that screws up the result.

Any idea?

09-01-2011, 01:29 PM
GLSL 1.20-only capable cards don't have native integer support thus there is not shift operators in GLSL. You need at least GLSL 1.30 to do that.

09-01-2011, 02:26 PM
Ok what I want to do is extract the bit value (1 or 0) from a word based on an index into that word's bits. For example:

int v = somevalue;

int i = 3; // an index into "v" bits;

int b = giveMeBinaryValue(i, v); // returns either 0 or 1

Can anybody help me with this homework? :D just kidding :D

But I'm really restricted to GLSL 1.20.8.

09-01-2011, 03:09 PM
On pre-DX10 hardware, the differences between ATI, NVIDIA and Intel hardware can give different results with the same shader, so your code may be right, but the float point differences maybe the reason you aren't getting the result you expect. There are instances where you'll have to use floor/ceil. But yes, it does look like homework. This post (http://stackoverflow.com/questions/3466567/how-to-perform-bit-shift-without-operator-efficiently) will guide you in the right direction.