Home > Java > javaTutorial > body text

What is the scope of application of Java bitwise operators?

WBOY
Release: 2023-04-23 17:55:15
forward
1528 people have browsed it

Explanation

The main bitwise operators in Java are: bitwise AND&, bitwise OR|, bitwise NOT~, bitwise XOR^.

When using it, you need to convert the operands into binary before performing the operation. If it is a negative number, use the complement representation.

Application Scope

1. Java defines bit operators, which are applied to 5 data types.

2. They are integer type (int), long integer (long), short integer (short), character type (char), and byte type (byte).

Example

public static void main(String[] args) {
     int a = 60; /* 60 = 0011 1100 */
     int b = 13; /* 13 = 0000 1101 */
     int c = 0;
     c = a & b;       /* 12 = 0000 1100 */
     System.out.println("a & b = " + c );
 
     c = a | b;       /* 61 = 0011 1101 */
     System.out.println("a | b = " + c );
 
     c = a ^ b;       /* 49 = 0011 0001 */
     System.out.println("a ^ b = " + c );
 
     c = ~a;          /*-61 = 1100 0011 */
     System.out.println("~a = " + c );
 
     c = a << 2;     /* 240 = 1111 0000 */
     System.out.println("a << 2 = " + c );
 
     c = a >> 2;     /* 15 = 1111 */
     System.out.println("a >> 2  = " + c );
  
     c = a >>> 2;     /* 15 = 0000 1111 */
     System.out.println("a >>> 2 = " + c );
  }
Copy after login

The above is the detailed content of What is the scope of application of Java bitwise operators?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template