Java bit operator detailed example

Source: Internet
Author: User
Tags bitwise operators logical operators

Java bitwise operators in detail instances--with (&), non (~), or (|), XOR (^), right Shift (>>), left (<<), unsigned Right shift (>>>)

Bitwise operators are primarily for binary,

It includes: And, non, or, XOR, shift right, shift left, unsigned right shift, and so on. "

Looks a bit like a logical operator on the surface,

But the logical operators are logical operations on two relational operators,

The bitwise operators are mainly for the bits of the two binary digits for the logical operation.

Each bit operator is described in detail below.

1. With operator
And the operator is denoted by the symbol "&", which uses the following rules:
The two operands have a bit of 1 and the result is 1, otherwise the result is 0, for example, the following program segment.
public class Demo1
{
public static void Main (string[] args)
{
int a=129;
int b=128;
System.out.println ("A and B" with the result is: "+ (a&b));
}
}
Run results
A and B with the result is: 128
The following analysis of this program:
The value of "a" is 129, converted to binary is 10000001, and the value of "B" is 128, converted to binary is 10000000. According to the operator's operation Law, only two bits are 1, the result is 1, you can know the result is 10000000, that is, 128.


2. Or operator
Or operator with the symbol "|" Shows that its operation law is as follows:
Two bits as long as there is a 1, then the result is 1, otherwise 0, see a simple example below.
public class Demo2
{
public static void Main (string[] args)
{
int a=129;
int b=128;
System.out.println ("A and B" or the result is: "+ (a|b));
}
}
Run results
A and B or the result is: 129
The following analysis of this program segment:
The value of a is 129, the conversion to binary is 10000001, and the value of B is 128, converted to binary is 10000000, according to the operator's operation Law, only two bits have a 1, the result is 1, you can know the result is 10000001, that is 129.


3. Non-operator
The non-operator is denoted by the symbol "~", and its Operation law is as follows:

If the bit is 0, the result is 1, if the bit is 1, the result is 0, see a simple example below.
public class Demo3
{
public static void Main (string[] args)
{
int a=2;
SYSTEM.OUT.PRINTLN ("A non-result is:" + (~a));
}
}

Run results
The result of non-A is: 3

Analysis of the above program segment: 2 of the binary is 0000 0010, after the non-operation 1111 1101, so the result is-3


4. Xor operator
The XOR operator is denoted by the symbol "^", and its Operation law is:
The two operand bits, the same result is 0, and the result is 1. Let's look at a simple example.
public class Demo4
{
public static void Main (string[] args)
{
int a=15;
int b=2;
System.out.println ("A and B xor the result is:" + (a^b));
}
}
Run results
The result of A and B xor is: 13
Analysis of the above program segment: The value of a is 15, converted to binary 1111, and the value of B is 2, converted to binary 0010, according to the different or the Operation law, can be obtained the result is 1101 that 13

5.>> displacement

Right SHIFT operation Symbol

The number of digits to the right is discarded, and the left side complements

public class Demo5
{
public static void Main (string[] args)
{
int a=7;
System.out.println (integer.tobinarystring (a));
SYSTEM.OUT.PRINTLN (the result of a right shift is: "+ (a>>1));
}
}

Run results
A the result of the right shift is: 3
Analysis of the above program segment: The value of A is 0111, the right to move 1 bits, the result is 0011 that is 3

6.<< displacement

Left shift operation Symbol

The number of bits left to move is discarded, and the right side is

public class Demo6
{
public static void Main (string[] args)
{
int a=-7;
System.out.println (Integer.tobinarystring (a));
System.out.println ("A right shift result is:" + (a<<1));
}
}

Run results
The result of a left shift is: 14
Analyze the above program segment: The value of A is 1111 1001, shift left 1 bits, the result is 1111 0010 that is 14

7.>>> displacement

Unsigned Right Shift

The number of digits to the right is discarded, 0 on the left

public class Demo7
{
public static void Main (string[] args)
{
int a = 7;

int B =-7;


System.out.println (Integer.tobinarystring (a));

System.out.println (integer.tobinarystring (b));
System.out.println ("A unsigned right shift result is:" + (a>>>1));

System.out.println ("b unsigned Right shift result is:" + (b>>>1));
}
}

Run results
A unsigned right shift result is : 3

b The result of the unsigned Right shift is: 2147483644
Analyze the above program segment:

The value of a is 0000 0111, the right shift 1 bits, the result is 0000 0011 that is 3

The value of B is 1111 1111 1111 1111 1111 1111 1111 1001, shift right 1 bit,

The result is 0111 1111 1111 1111 1111 1111 1111 1100 that is 2147483644

Java bit operator detailed example

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.