Bitwise operators in Java

Source: Internet
Author: User

The technology is only available in the project. I didn't care much about the operator when I learned j2se. I realized that the bitwise operator had something inside the permission management module some time ago. The author makes a summary here, hoping to be useful to readers.

Bitwise operators supported by Java:

&: Bitwise AND.

|: By bit or.

~ : Non-bitwise.

^: Returns the bitwise XOR.

<: Left displacement operator.

>>: Right shift operator.

<: Unsigned right shift operator.

These four operators are common and are also summarized as follows:

First Operator

Second Operator

Bitwise AND

By bit or

Bitwise OR

0

0

0

0

0

0

1

0

1

1

1

0

0

1

1

1

1

1

1

0

PS: we may find it difficult to remember an exclusive OR operator (which can never be remembered). In fact, our predecessors have long summarized the laws of exclusive or operator, as described below.

Exclusive or: This is also called a semi-addition operation. For example, 1 exclusive or 1 can be regarded as 1 + 1 = 10 in binary and then the last bit is taken. This is exactly the result of exclusive or, 0 + 0, 1 + 1, 0 + 1.

Operation Principle:

The first four bitwise operators are relatively simple. You only need to perform logical operations by bit. For example, the execution process of 5 & 9 (result 1) is as follows:

For example ~ -5 (Result 4:

Other operations (| ,~ , ^) Similarly.

The subsequent shift operations are a bit more complex than the previous ones, but it is still easy to understand.

-5 <2 (Result:-20:

-5> 2:

-5> 2 Operation Process

We can see from the above process that as long as the binary code to be shifted has no digits with valid bits lost (for positive numbers, it usually means that all the bits to be removed are 0 ), it is not difficult to find that the n-bit left is equivalent to multiplying the N power of 2, and the N power of 2 is shifted to the N power of 2. Another point is that the shift operation won't change the operand itself, but it just gets a new operation result, and the original operand itself won't change.

Learn and feel:

In many cases, it is useless to learn basic things.

For example, when I first came into contact with my computer and learned the keyboard, I thought that the end and home keys were useless. Later I found out how great these two keys were invented by my predecessors when I typed more code, these two keys do not know how much efficiency they have improved for the whole human being. In addition, this study of the Java bitwise operator is also a typical example. I didn't think this bitwise operator was useful (a little tasteless ), after completing the permission module, you can find out its advantages (for details about how to use it, see my other blog ). There are still many similar situations. For example, when studying basic courses such as operating systems, computer components, and data structures, they always feel that they have little to do with writing their own code, however, once you encounter problems (large data volumes and high concurrency), you can use these "useless" courses to analyze code efficiency and analyze system resources to solve the problem.

Do not doubt the usefulness of existing knowledge (courses, technologies) or things. "existence is reasonable." If it is useless, it will be eliminated by our predecessors. If you do not use a certain knowledge, it has nothing to do with the knowledge itself. do not feel that it is useless if you do not use it now. If you do not use it now, it is not enough to indicate your own level, what we need to do is to create an index that can be used with learning.

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.