Skip to content

Instantly share code, notes, and snippets.

@galek
Forked from mattatz/BitwiseOperations.glsl
Created February 21, 2017 03:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save galek/eace3fc96283c6b64d12db42efb15d7c to your computer and use it in GitHub Desktop.
Save galek/eace3fc96283c6b64d12db42efb15d7c to your computer and use it in GitHub Desktop.
Bitwise operations without operators for glsl.
// BitwiseOperations.glsl
const int BIT_COUNT = 8;
int modi(int x, int y) {
return x - y * (x / y);
}
int or(int a, int b) {
int result = 0;
int n = 1;
for(int i = 0; i < BIT_COUNT; i++) {
if ((modi(a, 2) == 1) || (modi(b, 2) == 1)) {
result += n;
}
a = a / 2;
b = b / 2;
n = n * 2;
if(!(a > 0 || b > 0)) {
break;
}
}
return result;
}
int and(int a, int b) {
int result = 0;
int n = 1;
for(int i = 0; i < BIT_COUNT; i++) {
if ((modi(a, 2) == 1) && (modi(b, 2) == 1)) {
result += n;
}
a = a / 2;
b = b / 2;
n = n * 2;
if(!(a > 0 && b > 0)) {
break;
}
}
return result;
}
int not(int a) {
int result = 0;
int n = 1;
for(int i = 0; i < BIT_COUNT; i++) {
if (modi(a, 2) == 0) {
result += n;
}
a = a / 2;
n = n * 2;
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment