Skip to content

Instantly share code, notes, and snippets.

@dkohlsdorf
Last active March 13, 2019 20:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dkohlsdorf/9a63495c673673afcdbe to your computer and use it in GitHub Desktop.
Save dkohlsdorf/9a63495c673673afcdbe to your computer and use it in GitHub Desktop.
Implements a set on an integer
public class BitSet {
private int set = 0;
public void add(int position) {
int mask = 1 << position;
set = set | mask;
}
public void union(BitSet other) {
set = set | other.getSet();
}
public void intersetction(BitSet other) {
set = set & other.getSet();
}
public int getSet() {
return set;
}
public boolean inset(int pos) {
return (set & (1 << pos)) != 0;
}
public static void main(String[] args) {
BitSet set = new BitSet();
set.add(4);
set.add(2);
BitSet set2 = new BitSet();
set2.add(2);
set2.add(6);
set2.add(8);
set.union(set2);
System.out.println(Integer.toBinaryString(set.getSet()));
System.out.println(Integer.toBinaryString(set.getSet()));
System.out.println(set.inset(1));
System.out.println(set.inset(2));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment