java常用位操作
来源:互联网 发布:好看男士衣服的淘宝店 编辑:程序博客网 时间:2024/05/22 15:01
public class TestIndex { /** * 数组越界 * 这个方法是java源码中常用的一个数组越界的判断检测 * 顺便复习一下位操作 */ public static int read(byte[] b, int off, int len) throws Exception { // parameter check int result = off | len | (off + len) |(b.length - (off+len)); if (result < 0) { System.out.println(result); } else if (len == 0) { return 0; } return result; } /** * 将一个高位在前的字节数组转为int * @param num * @return */ public static byte[] intToBytes(int num){ byte[] bytes = new byte[4]; bytes[0] = (byte) (num >>> 24); bytes[1] = (byte) (num >>> 16); bytes[2] = (byte) (num >>> 8); bytes[3] = (byte) (num >>> 0); return bytes; } /** * 将一个高位在前的字节数组转为int * @param bytes * @return */ public static int byteToInt(byte[] bytes){ int _1 = bytes[0] << 24; int _2 = bytes[1] << 16; int _3 = bytes[2] << 8; int _4 = bytes[3] << 0; return _1 | _2 | _3 | _4; } /** * 显示一个byte数组 * @param bytes */ public static void showByte(byte[] bytes){ for (byte b : bytes) { System.out.print(byteToBit(b) +" "); } System.out.println(); } /** * 将byte转换为一个长度为8的byte数组,数组每个值代表bit */ public static byte[] getBooleanArray(byte b) { byte[] array = new byte[8]; for (int i = 7; i >= 0; i--) { array[i] = (byte)(b & 1); b = (byte) (b >> 1); } return array; } /** * 把byte转为字符串的bit */ public static String byteToBit(byte b) { return "" + (byte) ((b >> 7) & 0x1) + (byte) ((b >> 6) & 0x1) + (byte) ((b >> 5) & 0x1) + (byte) ((b >> 4) & 0x1) + (byte) ((b >> 3) & 0x1) + (byte) ((b >> 2) & 0x1) + (byte) ((b >> 1) & 0x1) + (byte) ((b >> 0) & 0x1); } public static void main(String[] args) { int num = 100; byte[] bytes = intToBytes(num); showByte(bytes); num = byteToInt(bytes); System.out.println(num); }}
阅读全文
0 0
- java常用位操作
- 常用的位操作
- 常用位操作
- 常用位操作
- 常用位操作2
- Java常用位运算
- 位运算常用操作总结
- 位运算常用操作总结
- 位运算常用操作总结
- 位运算常用操作总结
- 常用位操作小技巧
- 常用位域操作宏
- 常用位操作小技巧
- 常用位操作小技巧
- 位运算常用操作总结
- 2.1.常用位操作符
- Java: 位操作
- java位操作符
- javascript基础练习-数字时钟
- Could not resolve all dependencies for configuration ':classpath'. Could not find com.android.t
- [莫比乌斯反演+数位DP]计蒜客 阿里云秘钥池
- 如何理解jsp页面中的“<base href="<%=basePath%>">”?
- 数据挖掘魔法学院(DMM)— 关于DMM关于我
- java常用位操作
- 关于Java咋记记不住的东西!很闹心~(1)
- 关于变量提升
- pymysql模块连接数据库详解
- NEFU 1268 区间最小值求和(单调队列||RMQ)
- 重拾springmvc(三)
- Leetcode_3Sum Closest
- SDNU1171.合并果子优先队列
- 前台正则表达式校验