算法:位运算加减乘除
今日做leetCode的371题算法题,用位运算计算两个数之和。借此机会,将位运算的加减乘除做一整理。
package Algorithm;
public class Code371 {
public static void main(String[] args) {
Code371 code = new Code371();
System.out.println(code.add(27, 35));
System.out.println(code.sub(27, 35));
System.out.println(code.posMutiply(27, 35));
System.out.println(code.mutiply(-27, -35));
System.out.println(code.posDiv(57, 35));
System.out.println(code.div(-57, 35));
System.out.println(code.isPos(35));
System.out.println(code.isNeg(-23));
System.out.println(code.isZero(0));
}
// 加法运算
public int add(int a, int b) {
if (b == 0)
return a;
int sum = a ^ b;
int carry = (a & b) << 1;
return add(sum, carry);
}
// 取补码
public int negtive(int a) {
return add(~a, 1);
}
// 减法运算
public int sub(int a, int b) {
return add(a, negtive(b));
}
// 正数乘法运算
public int posMutiply(int a, int b) {
int ans = 0;
while (b > 0) {
if ((int) (b & 1) > 0)
ans = add(ans, a);
a = (a << 1);
b = (b >> 1);
}
return ans;
}
public int mutiply(int a, int b){
if(isZero(a) || isZero(b)){
return 0;
}
int mutiplyResult = posMutiply(isPos(a)? a: negtive(a), isPos(b)? b:negtive(b));
if(isPos(a) ^ isPos(b)){
return negtive(mutiplyResult);
}
return mutiplyResult;
}
// 正数除法运算
public int posDiv(int x, int y) {
int ans = 0;
for (int i = 31; i >= 0; i--) {
// 比较x是否大于y的(1<<i)次方,避免将x与(y<<i)比较,因为不确定y的(1<<i)次方是否溢出
if ((x >> i) >= y) {
ans += (1 << i);
x -= (y << i);
}
}
return ans;
}
public int div(int x, int y){
if(isZero(y)){
System.exit(1);
}
if(isZero(x)){
return 0;
}
int divResult = posDiv(isPos(x)? x: negtive(x), isPos(y)? y:negtive(y));
if(isPos(x) ^ isPos(y)){
return negtive(divResult);
}
return divResult;
}
// 正数
public boolean isPos(int a) {
return (a & 0xFFFF) != 0 && (a & 0x8000) == 0;
}
// 负数
public boolean isNeg(int a) {
return (a & 0x8000) != 0;
}
// 0
public boolean isZero(int a) {
return (a & 0xFFFF) == 0;
}
}
参考资料
1.用位运算实现四则运算之加减乘除(用位运算求一个数的1/3),CSDN博客,http://blog.csdn.net/hackbuteer1/article/details/7390093
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端
· AI Agent开发,如何调用三方的API Function,是通过提示词来发起调用的吗