Skip to content

Instantly share code, notes, and snippets.

@squeaky-pl
Last active August 29, 2015 13:56
Show Gist options
  • Save squeaky-pl/9048783 to your computer and use it in GitHub Desktop.
Save squeaky-pl/9048783 to your computer and use it in GitHub Desktop.
operation(x, y)
a <= x <= b; c <= y <= d
// 32 bit machine
unsigned maxOR(unsigned a, unsigned b,
unsigned c, unsigned d) {
unsigned m, temp;
m = 0x80000000;
while (m != 0) {
if (b & d & m) {
temp = (b - m) | (m - 1);
if (temp >= a) {b = temp; break;}
temp = (d - m) | (m - 1);
if (temp >= c) {d = temp; break;}
}
m = m >> 1;
}
return b | d;
}
unsigned maxAND(unsigned a, unsigned b,
unsigned c, unsigned d) {
unsigned m, temp;
m = 0x80000000;
while (m != 0) {
if (b & ~d & m) {
temp = (b & ~m) | (m - 1);
if (temp >= a) {b = temp; break;}
}
else if (~b & d & m) {
temp = (d & ~m) | (m - 1);
if (temp >= c) {d = temp; break;}
}
m = m >> 1;
}
return b & d;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment