| 位运算 | 说明 | 使用 |
|---|---|---|
| &按位与 | 对应位都是1,结果为1,否则为0 | a&b |
| |按位或 | 对应位都是0,结果为0,否则为1 | a|b |
| ^按位非 | 对应位值相同,结果为0,否则为1 | a^b |
| ~按位取反 | 每一位变相反位,即0变成1,1变成0 | ~a |
| <<按位左移 | 低位补0 | a<<2 |
| >>按位右移 | 保留符号位,0为正,1为负 | a>>2 |
| >>>按位或 | 高位补0 | a>>>2 |
补充:
- 微机原理中,算数移动会保留符号位,逻辑移动不会保留符号位
- byte数组》整型:利用byte,&,0xFF,|和<<
- 整型》byte数组:利用value,&,0xFF,强制装换(byte),和>>
端序:字节序
- byte数据顺序与内存地址高低的关系
- 小端:将byte数组的低byte放在内存的低地址处
- 大端:将byte数组的低byte放在内存的高地址处
小端:一般都是小端
- 低字节在后,byte数组中序号小的在右边
大端:
- 高字节在后,byte数组中序号大的在右边
public static int bytesToIntLittleEndian(byte[] bytes) {
// byte数组中序号小的在右边
return bytes[0] & 0xFF | //
(bytes[1] & 0xFF) << 8 | //
(bytes[2] & 0xFF) << 16 | //
(bytes[3] & 0xFF) << 24; //
}
public static int bytesToIntBigEndian(byte[] bytes) {
// byte数组中序号大的在右边
return bytes[3] & 0xFF | //
(bytes[2] & 0xFF) << 8 | //
(bytes[1] & 0xFF) << 16 | //
(bytes[0] & 0xFF) << 24; //
}
int》bytes
public static byte[] intToBytesLittleEndian(short intValue) {
// byte数组中序号小的在右边
// 右边的数据放到byte数组中序号小的位置
byte[] bytes = new byte[Short.BYTES];
bytes[0] = (byte) (intValue & 0xff);
bytes[1] = (byte) ((intValue >> Byte.SIZE) & 0xff);
bytes[2] = (byte) ((intValue >> Byte.SIZE * 2) & 0xff);
bytes[3] = (byte) ((intValue >> Byte.SIZE * 3) & 0xff);
return bytes;
}
public static byte[] intToBytesBigEndian(short intValue) {
// byte数组中序号大的在右边
// 右边的数据放到byte数组中序号大的位置
byte[] bytes = new byte[Short.BYTES];
bytes[3] = (byte) (intValue & 0xff);
bytes[2] = (byte) ((intValue >> Byte.SIZE) & 0xff);
bytes[1] = (byte) ((intValue >> Byte.SIZE * 2) & 0xff);
bytes[0] = (byte) ((intValue >> Byte.SIZE * 3) & 0xff);
return bytes;
}
bytes》short
public static short bytesToShortLittleEndian(byte[] bytes) {
// byte数组中序号小的在右边
return (short) (bytes[0] & 0xFF | (bytes[1] & 0xFF) << 8);
}
public static short bytesToShortBigEndian(byte[] bytes) {
// byte数组中序号大的在右边
return (short) (bytes[1] & 0xFF | (bytes[0] & 0xFF) << 8);
}
short》bytes
public static byte[] shortToBytesLittleEndian(short shortValue) {
// byte数组中序号小的在右边
// 右边的数据放到byte数组中序号小的位置
byte[] bytes = new byte[Short.BYTES];
bytes[0] = (byte) (shortValue & 0xff);
bytes[1] = (byte) ((shortValue >> Byte.SIZE) & 0xff);
return bytes;
}
public static byte[] shortToBytesBigEndian(short shortValue) {
// byte数组中序号大的在右边
// 右边的数据放到byte数组中序号大的位置
byte[] bytes = new byte[Short.BYTES];
bytes[1] = (byte) (shortValue & 0xff);
bytes[0] = (byte) ((shortValue >> Byte.SIZE) & 0xff);
return bytes;
}
bytes》long
public static long bytesToLongLittleEndian(byte[] bytes) {
// byte数组中序号小的在右边
long values = 0;
for (int i = (Long.BYTES - 1); i >= 0; i--) {
values <<= Byte.SIZE;
values |= (bytes[i] & 0xff);
}
return values;
}
public static long bytesToLongBigEndian(byte[] bytes) {
// byte数组中序号大的在右边
long values = 0;
for (int i = 0; i < Long.BYTES; i++) {
values <<= Byte.SIZE;
values |= (bytes[i] & 0xff);
}
return values;
}
long》bytes
public static byte[] longToBytesLittleEndian(long longValue) {
// byte数组中序号小的在右边
// 右边的数据放到byte数组中序号小的位置
byte[] result = new byte[Long.BYTES];
for (int i = 0; i < result.length; i++) {
result[i] = (byte) (longValue & 0xFF);
longValue >>= Byte.SIZE;
}
return result;
}
public static byte[] longToBytesBigEndian(long longValue) {
// byte数组中序号大的在右边
// 右边的数据放到byte数组中序号大的位置
byte[] result = new byte[Long.BYTES];
for (int i = (result.length - 1); i >= 0; i--) {
result[i] = (byte) (longValue & 0xFF);
longValue >>= Byte.SIZE;
}
return result;
}



