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. 【AHOI2013】【BZOJ3238】差异

    Description Input 一行,一个字符串S Output 一行.一个整数.表示所求值 Sample Input cacao Sample Output 54 HINT 2<=N< ...

  2. python之文件操作-复制、剪切、删除等

    以下是把sourceDir目录下的以.JPG结尾的文件所有拷贝到targetDir目录下: <span style="font-size:18px;">>> ...

  3. SQLServer2008 R2安装步骤

    1.解压缩sqlserver_2008_r2.iso到指定的目录,记住这个目录的位置 sqlserver_2008_r2.iso下载位置是:http://download.csdn.net/u0123 ...

  4. vue11 vue实例方法

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. poj--2007--Scrambled Polygon(数学几何基础)

    Scrambled Polygon Time Limit: 1000MS   Memory Limit: 30000KB   64bit IO Format: %I64d & %I64u Su ...

  6. noip 2018 day2 T1 旅行 基环树 tarjan

    Code: #include<cstdio> #include<cstring> #include<string> #include<stack> #i ...

  7. blkid---对系统块设备信息查询

    在Linux下可以使用blkid命令对查询设备上所采用文件系统类型进行查询.blkid主要用来对系统的块设备(包括交换分区)所使用的文件系统类型.LABEL.UUID等信息进行查询.要使用这个命令必须 ...

  8. ios程序启动过程和UIWidnow介绍

    一.iOS程序的完整启动过程(有storyboard) 1.先执行main函数,main内部会调用UIApplicationMain函数 2.UIApplicationMain函数里面做了什么事情: ...

  9. Mybatis使用注解进行增删改查

    // 增public interface StudentMapper{ @Insert("insert into student (stud_id, name, email, addr_id ...

  10. [React] Validate Custom React Component Props with PropTypes

    In this lesson we'll learn about how you can use the prop-types module to validate a custom React co ...