Skip to content

Instantly share code, notes, and snippets.

@mattatz
Created November 13, 2015 06:46
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save mattatz/70b96f8c57d4ba1ad2cd to your computer and use it in GitHub Desktop.
Save mattatz/70b96f8c57d4ba1ad2cd 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;
}
@coffeenotfound
Copy link

Isn't "not" just int r = 0xFFFFFFFF - a;?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment