Skip to content

Instantly share code, notes, and snippets.

@jamesmccann-zz
Created November 7, 2014 03:46
Show Gist options
  • Save jamesmccann-zz/2e16cd33d1eeea105a2f to your computer and use it in GitHub Desktop.
Save jamesmccann-zz/2e16cd33d1eeea105a2f to your computer and use it in GitHub Desktop.
Covert repeating day flags to an integer
public static int booleanArrayToInt(boolean[] dayFlags) {
int result = 0;
for (int i = 0; i < dayFlags.length; i++) {
result += (dayFlags[i]? 1 : 0) * Math.pow(2, i);
}
return result;
}
public static boolean[] intToBooleanArray(int days) {
boolean[] result = new boolean[7];
for (int i = result.length - 1; i >= 0; i--) {
int pow = (int) Math.pow(2, i);
if (days > (pow - 1)) {
result[i] = true;
days -= pow;
} else {
result[i] = false;
}
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment