The bitwise operator of Java

Source: Internet
Author: User
Tags bitwise bitwise operators logical operators

Bitwise operators allow us to manipulate a single "bit" in the primary data type of an integer, that is, bits. The bitwise operator performs a Boolean algebra on the corresponding bits in the two arguments and eventually produces a result.
Bitwise operations come from low-level operations in the C language. We often have to directly manipulate the hardware, we need to frequently set the hardware register bits. Java was originally designed to be embedded in the TV's top box, so this low-level operation is still preserved. However, due to the progress of the operating system, it may not be necessary to do bitwise operations too frequently.
If two input bits are 1, then the bitwise AND operator (&) generates a 1 in the output bit, otherwise the 0 is generated. If at least one of the two input bits is 1, the bitwise OR operator (|) generates a 1 in the output bit, and only if two input bits are 0, it generates a 0. If one of the two input bits is 1, but not all 1, a bitwise XOR (^, XOR, OR) generates a 1 in the output bit. A bitwise NOT (~, also called a "non" operator) is a unary operator; it operates on only one argument (all other operators are two-dollar operators). The opposite value of the input bit is generated by a bit not--if you enter 0, the output is 1; input 1, then output 0.
Both bitwise and logical operators use the same characters, but the number is different. Therefore, we can conveniently memorize the meanings of each: because "bit" is very "small", the bitwise operator uses only one character.
Bitwise operators can be used in conjunction with the equal sign (=) to combine operations and assignments: both &=,|= and ^= are legitimate (because ~ is a unary operator, it cannot be used in conjunction with =).
We treat Boolean (Boolean) types as a "unit" or "single bit" value, so it's somewhat unique. We can perform bitwise and,or and XOR, but cannot execute bitwise not (presumably to avoid confusion with logical not). For Boolean values, bitwise operators have the same effect as logical operators, except that they do not "short-circuit" in the middle. In addition, bitwise operations for Boolean values add an XOR logical operator that is not included in the list of "logical" operators. In the shift expression, we are barred from using Boolean operations, which are explained below.

Related Article

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: 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.