Bit-Wise Shift

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?

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.

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? :smiley: just kidding :smiley:

But I’m really restricted to GLSL 1.20.8.

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 will guide you in the right direction.