This article originated from the fact that when I learned the principle of HashMap, I saw the hashmap expansion mechanism and found that I almost forgot binary operation. Probably most people are similar to me. It is good that you have learned the technology that is not used in daily work, but you will forget it quickly. This requires us to constantly review the techniques we have learned (here the manual dog stick hits ourselves).

1, decimal to binary

Divide a decimal number by 2 until the quotient is 0 or 1, record the remainder of each step, and then reverse the remainder to the corresponding binary

Example: decimal 8 to binary

The remainder of 8 divided by 2 is 4, the remainder of 4 divided by 2 is 2, the remainder of 2 divided by 2 is 1, and the remainder of 1 is 0001.

In the computer, the length of the number is fixed, such as 8 bits, 16 bits, 32 bits. If the number is less than 8 bits, the number will be filled up in the high digit (why the high digit, because filling 0 in the high digit does not affect the number).

Java code implementation:

public class mapHashCodeTest {
     public static void main(String[] args) {
         String str = toBinary(8);
         System.out.println(str);
     }
     static String toBinary(int num) {
         String str = "";
         while(num ! = 0) { str = num % 2 + str; num = num / 2; }returnstr; }}Copy the code

2. Convert binary to decimal

Binary 1000, to decimal, just take the corresponding 0 and 1 digit and multiply the power of 2.

8 = 0 * 2 + 0 (0 power) * 2 (1 power) + 0 * 2 (power) 2 + 0 * 2 (three times power)

The integer.parseint () implementation can be called directly, for example:

System.out.println(Integer.parseInt("1000", 2));Copy the code

The result is: 8

3. Bit and operation (&)

If it is a binary number, the operation can be performed directly. If it is a decimal or hexadecimal number, the operation needs to be converted to binary. The operation rule is: starting from the ones place, if both numbers are 1, it is 1; otherwise, it is 0

Such as:

The binary of 8 is 1000, the binary of 9 is 1001, and the sum is 1000

Code implementation:

int a = 8; int b = 9; System.out.println(a&b); // The result is 8Copy the code

The and operation is used in many scenarios, such as IP addresses and subnet masks.

4, or (|)

Both numbers are converted to binary numbers. The operation rules are as follows: starting from the units digit, if one of the two numbers is 1, it is 1; otherwise, it is 0.

Such as:

The binary of 8 is 1000, the binary of 9 is 1001, and the bit or operation is 1001

Code implementation:

int a = 8; int b = 9; System.out.println(a|b); // The result is 9Copy the code

5, bit xor operation (^)

The algorithm is: two numbers are converted to binary, and then compared from the higher order, 0 if they are the same, 1 if they are different.

Such as:

The binary of 8 is 1000, the binary of 9 is 1001, and the xOR bit is 0001

Code implementation:

int a = 8; int b = 9; System.out.println(a^b); // The result is 1Copy the code

It’s a little tricky, so please watch carefully

6. Bit non-operator (~)

To convert a number to a binary number, the operation rules are as follows: from the one digit, the digit 1 is 0, and the digit 0 is 1.

All data in Java is represented in the form of complement. Unless otherwise specified, the default data type in Java is int. The length of an int data type is 8 bits, and one bit is 4 bytes, which is 32 bytes, or 32 bits.

Such as:

The binary of eight is 1000,

0000 0000 0000 0000 1000

The inverse is 1111 1111 1111 1111 1111 1111 1111 1111 0111

Since the high order is 1, the source code is negative, and the complement of negative is the reverse of the source code of its absolute value, and the end is added with 1

So we can restore the complement of this binary number,

Subtract 1 from the end of the code: 1111 1111 1111 1111 1111 1111 1111 0110

0000 0000 0000 0000 0000 0000 0000 0000 0000 0000

If you subtract the complement bits, you get 1001, so 8 bits are negative 9

Code implementation:

int a = 8; System.out.println(~a); // The result is -9Copy the code

If you enjoyed this article,

Please scan the QR code for more articles