Created
February 13, 2014 05:57
-
-
Save jostyee/8970490 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 int clearBits(int num, int i) { | |
int mask = (1 << i) - 1; | |
return num & mask; | |
} |
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 int clearBitsIthrough0( int num, int i) { | |
int mask = ~((1 << (i + 1)) - 1); | |
return num & mask; | |
} |
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 int updateBit(int num, int i, int v) { | |
int mask = ~(1 << i); | |
return (num & mask) | (v << i); | |
} |
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 int clearBit(int num, int i) { | |
int mask = ~(1 << i); | |
return num & mask; | |
} |
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 int setBit(int num, int i) { | |
return num | (1 << i); | |
} |
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 boolean getBit(int num, int i) { | |
return ((num & (1 << i)) != 0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment