Created
November 13, 2015 06:46
-
-
Save mattatz/70b96f8c57d4ba1ad2cd to your computer and use it in GitHub Desktop.
Bitwise operations without operators for glsl.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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
Isn't "not" just
int r = 0xFFFFFFFF - a;
?