Bit operation || XOR operation || Shift operation JAVA

The sum without carry is a^b, and the sum with carry is a&b a+b=a^b+(a&b)< <1


1. Bit operation

There are 4 bit operations in Java, and their operation rules are as follows:

(1) Bitwise AND (&): if all two bits are 1, the result is 1; otherwise, it is 0;

(2) Bitwise OR ( | ): if all two bits are 0, the result is 0; otherwise, it is 1;

(3) Bitwise inversion ( ~ ): 0 becomes 1, 1 becomes 0;

(4) Bitwise exclusive OR (^): two bits, if they are the same, the result is 0; if they are different, the result is 1

Notice:

(1) The sign bit (highest bit) in the bit operation also changes;

(2) Bitwise operators are similar to logical operators (logical and &&, logical or ||, logical not!), but logical operators can only operate on Boolean variables [that is, both sides are Boolean values]


2. Application of bit operation

(1)~5=?

        >>>>>5's complement: 00000000 00000000 00000000 00000 101

        >>>>>> ~5    :11111111 11111111 11111111 11111010

                        Note: Because the high digit is 1 (negative number), the complement code must be converted to the original code;

                                  If the high digit is 0 (integer), there is no need to convert the complement to the original code,

                                        Because the original code, inverse code, and complement code of positive numbers are the same.

           >>>>> The complement of the negative number is converted to one's complement: 11111111 1111111 1111111 11111 001

            >>>>>The inverse code of the negative number is converted to the original code: 10000000 00000000 00000000 00000 110

            >>>>> Convert the original binary code to decimal: 0 * 2^0 + 1 * 2^1 + 1 * 2^2 = 0+2+4 = -6 (the highest bit is 1, so it is a negative number


3. Shift operation

There are three shift operators in Java

(1) Arithmetic right shift (>>): the low bit overflows, the sign bit remains unchanged, and the high bit of the overflow is filled with the sign bit;

        Right shift rule: the sign bit remains unchanged, and the sign bit is added to the left! [Add 0 for positive numbers and 1 for negative numbers]

        Shifting right by one bit is equivalent to dividing by 2, and shifting right by n bits is equivalent to dividing by 2 raised to the nth power.

     11 >> 2 (11 is int type)
      1) The binary form of 11 is: 0000 0000 0000 0000 0000 0000 0000 1011
      2) Shift out the last two digits of the low order, because the number is a positive number, so add zeros in the high order.
      3) The final result is 0000 0000 0000 0000 0000 0000 0000 0010.
         Converting to decimal is 3.

(2) Algorithm left shift (<<): the sign bit remains unchanged, and the low bit is filled with 0;

        Left shift rule: discard the highest bit, 0 complement the lowest bit!

        In the absence of numeric overflow, for both positive and negative numbers, a left shift by n bits is equivalent to multiplying by 2 to the nth power.

    For example: 3 <<2 (3 is int type)
    1) Convert 3 to binary number 0000 0000 0000 0000 0000 0000 0000 0011,
    2) Shift out the two zeros of the high order (left side) of the number, and the other numbers are towards Shift left by 2 bits,
    3) Fill the two vacancies in the lower (right) position with zeros. The final result is 0000 0000 0000 0000 0000 0000 0000 1100, which
       translates to 12 in decimal.

(3) Logical right shift (>>>): The low bit overflows, and the high bit is filled with 0; [ Note: the sign bit (highest bit) in the logical right shift also changes accordingly ]

        The unsigned right shift operator >>> only makes sense for 32-bit and 64-bit values.


Remember! Remember! Remember! Bit operations and shift operations,

All are operated in two's complement ! ! !










Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324782389&siteId=291194637