(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
- Public static void Main (string[] args) {
- /*
- * Decimal conversion to other binary
- */
- //Binary
- System.out.println (Integer.tobinarystring (0));
- //16 in the system
- System.out.println (integer.tohexstring (112));
- //8 in the system
- System.out.println (integer.tooctalstring (112));
- /*
- * Other binary conversions to be decimal
- */
- //Binary
- System.out.println (Integer.parseint ("1110000", 2));
- //8 in the system
- System.out.println (Integer.parseint (" 8)");
- //16 in the system
- System.out.println (Integer.parseint ("A1", 16));
- }
[Java]View PlainCopy
- Public class Phone {
- /*
- * Convert int to byte array
- */
- public static byte[] Int2bytes (int id) {
- byte[] arr = new byte[4];
- Arr[0] = (byte) ((int) (ID >> 0*8) & 0xff);
- ARR[1] = (byte) ((int) (ID >> 1*8) & 0xff);
- ARR[2] = (byte) ((int) (ID >> 2*8) & 0xff);
- ARR[3] = (byte) ((int) (ID >> 3*8) & 0xff);
- For (int i = 0; i < arr.length; i++) {
- Arr[i] = (byte) ((int) (ID >> i*8) & 0xff);
- }
- return arr;
- }
- /*
- * Convert byte array to int
- */
- public static int bytes2int (byte[]arr) {
- int rs0 = (int) ((arr[0]& 0xff) << 0*8);
- int rs1 = (int) ((arr[1]& 0xff) << 1*8);
- int rs2 = (int) ((arr[2]& 0xff) << 2*8);
- int RS3 = (int) ((arr[3]& 0xff) << 3*8);
- int result =0;
- For (int i = 0; i < arr.length; i++) {
- Result + = (int) ((arr[i]& 0xff) <<i*8);
- }
- return result;
- }
- public static void Main (string[] args) {
- byte[] arr = phone.int2bytes (8143);
- System.out.println (arr[0]+"," +arr[1]+"," +arr[2]+"," +arr[3]);
- System.out.println (Phone.bytes2int (arr));
- //string and character array
- String info="Good study, day up";
- byte[] Barr = Info.getbytes ();
- String des = new String (Barr);
- System.out.println (DES);
- }
- }
If it is wrong, please correct me!
Binary binary in Java