Common use of bit operations

All the numbers in the program are stored in binary form in the computer memory. Bit operation is to directly operate on the binary bits of the integer in the memory, and its efficiency will be higher than the arithmetic operation efficiency of the programming language.

Common bit operations

The common bitwise operators are shown in the table, mainly including & (and), | (or), ^ (exclusive OR), << (left shift) and >> (right shift).

& Operation

The & operation is called an AND operation. The result of the operation for the column that is the same as 1 is 1, and the rest is 0.

25&15 

25 binary is expressed as 11001, 15 binary is expressed as 1111, bitwise & operation, the result is 1 for positions that are the same as 1, and the rest are 0, the final result is 01001, and 01001 decimal is 9.

    System.out.println(Integer.toBinaryString(25));
    System.out.println(Integer.toBinaryString(15));
	System.out.println(25&15);

Execute the code, you can get the result 9

Purpose, by using the feature of & operation that the same as 1 to 1, you can judge the odd and even numbers, the last digit of any odd number is 1, and the even number is 0. Any number num: num&1 = 1, it means that the last digit of num is 1, which is an odd number; if num&0 = 0, it means that the last digit of num is 0, which is an even number.

	System.out.println(25&1);
	System.out.println(24&0);

The result of the operation is 1 and 0

|Operation

| Operation, called OR operation, the result of the operation is 0 for the same value of 0, and the rest are 1

25|15

25 is expressed in binary as 11001, 15 in binary is expressed as 1111, bitwise operation, the result is 0 for positions that are the same as 0, and the rest are all 1, the final result is 11111, and 11111 is 31 in decimal.

	System.out.println(25|15);

The execution result is 31

^Operation

The ^ operation is called an XOR operation. If the values ​​of a and b are not the same, the XOR result is 1. If the two values ​​of a and b are the same, the XOR result is 0.

25^15

25 binary is expressed as 11001, 15 binary is expressed as 1111, bit ^ operation, take 1 for different bits, and the rest are 0, the final result is 10110, and 10110 is 22 in decimal.

XOR operation jdk source code, it is used when HashMap takes the hash value

Shift the hashCode value to the right by 16 bits without sign, and then do an XOR operation with the hashCode value itself to obtain a more accurate hash value.

<<Operation

a << b means that after converting a to binary, shift to the left by b bits (add b 0s at the end), which is equivalent to multiplying by 2 to the power of b

25 << 2, 25 is expressed in binary as 11001. After shifting to the left twice, it is equivalent to complementing two 0s to the right of the binary number, which is 1100100, which is converted to 100

   System.out.println(25<<2);

Based on the efficiency of shift operations, there are many places where shift operations are used in Java code. HashMap source code

The default capacity 1<<4 is the initial capacity of 16; the maximum hashmap capacity is 1<<30.

    /**
     * The default initial capacity - MUST be a power of two.
     */
    static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16

    /**
     * The maximum capacity, used if a higher value is implicitly specified
     * by either of the constructors with arguments.
     * MUST be a power of two <= 1<<30.
     */
    static final int MAXIMUM_CAPACITY = 1 << 30;

When the hashMap is expanded, the shift operation is used to double the capacity

>>Calculation

a >> b means that after converting a to binary, shift b bits to the right (remove the last b bits), which is equivalent to dividing by 2 to the power of b.

25>>2, the binary representation of 25 is 11001, shifted to the right by two bits, which is equivalent to removing the last two bits, the result is 110, and the binary number 110 is converted to decimal number 6.

Shift right is also used in jdk source code, such as the expansion part of ArrayList

When ArrayList expands, the new capacity newCapacity = oldCapacity + (oldCapacity >> 1), oldCapacity >> 1 means that the old capacity is shifted right by 1 bit, and the old capacity is divided by 2, and newCapacity is 1.5 oldCapacity.

Guess you like

Origin blog.csdn.net/magi1201/article/details/115190873