Binary binary in Java

Source: Internet
Author: User
Tags bitwise

(1) Bitwise AND Arithmetic &

1 & 1 = 1, 0 & 1 = 0

Wuyi & 5 i.e. 0011 0011 & 0000 0101 = 0000 0001 = 1;

(2) bitwise OR Operation | 1 | 0 = 1,1|1 = 1, 0|0 = 0 51 | 5 or 0011 0011 | 0000 0101 = 0011 0111 = 55; (3) XOR operation ^

1 ^ 1 = 0,1 ^ 0 = 1, 0^ 0 = 0 (two bits have different values, the result is 1, the same result is 0)

51 ^ 5 is 0011 0011 ^0000 0101 = 0011 0110=54;

(4) << left shift operator

1. Shift all bits of an operand to the left of a number of bits (left binary discard, 0 on the right)

(Note: full-digit 32-bit in Java)

<< 2 = 44

-14 <<2 =-56

-14 Binary (11111111 11111111 11111111 11110010) shift left 2 bit

For 11111111 11111111 11111111 11001000

The result is (-56) (followed by how negative numbers are represented in the binary)

(5) >> right shift operator

All the bits of an operand are shifted to the right by several digits, the positive left is 0, and the negative left is 1.

4 >> 2 = 1;

-14 >> 2 =-4;

(6) ~ Bitwise REVERSE

~ =-7

(7) >>> unsigned Right shift operator

The binary bits move the specified number of bits to the right, and the left empty bits are filled with zero after the right shift, and the bits to the right are discarded.

-14 >>> 2 =11111111 11111111 11111111 11110010 = 00111111 11111111 11111111 11111100 =1073741820

(8) <<< unsigned left shift operator

The binary bits move the specified number of bits to the left, and the left-hand side is padded with zero, and the left bit is discarded.

3 <<< 1 = 6

(9) Calculation of negative numbers in binary

Negative numbers are expressed in the complement of positive numbers

Original code: An integer converted to a binary number by the size of the absolute value

Inverse code: The binary number is reversed by a bitwise

Complement: Anti-code plus 1

With a-14 example

Original code: 14 that is 00000000 00000000 00000000 00001110

Reverse code: 11111111 11111111 11111111 11110001

Complement: 11111111 11111111 11111111 11110010

So the binary of 14 is 11111111 11111111 11111111 11110010

Let's say we get the binary, we're going to ask for an integer that is backwards to take the opposite number.

If binary is 11111111 11111111 11111111 11110010

Get anti-code minus 1 11111111 11111111 11111111 11110001

Original code: 00000000 00000000 00000000 00001110

That is 1110 = 14 so the inverse is-14

[Java]View PlainCopy 
  1. Public static void Main (string[] args) {
  2. /* 
  3. * Decimal conversion to other binary
  4. */
  5. //Binary
  6. System.out.println (Integer.tobinarystring (0));
  7. //16 in the system
  8. System.out.println (integer.tohexstring (112));
  9. //8 in the system
  10. System.out.println (integer.tooctalstring (112));
  11. /* 
  12. * Other binary conversions to be decimal
  13. */
  14. //Binary
  15. System.out.println (Integer.parseint ("1110000", 2));
  16. //8 in the system
  17. System.out.println (Integer.parseint (" 8)");
  18. //16 in the system
  19. System.out.println (Integer.parseint ("A1", 16));
  20. }



[Java]View PlainCopy  
  1. Public class Phone {
  2. /* 
  3. * Convert int to byte array
  4. */
  5. public static byte[] Int2bytes (int id) {
  6. byte[] arr = new byte[4];
  7. Arr[0] = (byte) ((int) (ID >> 0*8) & 0xff);
  8. ARR[1] = (byte) ((int) (ID >> 1*8) & 0xff);
  9. ARR[2] = (byte) ((int) (ID >> 2*8) & 0xff);
  10. ARR[3] = (byte) ((int) (ID >> 3*8) & 0xff);
  11. For (int i = 0; i < arr.length; i++) {
  12. Arr[i] = (byte) ((int) (ID >> i*8) & 0xff);
  13. }
  14. return arr;
  15. }
  16. /* 
  17. * Convert byte array to int
  18. */
  19. public static int bytes2int (byte[]arr) {
  20. int rs0 = (int) ((arr[0]& 0xff) << 0*8);
  21. int rs1 = (int) ((arr[1]& 0xff) << 1*8);
  22. int rs2 = (int) ((arr[2]& 0xff) << 2*8);
  23. int RS3 = (int) ((arr[3]& 0xff) << 3*8);
  24. int result =0;
  25. For (int i = 0; i < arr.length; i++) {
  26. Result + = (int) ((arr[i]& 0xff) <<i*8);
  27. }
  28. return result;
  29. }
  30. public static void Main (string[] args) {
  31. byte[] arr = phone.int2bytes (8143);
  32. System.out.println (arr[0]+"," +arr[1]+"," +arr[2]+"," +arr[3]);
  33. System.out.println (Phone.bytes2int (arr));
  34. //string and character array
  35. String info="Good study, day up";
  36. byte[] Barr = Info.getbytes ();
  37. String des = new String (Barr);
  38. System.out.println (DES);
  39. }
  40. }

If it is wrong, please correct me!

Binary binary in Java

Related Article

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.