Java bit operation bitwise (bitwise) Operation bit

Source: Internet
Author: User
Tags bit set

Java bit operation bitwise (bitwise) Operation bit

8 0000 0000 0000 1000 Original Code
1111 1111 1111 0111 anti-code

+ 1

1111 1111 1111 1000 (8 of the complement) to represent-8

-8 1111 1111 1111 1000 65528 complement (positive inverse code + 1)

65535 1111 1111) 1111 111165535
65535-65528=7+1=8

Operation Longvalue = Longvalue | (1 << N); You can set the value from the right count to the left number n + 1 bit to 1 in the Longvalue 2 binary representation, and do not affect the value above the other bits to do or |or operation with the 0 and 2 binary value variable x, without affecting the value of the 2 binary variable x

Operation Longvalue = Longvalue & ~ (1 << N); Can be in the Longvalue 2 binary representation, the number from the right to the left of the Nth + 1 bit set to 0, and does not affect the other bits above the value of 1 and 2 binary value variable x do with the &and operation, does not affect the value of the 2 binary variable x

Operation System.out.println ((Longvalue >> N & 1) = = 1); Can determine the value of the Longvalue 2 binary representation, from the right to the left of the Nth + 1 bit value is 0false or 1true

Java code
  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. // One
  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. //
  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 = n, 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", "$", "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 =
  98. System.out.println ("~a =" + binary[g] + "=" + g);
  99. System.out.println ("\ r \ n");
  100. }
  101. }

Java bit operation bitwise (bitwise) Operation bit

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.