-
-
Save galek/eace3fc96283c6b64d12db42efb15d7c to your computer and use it in GitHub Desktop.
Bitwise operations without operators for glsl.
This file contains hidden or 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