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. cigarette

    #include<iostream> using namespace std; int main() { int N; cin>>N; while(N--) { int Who ...

  2. Oracle 导入导出 创建用户等

    localhost:1158/emD:\app\Administrator\product\11.2.0\dbhome_1\bin\imp.exe log  path  E:\app\Administ ...

  3. Cisco路由器交换机配置命令详解

    1. 交换机支持的命令: 交换机基本状态:switch: :ROM状态, 路由器是rommon>hostname> :用户模式hostname# :特权模式hostname(config) ...

  4. 2008R2域控环境中 应用组策略 实现禁用USB设备使用

    本文介绍如何在Windows Server 2008 AD中禁用客户端USB端口.本文使用的系统:Windows Server 2008 R2 企业版.域功能级别:Windows Server 200 ...

  5. Python正则表达式初识(四)

    今天继续给大家分享Python正则表达式基础知识,主要给大家介绍一下特殊字符“{}”的用法,具体的教程如下. 特殊字符“{}”实质上也是一个限定词的用法,其限定前面字符所出现的次数,其常用的模式有三种 ...

  6. SpringMVC与SpringBoot返回静态页面遇到的问题

    1.SpringMVC静态页面响应 package com.sv.controller; import org.springframework.stereotype.Controller; impor ...

  7. GPU-directX的发展历史

    GPU发展历史: GPU之前的基础: 1962 麻省理工学院的博士伊凡•苏泽兰发表的论文以及他的画板程序奠定了计算机图形学的基础. 1962-1984 没有专门图形处理硬件,由CPU完成 1984 专 ...

  8. CodeForcesGym 100502G Outing

    Outing Time Limit: 1000ms Memory Limit: 524288KB This problem will be judged on CodeForcesGym. Origi ...

  9. [Python] Execute a Python Script

    Python scripts can be executed by passing the script name to the python command or created as execut ...

  10. ORACLE-016:ora-01720 授权选项对于&#39;xxxx&#39;不存在

    报错的情形例如以下. A用户:视图V_A B用户:视图V_B,而且用到了V_A C用户:须要用V_B, 授权过程, A用户下: grant select on V_A to B B用户下: grant ...