java 位操作 bitwise(按位) operation bit

//一篇对于 原码 反码 补码 的介绍

http://www.cnblogs.com/zhangziqiu/archive/2011/03/30/ComputerCode.html

java中有三种移位运算符

<<      :     左移运算符,num << 1,相当于num乘以2

>>      :     右移运算符,num >> 1,相当于num除以2

>>>    :     无符号右移,忽略符号位,空位都以0补齐

// 8   0000 0000 0000 1000     原码
      1111 1111 1111 0111     反码

+              1

   1111 1111 1111 1000     (8的补码)来表示 -8

// -8 1111 1111 1111 1000 65528     补码(正值 的反码+1)

// 65535 1111 1111 1111 1111 65535
// 65535-65528=7+1=8

 
操作longValue = longValue | (1 << n); 可以在longValue的2进制表示中,把从右边数到左边数第n + 1位的值设置为1,并且不影响其他位上面的值   即用0和2进制值变量x做或|or操作,不会影响到2进制变量x的值

操作longValue = longValue & ~(1 << n); 可以在longValue的2进制表示中,把从右边数到左边数第n + 1位设置为0,并且不影响其他位上面的值   即用1和2进制值变量x做与&and操作,不会影响到2进制变量x的值

操作System.out.println((longValue >> n & 1) == 1); 可以判断值longValue的2进制表示中,从右边数到左边第n + 1位的值是0false 还是1true

  1. public class bitOperation {
  2. /**
  3. * @param args
  4. */
  5. public static void main(String[] args) {
  6. long longValue = 0;
  7. longValue = longValue | (1 << 0);
  8. // 1
  9. System.out.println(Long.toBinaryString(longValue));
  10. longValue = longValue | (1 << 1);
  11. // 11
  12. System.out.println(Long.toBinaryString(longValue));
  13. longValue = longValue | (1 << 4);
  14. // 10011
  15. System.out.println(Long.toBinaryString(longValue));
  16. longValue = longValue | (1 << 5);
  17. // 110011
  18. System.out.println(Long.toBinaryString(longValue));
  19. longValue = longValue | (1 << 6);
  20. // 1110011
  21. System.out.println(Long.toBinaryString(longValue));
  22. String hex = Long.toBinaryString(longValue);
  23. // 1110011
  24. System.out.println(hex);
  25. // 115
  26. System.out.println(Integer.valueOf("1110011", 2));
  27. // 1110011
  28. System.out.println(Long.toBinaryString(longValue >> 0));
  29. // 1
  30. System.out.println(Long.toBinaryString(longValue >> 0 & 1));
  31. // 111001
  32. System.out.println(Long.toBinaryString(longValue >> 1));
  33. // 1
  34. System.out.println(Long.toBinaryString(longValue >> 1 & 1));
  35. // true
  36. System.out.println((longValue >> 0 & 1) == 1);
  37. // true
  38. System.out.println((longValue >> 1 & 1) == 1);
  39. // false
  40. System.out.println((longValue >> 2 & 1) == 1);
  41. // false
  42. System.out.println((longValue >> 3 & 1) == 1);
  43. // true
  44. System.out.println((longValue >> 4 & 1) == 1);
  45. // true
  46. System.out.println((longValue >> 5 & 1) == 1);
  47. // true
  48. System.out.println((longValue >> 6 & 1) == 1);
  49. // false
  50. System.out.println((longValue >> 7 & 1) == 1);
  51. // Demonstrate the bitwise logical operators.
  52. bitLogic();
  53. // Left shifting a byte value.
  54. byteShift();
  55. }
  56. /**
  57. * Left shifting a byte value.
  58. */
  59. private static void byteShift() {
  60. byte a = 64, b;
  61. int i;
  62. i = a << 2;
  63. b = (byte) (a << 2);
  64. // Original value of a: 64
  65. System.out.println("Original value of a: " + a);
  66. // i and b: 256 0
  67. System.out.println("i and b: " + i + " " + b);
  68. System.out.println("\r\n");
  69. }
  70. /**
  71. * Demonstrate the bitwise logical operators.
  72. */
  73. private static void bitLogic() {
  74. String binary[] = { "0000", "0001", "0010", "0011", "0100", "0101",
  75. "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101",
  76. "1110", "1111"
  77. };
  78. int a = 3; // 0 + 2 + 1 or 0011 in binary
  79. int b = 6; // 4 + 2 + 0 or 0110 in binary
  80. int c = a | b;
  81. int d = a & b;
  82. int e = a ^ b;
  83. int f = (~a & b) | (a & ~b);
  84. int g = ~a & 0x0f;
  85. // a = 0011 = 3
  86. System.out.println(" a = " + binary[a] + " = " + a);
  87. // b = 0110 = 6
  88. System.out.println(" b = " + binary[b] + " = " + b);
  89. // a|b = 0111 = 7
  90. System.out.println(" a|b = " + binary[c] + " = " + c);
  91. // a&b = 0010 = 2
  92. System.out.println(" a&b = " + binary[d] + " = " + d);
  93. // a^b = 0101 = 5
  94. System.out.println(" a^b = " + binary[e] + " = " + e);
  95. // ~a&b|a&~b = 0101 = 5
  96. System.out.println("~a&b|a&~b = " + binary[f] + " = " + f);
  97. // ~a = 1100 = 12
  98. System.out.println(" ~a = " + binary[g] + " = " + g);
  99. System.out.println("\r\n");
  100. }
  101. }

java 位操作 bitwise(按位) operation bit的更多相关文章

  1. Java位操作全面总结

    转载: Java位操作全面总结 在计算机中所有数据都是以二进制的形式储存的.位运算其实就是直接对在内存中的二进制数据进行操作,因此处理数据的速度非常快.在实际编程中,如果能巧妙运用位操作,完全可以达到 ...

  2. JAVA 位操作学习

    一,基础知识 计算机中数值的编码方式中,原码.反码.补码. 正数的补码与原码相同,负数的补码为:负数的原码符号位不变,其它位取反,再加1. 在计算机中,数值是以补码的形式存储的.补码的好处: ①用补码 ...

  3. Java位操作全面总结[ZZ]

    Java位操作全面总结 在计算机中所有数据都是以二进制的形式储存的.位运算其实就是直接对在内存中的二进制数据进行操作,因此处理数据的速度非常快.在实际编程中,如果能巧妙运用位操作,完全可以达到四两拨千 ...

  4. Java float保留两位小数或多位小数

    Java float保留两位小数或多位小数 方法1:用Math.round计算,这里返回的数字格式的.    float price=89.89;int itemNum=3;float totalPr ...

  5. 转:java开发的10位牛人

    文章来自于:http://it.deepinmind.com/java/2014/04/10/top-10-java-people-you-should-know.html James Gosling ...

  6. Java随机获取32位密码且必须包含大小写字母、数字和特殊字符,四种的任意三种

    Java随机获取32位密码且必须包含大小写字母.数字和特殊字符,四种的任意三种 Java随机获取32位密码且必须包含大小写字母.数字和特殊字符,四种的任意三种,代码如下: import java.ut ...

  7. 你知道吗?Java开发的10位牛人

    James Gosling 1983年,Gosling获得了加州大学的计算机科学学士学位.1990年,他获得了卡内基梅隆大学的计算机科学博士学位,师从Bob Sproull.在攻读博士期间,他自己开发 ...

  8. Java生成前三位是字母循环的字典

    title: Java生成前三位是字母循环的字典 date: 2018-08-17 18:52:22 tags: Java --- 最近要破解一个秘密,还好这个密码是有线索的,已知密码的前三位是三个字 ...

  9. java 随机生成4位随机数

    java 随机生成4位的随机数测试类 @org.junit.Testpublic void testRandom(){ String msg="您的注册码为%s,谢谢注册!"; S ...

随机推荐

  1. ArcGIS api for javascript——动态创建图层列表

    描述 本例循环地图服务里的所有图层并增加每个图层到一个带checkbox的列表,checkbox能设置图层的显示或隐藏.动态创建列表的优势是所有的图层都会包含在列表中,即使服务器管理员删除或增加了图层 ...

  2. new期间的异常

    new包含两步,调用operator new申请空间,以及调用构造函数. 如果第一步结束之后,第二步发生异常,需要归还第一步的空间. 编译器帮我们做了这件事情,并且会调用对应的delete. 另外 n ...

  3. Use Uncertainty As a Driver

     Use Uncertainty As a Driver Kevlin Henney ConFRonTEd WiTH TWo opTionS, most people think that the ...

  4. Spark技术内幕:Client,Master和Worker 通信源代码解析

    Spark的Cluster Manager能够有几种部署模式: Standlone Mesos YARN EC2 Local 在向集群提交计算任务后,系统的运算模型就是Driver Program定义 ...

  5. Azure 配置高可用的准备系列工作-建立不同区域的存储账户和建立网络!

     我们谈到我们的业务,常常谈到一个词.三层架构,就是我们的UI层.数据訪问层和数据存储层的分离,通常情况下我们的业务高可用必须满足这三层的所有高可用的情况下才干达到最高级别的高可用. 那么谈到Az ...

  6. css footer not displaying at the bottom of the page

    https://stackoverflow.com/questions/15960290/css-footer-not-displaying-at-the-bottom-of-the-page The ...

  7. Word 操作组件介绍 - Spire.Doc

    http://www.cnblogs.com/liqingwen/p/5898368.html

  8. 系统管理员的 SELinux 指南:这个大问题的 42 个答案

    安全.坚固.遵从性.策略是末世中系统管理员的四骑士.除了我们的日常任务之外 —— 监控.备份.实施.调优.更新等等 —— 我们还需要负责我们的系统安全.即使这些系统是第三方提供商告诉我们该禁用增强安全 ...

  9. yum配置中driver-class-name: com.mysql.jdbc.Driver报错

    错误: 原因: 解决方法:把方框中的<scope>runtime</scope>删掉

  10. oracle 正序 逆序 排序查询

    正序:从小到大 order by t.id asc 逆序:从大到小 order by t.id desc