Java bitwise operators bitwise and, bitwise OR, bitnon, bitxor, shift right, shift left

Keywords: Java

Today, I have encountered bit operators in the code, because I have forgotten them, so I will review them. Java bit operators include: bitwise and '&', bitwise or '|, bitwise non' ~ ', bitwise exclusive or' ^ ', shift right' > ', shift left' < and shift right '> >'.

A bit operation is an operation in binary units. Its operands and operation results are integer values. Some binary knowledge is needed in the operation. Let's review it. Here is an article about binary, source code, inverse code and complement code: https://blog.csdn.net/vickyway/article/details/48788769 . It's more detailed. I won't go over it here.

The following code contains the specific calculation process (note that the int type in Java is 32bit)

//java contains operators = bitwise and '&', bitwise or '|, bitwise not' ~ ', bitwise exclusive or' ^ ', shift right' > ', shift left' < ', shift right' >
        //The numbers involved in bit operation are all binary complements, which are bitwise and, or, XOR. The inverse and complement of positive numbers are themselves

        //1. '&' bit and operator (if both operands are 1, the result is 1, otherwise the result is 0)
        System.out.print("1&-3 = ");
        System.out.println(1&-3);
        /*Calculation process (using complement)
             1:    00000000 00000000 00000000 00000001
            -3:    11111111 11111111 11111111 11111101
            Result: 00000000 00000000 00000000 01 (the result is also a complement, because it is a positive number, the original code is the same) 1
         */

        //2. '|' bit or operator (if one of the two bits is 1, the result will be 1, otherwise it will be 0)
        System.out.print("1|-3 = ");
        System.out.println(1|-3);
        /*Calculation process (using complement)
             1:    00000000 00000000 00000000 00000001
            -3:    11111111 11111111 11111111 11111101
            Results: 11111111 11111111 11111111 11111111101 (the result is also a complement, which is converted to the original code 100000000000000000000 00000011) - 3
         */

        //3. '~' bit non operator (if bit is 0, result is 1, if bit is 1, result is 0)
        System.out.print("~-3 = ");
        System.out.println(~-3);
        /*Calculation process (using complement)
            -3:    11111111 11111111 11111111 11111101
            Result: 00000000 00000000 00000000 10 (positive number, same original code) 2
         */

        //4. '^' exclusive or operator (if two operands are the same, the result is 0; if they are different, the result is 1)
        System.out.print("1^-3 = ");
        System.out.println(1^-3);
        /*Calculation process (using complement)
             1:    00000000 00000000 00000000 00000001
            -3:    11111111 11111111 11111111 11111101
            Results: 11111111 11111111 11111111 11111111100 (original code: 10000000000000000000000 0100) - 4
         */

        //5. '<' shift left operator (shift left operator, move the object to the left of the operator to the left of the number of digits specified to the right of the operator (fill 0 in the low order))
        System.out.print("-3<<2 = ");
        System.out.println(-3<<2);
        /*Calculation process (using complement)
            -3:    11111111 11111111 11111111 11111101
            Results: 11111111 11111111 11111111 11110100 (original code: 1000000000000000000000000000011100) - 12
         */

        //6. '>' shift right operator ("signed" shift right operator, move the object to the left of the operator to the right of the digit specified on the right of the operator. If the value is positive, fill 0 in the high position, if the value is negative, fill 1 in the high position.)
        System.out.print("-3>>2 = ");
        System.out.println(-3>>2);
        /*Calculation process (using complement)
            -3:    11111111 11111111 11111111 11111101
            Result: 11111111 11111111 11111111 11111111 (original code: 100000000000000 00000000 00000001) - 1
         */

        //7. '> >' shift right operator ("unsigned" shift right operator, move the object to the left of the operator to the right of the digit specified on the right of the operator. No matter the value is positive or negative, fill 0 in the high position.)
        System.out.print("-3>>>2 = ");
        System.out.println(-3>>>2);
        /*Calculation process (using complement)
            -3:    11111111 11111111 11111111 11111101
            Results: 00111111 11111111 11111111 11111111 2 ^ 30 power - 1 = 1073741823
         */

 

Posted by kalivos on Mon, 16 Dec 2019 07:54:46 -0800