Copyright Notice 1: This article is the blogger’s original article, please attach the original source link and this statement. Copyright notice 2: All works on this website will be updated in time, welcome to read after comments, for the improvement of works. Copyright statement three: do not comply with this statement or other illegal, malicious use of the content of this network, reserve the right to investigate its legal responsibility.

In Java, the “and operation &” rule: if both values are 1, the value is 1; otherwise, the value is 0

  • That is, the result is 1 only when both digits are 1. Otherwise, the result is 0
Such as:
    public static void main(String args[]{

        System.out.println( 7 & 9);



        / *

* 7 binary

* 7/2 = 3... 1

* l = 1... 1

* 1/2 = 0... 1

* until the quotient is 0, and the remainder is 111

* So this is 111

* /




        / *

* 9 binary

* 9/2 = 4... 1

* 4/2 = 2... 0

* 2/2 = 1... 0

* 1/2 = 0... 1

* So the number is 1001

* /




        / *

* 7 binary 0111

* 9 Binary 1001

* -- -- -- -- -- -- -- -- -- -- -- --

* 0001 = = 1

* /
        

    }

Copy the code

In Java | operation on “or” rule: there is a is 1, it is 1

  • That is, as long as one of the two objects in the operation is 1, its value is 1.
Such as:
    public static void main(String args[]){     

        System.out.println(7 | 9);  



        / *

* 7 binary 0111

* 9 Binary 1001

* -- -- -- -- -- -- -- -- -- -- -

* 1111 = = 15

* * /
    

    }

Copy the code

In Java, xOR ^ rule: if neither of them is the same, it is 1

  • That is, if two corresponding bits of the two objects in the operation are “different” (different values), the result of that bit is 1, otherwise it is 0.
Such as:
    public static void main(String args[]){

        System.out.println( 7 ^ 9);

        / *

* 7 binary 0111

* 9 Binary 1001

* -- -- -- -- -- -- -- -- -- -- -- --

* 1110 = = 14

* * /


    }   

Copy the code

Reverse operation ~ rule in Java: reverse operation by bit

  • That is, a binary number is reversed by bits, that is, 1 becomes 0, and 0 becomes 1.

  • The principle of the bitwise invert operator “~” is to invert the complement in memory bitwise (including the sign bit).

    1. Binary numbers are stored in memory as complement.
    2. The first part of the complement is a sign bit, where 0 means the number is positive and 1 means the number is negative.
    3. The complement and inverse of a positive number are themselves.
    4. The inverse of a negative number is: the sign bit is 1, the rest bits are reversed, but the last bit is not incremented by 1.
    5. The complement of a negative number is: the sign bit remains the same, the other bits are reversed, and the last bit is added by 1.
    6. All negation operations, addition and subtraction operations are performed in significant bits.
    • For example, a positive number

    • The positive number 9 (binary: 1001) is stored in memory as 01001 and must be filled with a sign bit (the leading digit 0 is a sign bit).

    • Switch to binary: 0 1001

    • Calculate the complement: 0 1001

    • Bitwise inverse: 1 0110 (becomes a complement, which obviously becomes a negative complement because the sign bit is 1)

    • The complement minus 1:1 0101

    • Take the inverse: 1, 1010

    • A sign bit of 1 is a negative number, that is, -10

    • For example: negative numbers

    • A negative number -1 (0001 in binary) is stored in memory as 10001 and must be filled with a sign bit (the leading digit 1 is a sign bit).

    • The inverse of -1 is 11110

    • The complement of -1 is 11111.

    • If ~-1, take the inverse of 00000

    • ~-1 The result is 0

Case 1:
package test2;



public class CeshiQuFan {



    public static void main(String args[]){

            System.out.println(~7);/ / positive



        / *

* 7 binary 0000 0000 0000 0000 0000 0000 0111

* 0000 0000 0000 0000 0000 0000 0000 0000

* 0000 0000 0000 0000 0000 0000 0000

* 1000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000

* So the value of ~7 is :-8

* /


    }

}    

Copy the code
Example 2:
package test2;



public class CeshiQuFan {



    public static void main(String args[]){

            System.out.println(~- 1);/ / negative



  / *

* -1 binary 1000 0000 0000 0000 0000 0000 0000 0001

* 1000 0000 0000 0000 0000 0000 0000 0000 1110

* 1000 0000 0000 0000 0000 0000 0000 0000 1111

0000 0000 0000 0000 0000 0000 0000 0000 0000

* So the value of ~-1 is :0

* /




    }

}

Copy the code

For more information, please refer to the common notation table