Last active
March 13, 2019 20:58
-
-
Save dkohlsdorf/9a63495c673673afcdbe to your computer and use it in GitHub Desktop.
Implements a set on an integer
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
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