位运算
# 测试代码
package com.yf.websocket.util;
import cn.hutool.core.util.HexUtil;
import java.util.Arrays;
public class BaseTest {
public static void main(String[] args) {
BaseTest.math(-10,1,">>");
BaseTest.math(-10,1,">>>");
BaseTest.math(-10,1,"<<");
BaseTest.math(10,1,"&");
BaseTest.math(10,1,"|");
BaseTest.math(10,1,"^");
BaseTest.math(10,1,"~");
}
public static String toBinaryString(int a ){
String binaryString = Integer.toBinaryString(a);
if(32 == binaryString.length()){
return binaryString;
}
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < (32 - binaryString.length()); i++) {
stringBuilder.append("0");
}
stringBuilder.append(binaryString);
return stringBuilder.toString();
}
public static void math(int a ,int b,String operator){
System.out.format("二进制:%s \n",BaseTest.toBinaryString(a));
int result;
switch (operator){
case ">>":
System.out.format("带符号右移 %d 位 \n",b);
System.out.format("运算:(a)%d %s (b)%d ,规则: 低位移除,高位补0,负数高位补1 ,相当于 %d/2 \n",a,operator,b,a);
result = (a >> b );
break;
case ">>>":
System.out.format("不带符号右移 %d 位 \n",b);
System.out.format("运算:(a)%d %s (b)%d ,规则: 低位移除,高位补0 \n",a,operator,b,a);
result = (a >>> b );
break;
case "<<":
System.out.format("左移 %d 位 \n",b);
System.out.format("运算:(a)%d %s (b)%d ,规则: 高位移除,低位补0 ,相当于 %d*2 \n",a,operator,b,a);
result = (a << b );
break;
case "|":
System.out.format("二进制:%s \n",BaseTest.toBinaryString(b));
System.out.format("运算:(a)%d %s (b)%d ,规则: 任意一个位为1 值为1,常用来设置1 \n",a,operator,b);
result = (a | b );
break;
case "&":
System.out.format("二进制:%s \n",BaseTest.toBinaryString(b));
System.out.format("运算:(a)%d %s (b)%d ,规则: 两个位为1 值为1,常用来设置0\n",a,operator,b);
System.out.format("用法");
System.out.format("设置0");
System.out.format("判读奇数偶数,二进制位0位偶数,1为奇数");
result = (a & b );
break;
case "^":
System.out.format("二进制:%s \n",BaseTest.toBinaryString(b));
System.out.format("运算:(a)%d %s (b)%d ,规则: 两个位相同为0 不同为1 \n",a,operator,b);
result = (a^b);
break;
case "~":
System.out.format("取反 %d 规则:0变1,1变0 \n",a);
result = (~a);
break;
default:
throw new RuntimeException("运算符异常");
}
System.out.format("二进制:%s => c=%d\n",BaseTest.toBinaryString(result),result);
System.out.println();
}
}
# 结果
二进制:11111111111111111111111111110110
带符号右移 1 位
运算:(a)-10 >> (b)1 ,规则: 低位移除,高位补0,负数高位补1 ,相当于 -10/2
二进制:11111111111111111111111111111011 => c=-5
二进制:11111111111111111111111111110110
不带符号右移 1 位
运算:(a)-10 >>> (b)1 ,规则: 低位移除,高位补0
二进制:01111111111111111111111111111011 => c=2147483643
二进制:11111111111111111111111111110110
左移 1 位
运算:(a)-10 << (b)1 ,规则: 高位移除,低位补0 ,相当于 -10*2
二进制:11111111111111111111111111101100 => c=-20
二进制:00000000000000000000000000001010
二进制:00000000000000000000000000000001
运算:(a)10 & (b)1 ,规则: 两个位为1 值为1,常用来设置0
用法设置0判读奇数偶数,二进制位0位偶数,1为奇数二进制:00000000000000000000000000000000 => c=0
二进制:00000000000000000000000000001010
二进制:00000000000000000000000000000001
运算:(a)10 | (b)1 ,规则: 任意一个位为1 值为1,常用来设置1
二进制:00000000000000000000000000001011 => c=11
二进制:00000000000000000000000000001010
二进制:00000000000000000000000000000001
运算:(a)10 ^ (b)1 ,规则: 两个位相同为0 不同为1
二进制:00000000000000000000000000001011 => c=11
二进制:00000000000000000000000000001010
取反 10 规则:0变1,1变0
二进制:11111111111111111111111111110101 => c=-11
上次更新: 2024/11/05, 08:29:31