Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.BitSet;
- public class Mod3AlgoTest
- {
- public static void main(String[] args)
- {
- int maxNum = 65535;
- int nbits = 32 - Integer.numberOfLeadingZeros(maxNum);
- System.out.println("Number of bits: " + nbits);
- boolean allCorrect = true;
- for (int i = 0; i <= maxNum; i++) {
- int j = collapse(i, nbits);
- boolean correct = (i % 3) == (j % 3);
- allCorrect &= correct;
- }
- System.out.println("All correct? " + allCorrect);
- }
- public static int collapse(int nbr, int nbits) {
- BitSet bset = BitSet.valueOf(new long[] {nbr});
- int accu = 0;
- for (int i = 0; i < nbits; i++) {
- if (bset.get(i)) {
- accu += 1 + (i & 1);
- }
- }
- return accu;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement