Skip to content

Instantly share code, notes, and snippets.

@ashkrit
Last active June 23, 2020 14:01
Show Gist options
  • Save ashkrit/836d96992e8d00627202c1680dc49cfa to your computer and use it in GitHub Desktop.
Save ashkrit/836d96992e8d00627202c1680dc49cfa to your computer and use it in GitHub Desktop.
public class CustomerActivity {
private final int[] months = new int[12];
public void record(LocalDate day) {
int monthOffSet = day.getMonthValue() - 1;
int monthValue = months[monthOffSet];
// Set bit for day in 32 bit int and then OR(|) with month value to merge value
months[monthOffSet] = monthValue | 1 << (day.getDayOfMonth() - 1);
}
public int daysActive(Month month) {
int monthValue = months[month.ordinal()];
return countBits(monthValue);
}
public boolean wasActive(LocalDate day) {
int monthOffSet = day.getMonthValue() - 1;
int monthValue = months[monthOffSet];
// Set bit for day in 32 bit int and then AND(|) with month value to check if bit was set
return (monthValue & 1 << (day.getDayOfMonth() - 1)) > 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment