Created
November 7, 2014 03:46
-
-
Save jamesmccann-zz/2e16cd33d1eeea105a2f to your computer and use it in GitHub Desktop.
Covert repeating day flags to 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 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