Home  >  Article  >  Java  >  Introduction to Java Bit Operations (Code Example)

Introduction to Java Bit Operations (Code Example)

不言
不言forward
2019-03-07 17:38:162486browse

This article brings you an introduction to Java bit operations (code examples). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

We all know that in the computer world, no matter how complex or beautiful the program is, it will become 0 and 1 in the end. That is what we often call: binary. I believe everyone is familiar with binary. What is different from the real world is that in the real world, we usually use decimal to express it, that is, when encountering ten to one, these are all familiar to us. At this point, we will find clues that the measurement units of decimal in the real world and binary in computers are different. So how to convert between them? This involves some relatively basic computer knowledge. Not discussed in this article (if you are interested, you can talk about it next time). Well, back to today’s topic, let’s talk about bit operations. What kind of concept is this? We have been exposed to the operations of addition, subtraction, multiplication and division in the real world since childhood, which are operations in the decimal system. What we are going to talk about today is: some common operations in binary bits. For example: & (bit AND), | (bit OR), ^ (exclusive OR), << (left shift), >> (right shift), etc.

True and False
Before using operators, we need to talk about true and false. In Java, we all know that the true value represents true and the false value represents false. In fact, in computers, 1 is usually used to represent true and 0 to represent false. Students who have used Json should know that the boolean type in Java can also be deserialized into true using 1, and 0 can be deserialized into false.

& (bit AND)
Before talking about bit AND, let’s first talk about the familiar && logical AND operation. To put it simply: A&&B means: it is true when A and B are true at the same time, otherwise it is false. Some people also call it: "Once a lie is false, it must be false."

Now let’s look at the position and. First, let’s look at a program:

@Test

public void testBit(){
    int a = 8;
    int b = 9;
    System.out.println("a binary: "+Integer.toBinaryString(a));
    System.out.println("b binary: "+Integer.toBinaryString(b));
    System.out.println("a & b binary: "+Integer.toBinaryString(a&b));
    System.out.println("a & b result: "+(a&b));
}

Before looking at the explanation, let’s guess what the result is?

Code explanation:

Bit AND: We understand it literally, it is also an AND operation of binary bits.

The decimal notation of the number 8 is: 1000.

The decimal notation of the number 9 is: 1001.

Let’s perform the location operation again:

is as follows:

8:1000
9:1001
&
8 1000

The leftmost 1&1 = 1, the middle 0&0 = 0, and the rightmost 0&1 = 0.

The binary result is: 1000, which is 8 after conversion to decimal.

The program running result is as follows:

a binary: 1000
b binary: 1001
a & b binary: 1000
a & b result: 8

The results are as expected.

| (bit OR)

The above mentioned & (bit AND) operation, now let’s take a look at the bit OR operation and continue to use the above example: as shown below :

@Test

public void testBit(){
    int a = 8;
    int b = 9;
    System.out.println("a binary: "+Integer.toBinaryString(a));
    System.out.println("b binary: "+Integer.toBinaryString(b));
    System.out.println("a & b binary: "+Integer.toBinaryString(a|b));
    System.out.println("a & b result: "+(a|b));
}

Look at the binary again:

8:1000
9:1001
|
9 1001
Leftmost The 1|1 = 1, the middle 0|0 = 0, and the rightmost 0|1 = 1.

The result in binary is: 1001 and the corresponding decimal is 9.

The operation result is as follows:

a binary: 1000
b binary: 1001
a & b binary: 1001
a & b result: 9

^(XOR)

This operator is more interesting. X literally means: different. The same goes for placement operations. Continue using the above example:

@Test

public void testBit(){
    int a = 8;
    int b = 9;
    System.out.println("a binary: "+Integer.toBinaryString(a));
    System.out.println("b binary: "+Integer.toBinaryString(b));
    System.out.println("a & b binary: "+Integer.toBinaryString(a^b));
    System.out.println("a & b result: "+(a^b));
}

Continue to look at the binary:

8:1000
9:1001
^
1 0001

will be false if the bits are the same, and true if they are different. The 1=1 on the left is the same as false, which is 0. The 0=0 in the middle is also false and 0. The rightmost 0 is not equal to 1, which is true. The result is 1.

<<(left shift)

In the real world, we often use multiplication. << means a displacement operation in binary, with the low bits filled with 0s. For example: 8<<1.

@Test

public void testCode(){
    int a =8;
    System.out.println("a toBinaryString: "+Integer.toBinaryString(a));
    System.out.println("a<<1 toBinaryString: "+Integer.toBinaryString(a<<1));
    System.out.println("result: "+(a<<1));

The binary is as follows:

8 1000
8<<1
16 10000

The result is: 2^ 4 = 16. << The a on the left represents the base, and the 1 on the right represents the number of bits that need to be shifted. Whichever way the arrow points, that's where the displacement will go. Program running result:

a toBiryString: 1000
a<<1 toBinaryString: 10000
result: 16

>> Shift right

(right shift) is the opposite of left shift <<, the high bits are filled with 0. Continuing with the above example:

@Test

public void testCode(){
    int a =8;
    System.out.println("a toBinaryString: "+Integer.toBinaryString(a));
    System.out.println("1>>a toBinaryString: "+Integer.toBinaryString(a>>1));
    System.out.println("result: "+(a>>1)
}

Binary:

8 : 1000
8>>1
4 : 0100

Run result:

a toBinaryString: 1000
a>>1 toBinaryString: 100
result: 4

In fact, there is another formula that is easier to remember. :

a>>n means: a / (2^n) power. (Rounding)

a<

Now let’s do a quick calculation:

When a = 13, n = 2. 13<<2 is equal to 13* 4 = 52. 13/4 = 3.

(If there is any error in the above speed algorithm, please feel free to slap me in the face!!!)


Bit shift operations are very common in our source code and common algorithms. It is also necessary for a Java programmer to master bit operations. This is very helpful for our algorithm and source code understanding!


The above is the detailed content of Introduction to Java Bit Operations (Code Example). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete