Created
January 29, 2021 07:16
-
-
Save phc15/54132fc0f9f0795d12ea5a722d45fa55 to your computer and use it in GitHub Desktop.
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 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