Ai
1 Star 0 Fork 3

aiobc/Algorithms

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
BitManipulations.java 910 Bytes
一键复制 编辑 原始数据 按行查看 历史
Rafael Chaves 提交于 2020-02-03 02:07 +08:00 . Adopt standard Java project layout (#120)
/**
* 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;
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/aiobc/Algorithms.git
git@gitee.com:aiobc/Algorithms.git
aiobc
Algorithms
Algorithms
master

搜索帮助