The difficulty

primary

Learning time

30 minutes

Suits the crowd

Zero basis

Development of language

Java

The development environment

  • JDK v11
  • IntelliJ IDEA v2018.3

1. What is a bit operation?

All the numbers in the program are stored in binary form in computer memory.

Bitwise operations operate directly on the binary bits of an integer in memory.

For example, bitwise and is originally a logical operator, but integers can also be bitwise and between integers. For example, if the binary of 6 is 110 and the binary of 11 is 1011, then the result of 6&11 is 2, which is the logical operation performed on the corresponding bits of the binary (0 means False, 1 means True, and the empty bits are treated as 0).

2. By bit and &

Bitwise and is the result of a logical sum operation on the binary corresponding bits (0 means False, 1 means True, and empty bits are treated as 0).

For example, the binary of 6 is 110 and the binary of 11 is 1011. What is the result of 6&11? So let’s do that.

The first step is to list the binary values of 6 and 11:

Step 2, carry out bitwise and operation:

And then the third step is to work from low to high, right to left, 0 for false, 1 for true. The first counterpoint is 0 and 1, which is false and true, and we just learned in the last chapter about logic and false and true, which is false, so the first counterpoint is 0:

Step 4, further to the left, 1&1, which is true & true, is true:

Step 5, further left, 1&0, which is true & false, is false:

Step 6, further to the left, 0&1, which is false & true, is false:

In step 7, if you continue to work to the left, all zeros will be false & false, and the result will be false:

In step 8, the final result is “0000 0010”. This binary number is 2 in decimal, so 6&11 is 2.

Code representation:

Running results:

(3) or by location

Bitwise or the result of logic or operation on the corresponding bits of binary (0 means False, 1 means True, and empty bits are treated as 0).

Example: six binary is binary, is 1011, 110, is the result of the 6 | 11? So let’s do that.

The first step is to list the binary values of 6 and 11:

The second step is to perform bitwise or operations:

And then the third step is to work from low to high, right to left, 0 for false, 1 for true. First registration is 0 and 1, which is false | true, we have just learned in the previous chapter, or logical false | true, the result is true, so, the first registration result of 1:

The fourth step, left to continue operation, 1 | 1, which is true |, the result is true:

Fifth, to continue operations, left 1 | 0, is the true | false, the result is true:

Step 6, continue to left, 0 | 1, which is false | true, the result is true:

Step 7, to continue operations, left behind are 0, namely is false | false, the result is false:

Step 8, the final computation results for “0000 1111”, the binary number to a decimal number is 15, so results 6 | 11 to 15.

Code representation:

Running results:

4. Xor

Xor is the result of xOR operation of binary corresponding bits (0 means False, 1 means True, empty bits are treated as 0), and the result is 0 for the same para-bit; So it’s going to be 1.

For example, if binary of 6 is 110 and binary of 11 is 1011, what is the result of 6^11? So let’s do that.

The first step is to list the binary values of 6 and 11:

Step 2, perform xOR operation:

And then the third step is to work from low to high, right to left, 0 for false, 1 for true. The first pair is 0 and 1, which is 0 to the 1, which we just said is the same pair, which is 0; So it’s going to be 1. 0 and 1 are different, so the result is 1:

Step 4, continue the operation to the left, 1^1, the same counterpoint, the result is 0:

Step 5, continue to the left, 1^0, different, the result is 1:

Step 6, further to the left, 0^1, different, the result is 1:

Step 7, continue to the left, the following are 0, that is, they are the same, the result is 0:

In step 8, the final result is “0000 1101”. This binary number is 13 in decimal, so 6^11 is 13.

Code representation:

Running results:

Exclusive or features

I don’t know if you noticed:

6 ^ 11 = “0000 1101”;

“0000 1101” ^ 6 = 11;

“0000 1101” ^ 11 = 6;

Xor property: A ^b^b=a

Feature application: encryption. For example, the plaintext is A and the key is B. A ^b gets the ciphertext. If someone else doesn’t know the key B, they can’t figure out what plaintext A is, and if you want to figure out the ciphertext, you just put the ciphertext ^ B, and you get plaintext A.

5. The original code

The so-called source code is a binary fixed point representation, that is, the highest bit is a sign bit, “0” represents positive, “1” represents negative, and the remaining bits represent the size of the value.

+7 source code:

-7 source code:

Note:

The byte value ranges from -2 to 2 to the 7th power. A total of 256 numbers.

That is:

Unsigned bits 0 to 255 (because the computer is counting from 0 instead of 1)

The signed bit ranges from -128 to +127

6. Radix-minus-one complement

Inverse: Positive: The inverse of a positive number is the same as the original code. Negative number: The inverse of a negative number. The sign bit is “1” and the numeric part is reversed by bit.

Let’s look at an example:

+7 inverse:

The inverse of -7:

Now that you know the inverse code, let’s look at the original code:

The sign is 1, that’s a negative number; It’s all 0 bits, so that’s minus 0; But minus 0 doesn’t make any sense, so what is this original code? “1000 0000” indicates -128;

We know that the source code for +127 is:

We add +127 to 1:

+ + 1 = 127-128; Strange, isn’t it? Why is that?

The 8-bit binary inverse code ranges from -127 to +127.

What is the reverse of the original code “1000 0000”?

That’s weird. Isn’t that -127? How could it be minus 128? It turns out that the inverse of a negative number, you add 1 to the result, which is -128.

Code representation:

Running results:

7. Complement

In calculations, two complementary numbers are called “complements”.

Here are some examples:

Positive: The complement of a positive number is the same as the original.

+7’s complement:

Negative numbers: The complement of negative numbers is the sign bit “1”. Moreover, this “1” is both a sign bit and a numeric bit. The numeric part is reversed and then the last digit (the lowest digit) is added by 1. That is, “inverse +1”.

The complement of -7:

Calculate the inverse of -7 first:

In calculating the inverse +1:

8. The left < <

Used to shift all the bits of a number to the left.

For example, 1<<2, what is the result?

Step 1, binary representation of 1:

In the second step, move the binary two bits to the left:

After moving, we find that there are two more “00” bits on the left and two more “00” bits on the right. The missing two bits are filled with zeros:

So, the result is “0000 0100”, 1<<2=4.

Code representation:

Running results:

9. Moves to the right > >

To shift all bits of a number to the right. Low displacement out (abandoned), high vacancy to complement the sign bit, that is, positive complement 0, negative complement 1.

For example, 4>>1, what is the result?

Step 1: Binary representation of 4:

In the second step, move the binary bit to the right by 1 bit:

After we move it, we find that the right side has one more “0” bit, the left side has one less bit, integer adds 0, negative adds 1, because our 4 is an integer, so adds 0:

So, the result is “0000 0010”, 4>>1=2.

10. Unsigned right move >>>

Unsigned right shift to shift all bits of a number to the right. The low displacement is discarded and the high vacancy is filled with 0.

For example, 4>>>1, what is the result?

Step 1: Binary representation of 4:

In the second step, move the binary bit unsigned 1 bit to the right:

After the move, we find that the right side of the “0” 1 bit, the left side of the 1 bit, the high side of the 0:

So, the result is “0000 0010”, 4>>1=2.

Code representation:

Running results:

11. & =

The bitwise and operation is performed first and then the assignment is made.

Example: x=6; X squared is equal to 11, what is the value of x?

Binary of 6:

Binary of 11:

6 & 11:

X = 6 & 11; X = “0000 0010”, so the value of x is 2.

Code representation:

Running results:

12. | =

The bitwise or operation is performed before the assignment.

Example: x=6; X | = 11, the value of x?

Binary of 6:

Binary of 11:

6 | 11:

X = 6 | 11; X = “0000 1111”, so the value of x is 15.

Code representation:

Running results:

13. ^ =

Perform xOR first and then assign.

Example: x=6; X to the eleventh, what is the value of x?

Binary of 6:

Binary of 11:

6 ^ 11:

X = 6 ^ 11; X = “0000 1101”, so the value of x is 13.

Code representation:

Running results:

14. < < =

First shift to the left, then assign.

Example: x=1; X <<=2, what is the value of x?

Binary of 1:

1 < < 2:

X = 1 < < 2; X = “0000 0100”, so x is 4.

Code representation:

Running results:

15. > > =

We do the right shift, and then we assign.

Example: x=4; X >>=1, what is the value of x?

Binary of 4:

4 > > 1:

X = 4 > > 1; X = “0000 0010”, so the value of x is 2.

Code representation:

Running results:

16. > > > =

Do the unsigned right shift first, and then assign.

Example: x=4; X >>>=1, what is the value of x?

Binary of 4:

4 > > > 1:

X = 4 > > > 1; X = “0000 0010”, so the value of x is 2.

Code representation:

Running results:

Bitwise, bitwise or, xOR, inverse, and bitwise operations in Java are over for now.

Attached: operator list

Answering questions

If you have questions or want to learn more about cutting-edge technology, please leave them in the comments below, and I’ll answer them for you.

The previous chapter

Full Stack 2019 Java Chapter 19: Relational, Conditional, and ternary Operators

The next chapter

“Full Stack 2019” Java Chapter 21: Process control statement branching if, if-else

A study group

Join a synchronous learning group for mutual communication and progress.

  • Method 1: Follow the headline number gorhaf, private message “Java study Group”.
  • Method 2: follow the public account gorhaf, reply “Java learning group”.

Full stack engineer learning program

Follow us and join the “Full stack Engineer Learning Program”.

Copyright statement

Original is not easy, shall not be reproduced without permission!