Java Basics: Bitwise Operators

The previous article covered shift operators, and this article covers bitwise operators.

overview

Bitwise operators work on the bits of a binary number or input value, and we can apply these to integer types: long, int, short, char, and byte.

calculation process:

  • First, the operands are converted to their binary representation
  • Next, apply the operator to each binary number and compute the result
  • Finally, convert the result back to its decimal representation

Java provides the following bitwise operators: Bitwise operators are used on integer values ​​and &|^ acts as a conditional operator when used with Boolean values.

operator symbol explain
& x & y If x and y are integer types, a bitwise AND operation is performed.
| × | y If x and y are integer types, a bitwise OR operation is performed.
^ x^y If x and y are integer types, a bitwise exclusive-or operation is performed.
~ ~x If x is an integer type, performs a bitwise NOT operation (inverts each bit).

& represents the AND operation in the bitwise operator, and the AND operation (logical product) is an operation method that returns 1 when the two bits to be operated are both 1.

The & operator compares each binary digit of two integers and returns 1 if both are 1, and 0 otherwise.

This is similar to the && operator with boolean values, the result of the && operation is true when both boolean values ​​are true.

x y result
0 0 0
0 1 0
1 0 0
1 1 1

Binary 1000 (decimal 8) AND binary 1010 (decimal 10) AND is binary 1000 (decimal 8).

Code demo:

package com.test.javaroads.operator;

/**
 * @author: javaroads
 * @date: 2022/12/6 18:12
 * @description:
 */
public class Eleven {

    public static void main(String[] args) {

        int x = 8;
        int y = 10;

        System.out.println("x & y的值为" + (x & y));
    }
}

Results of the:

|

|Indicates the OR operation in the bit operation, and the OR operation (logical sum) is an operation method that returns 1 when one of the bits to be operated is 1.

The | operator compares each binary digit of two integers and returns 1 if one of them is 1.

This is similar to the || logical operator used with boolean values, when comparing two boolean values, if one of them is true, the result is true, likewise, when one of them is 1, the output is 1.

x y result
0 0 0
0 1 1
1 0 1
1 1 1

The OR operation of binary 1000 (decimal 8) with binary 1010 (decimal 10) is binary 1010 (decimal 10).

Code demo:

package com.test.javaroads.operator;

/**
 * @author: javaroads
 * @date: 2022/12/6 18:14
 * @description:
 */
public class Twelve {

    public static void main(String[] args) {

        int x = 8;
        int y = 10;

        System.out.println("x | y的值为" + (x | y));
    }
}

Results of the:

^

^Indicates the XOR operation in the bit operation. The XOR operation (XOR) is an operation method that returns 1 when the bits to be operated are different.

The XOR operator compares each binary digit of two integers and returns 1 if the two bits compared are different. This means that if the bits of both integers are both 1 or 0, the result will be 0; otherwise, the result will be 1.

x y result
0 0 0
0 1 1
1 0 1
1 1 0

Binary 1000 (decimal 8) is XORed with binary 1010 (decimal 10) to get binary 0010 (decimal 2).

Code demo:

package com.test.javaroads.operator;

/**
 * @author: wangrui
 * @date: 2022/12/6 18:16
 * @description:
 */
public class Thirteen {

    public static void main(String[] args) {

        int x = 8;
        int y = 10;

        System.out.println("x ^ y的值为" + (x ^ y));
    }
}

Results of the:

~

~ represents the NOT operation in the bit operation, and the NOT operation is an operation method that inverts each bit in the operation.

The ~ operator changes each binary bit of an integer, meaning all 0's become 1's and all 1's become 0's, the operator works similarly for boolean values: it inverts a boolean value from true to false and vice versa .

The NOT of 00001000 in binary (8 in decimal) is 11110111 (-9 in decimal).

Code demo:

package com.test.javaroads.operator;

/**
 * @author: javaroads
 * @date: 2022/12/6 18:16
 * @description:
 */
public class Thirteen {

    public static void main(String[] args) {

        int x = 8;

        System.out.println("~x的值为" + (~x));
    }
}

Results of the:

Guess you like

Origin blog.csdn.net/weixin_43025343/article/details/132236333