代码拉取完成,页面将自动刷新
/**
* Fundamental bit manipulation operations you must know Time Complexity: O(1)
*
* @author Micah Stairs
*/
package com.williamfiset.algorithms.other;
public class BitManipulations {
// Sets the i'th bit to 1
public static int setBit(int set, int i) {
return set | (1 << i);
}
// Checks if the i'th is set
public static boolean isSet(int set, int i) {
return (set & (1 << i)) != 0;
}
// Sets the i'th bit to zero
public static int clearBit(int set, int i) {
return set & ~(1 << i);
}
// Toggles the i'th bit from 0 -> 1 or 1 -> 0
public static int toggleBit(int set, int i) {
return set ^ (1 << i);
}
// Returns a number with the first n bits set to 1
public static int setAll(int n) {
return (1 << n) - 1;
}
// Verifies if a number n is a power of two
public static boolean isPowerOfTwo(int n) {
return n > 0 && (n & (n - 1)) == 0;
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。