Java之“& 0x1”

来源:互联网 发布:网络借贷宝怎么还款啊 编辑:程序博客网 时间:2024/05/01 20:56

   第一感觉这是一个与byte类型数据相关的运算符!


package bytes; public class ByteToBit { /** * @param args */public static void main(String[] args) {// TODO Auto-generated method stubSystem.out.println(byteToBit(new Integer(10).byteValue()));} 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); }}

  b >> 7 将原第8位的bit值移到了第1位上,& 0x1的作用是只保留第一位的值,其余7位与0与将为0


参考至:http://www.plsyard.com/category/java/page/2/


0 0