Skip to content

Instantly share code, notes, and snippets.

@phc15
Created January 29, 2021 07:16
Show Gist options
  • Save phc15/54132fc0f9f0795d12ea5a722d45fa55 to your computer and use it in GitHub Desktop.
Save phc15/54132fc0f9f0795d12ea5a722d45fa55 to your computer and use it in GitHub Desktop.
public class UpdateBits {
public static int update(int n, int m, int i, int j) {
int max = ~0;
int left = max << j+1;
int right = ((1 << i) - 1);
int mask = left | right;
int n_cleared = n & mask;
int m_shifted = m << i;
return n_cleared | m_shifted;
}
public static void main(String[] args) {
System.out.println(update(128, 5, 2, 4));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment