Skip to content

Instantly share code, notes, and snippets.

@JoseRivas1998
Last active June 1, 2018 00:01
Show Gist options
  • Select an option

  • Save JoseRivas1998/679f46003d1bc05d9023e6af86724c3f to your computer and use it in GitHub Desktop.

Select an option

Save JoseRivas1998/679f46003d1bc05d9023e6af86724c3f to your computer and use it in GitHub Desktop.
An Introduction to Bit Operations, Masking and Filtering
public class BitFilteringTester {
InteractingObject a = new InteractingObject('a');
InteractingObject b = new InteractingObject('b');
InteractingObject c = new InteractingObject('c');
InteractingObject d = new InteractingObject('d');
InteractingObject e = new InteractingObject('e');
InteractingObject f = new InteractingObject('f');
public BitFilteringTester() {
a.filter.category = InteractingObject.TYPE_A;
a.filter.maskBits = InteractingObject.TYPE_B;
b.filter.category = InteractingObject.TYPE_B;
b.filter.maskBits = InteractingObject.TYPE_A;
c.filter.category = InteractingObject.TYPE_C;
c.filter.maskBits = InteractingObject.TYPE_A | InteractingObject.TYPE_B | InteractingObject.TYPE_C;
d.filter.category = InteractingObject.TYPE_D;
d.filter.maskBits = InteractingObject.TYPE_D;
e.filter.category = InteractingObject.TYPE_E;
e.filter.maskBits = 0;
}
public void runTest() {
interactAll(a);
interactAll(b);
interactAll(c);
interactAll(d);
interactAll(e);
interactAll(f);
}
public void interactAll(InteractingObject o) {
interact(o, a);
interact(o, b);
interact(o, c);
interact(o, d);
interact(o, e);
interact(o, f);
System.out.println();
}
public void interact(InteractingObject o1, InteractingObject o2) {
Filter f1 = o1.filter;
Filter f2 = o2.filter;
if(((f1.category & f2.maskBits) != 0) && ((f2.category & f1.maskBits) != 0)) {
System.out.printf("%s and %s interacted.\n", o1.data, o2.data);
} else {
System.out.printf("%s and %s did not interact.\n", o1.data, o2.data);
}
}
public static void main(String[] args) {
BitFilteringTester tester = new BitFilteringTester();
tester.runTest();
}
class InteractingObject {
public static final int TYPE_A = 1 << 1;
public static final int TYPE_B = 1 << 2;
public static final int TYPE_C = 1 << 3;
public static final int TYPE_D = 1 << 4;
public static final int TYPE_E = 1 << 5;
public Filter filter;
public char data;
public InteractingObject(char data) {
this.data = data;
this.filter = new Filter();
}
}
class Filter {
public int category = 1<<0;
public int maskBits = ~0;
}
}
public class BitMaskingTester {
public static final byte EXCLUSIVE = 0;
public static final byte INCLUDE_LEFT_ENDPOINT = 1<<0;
public static final byte INCLUDE_RIGHT_ENDPOINT = 1<<1;
public static void main(String []args){
testInterval(3, 2, 5);
testInterval(2, 2, 5);
testInterval(5, 2, 5);
testInterval(1, 2, 5);
testInterval(6, 2, 5);
}
public static void testInterval(int n, int min, int max) {
System.out.printf("%d on interval (%d, %d): %b\n", n, min, max, isOnInterval(n, min, max, EXCLUSIVE));
System.out.printf("%d on interval [%d, %d): %b\n", n, min, max, isOnInterval(n, min, max, INCLUDE_LEFT_ENDPOINT));
System.out.printf("%d on interval (%d, %d]: %b\n", n, min, max, isOnInterval(n, min, max, INCLUDE_RIGHT_ENDPOINT));
System.out.printf("%d on interval [%d, %d]: %b\n", n, min, max, isOnInterval(n, min, max, INCLUDE_LEFT_ENDPOINT | INCLUDE_RIGHT_ENDPOINT));
System.out.println();
}
public static boolean isOnInterval(int n, int min, int max, int options) {
boolean greaterThanMin;
if((INCLUDE_LEFT_ENDPOINT & options) != 0) {
greaterThanMin = n >= min;
} else {
greaterThanMin = n > min;
}
boolean lessThanMax;
if((INCLUDE_RIGHT_ENDPOINT & options) != 0) {
lessThanMax = n <= max;
} else {
lessThanMax = n < max;
}
return greaterThanMin && lessThanMax;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment