Java asks _03 basic syntax (005) _ binary is how to do bit arithmetic

Source: Internet
Author: User

Click to enter _ many other _java thousand ask


How binary is doing bit arithmetic

All the numbers in the program are stored in binary form in the computer's memory. bit arithmetic is the straightforward operation of the binary bits of integers in memory.

Other operators see here: What are the operators of Java

Most of the arithmetic processes are converting integers to binary first. The corresponding binary operation is then performed. The common operations are as follows:

Below we specify thatoperator Precedence See here: What is the precedence of the Java operator

1. Bitwise AND AND
Two binary numbers arebitwise-andOperation: Two digits of the same bit are 1. is 1; if one is not 1, it is 0.


For example: 00101 & 11100 = 00100
Pass is often used in binary take-up operations, such as the result of a number & 1 is the last bit of binary.
This can be used to infer the parity of an integer. The last digit of the binary is 0, which indicates that the number is even. The lowest of 1 indicates that the number is odd.



2, bitwise OR OR
Bitwise or operation of two binary numbers: the same bit only needs one of 1, which is 1.


For example: 00101 | 11100 = 11101
Pass is often used for unconditional assignment on binary positioning, such as a number | The result of 1 is to force the last binary into 1.


Suppose you need to turn the binary last to 0, to this number | After 1, we can lose one more. In fact, the meaning is to force this number into the nearest even.

3. Bitwise XOR or XOR
Bitwise XOR of two binary numbers: the same bit differs by 1, and the same is 0.
For example: 00101 ^ 11100 = 11001
The XOR operation can be used for simple encryption . is because the inverse operation of the XOR operation is it itself . That is, two times or the same number of the final result is unchanged. i.e. (a XOR b) xor B = A.


For example I want to say to someone 1314520, but afraid of others know, so the two parties agreed to take birthday 19871112 as the key. 1314520 xor 19871112 = 20659024. I will tell the 20659024 mm. MM again calculates the value of 20659024 xor 19871112, resulting in 1314520.

4, the bitwise take anti not
The bitwise Inverse of a binary number: 0 of each bit becomes 1. 1 becomes 0.
For example: ~ 0011 1100 = 1100 0011
Use the not operation with extreme caution. You need to be aware that integer types have no symbols .

Assuming that the object of not is an unsigned integer (which cannot represent a negative number), then the resulting value is the difference between it and the upper bound of the type, since the numbers of unsigned types are represented by 00 to $FFFF.



5. Move left
To move a binary number to the left : a << B means to shift A to binary and left B bit (add B 0 later). The left shift is indistinguishable from unsigned.
For example: 1100100 << 2 = 110010000, that is, << 2 = 400.
Can see. The value of a << B is actually a multiplied by 2 of the B-squared , since adding a 0 after the binary number is the equivalent of multiplying the number by 2.


It is generally thought that a << 1 is faster than a * 2, since the former is more of a lower level of operation. So if you multiply the 2 or 2 in the program, try to replace it with a left shift.


Defining some constants may use the SHL operation.

You can easily use 1 << 16-1 to represent 65535.

6. Right Shift with symbols
unsigned right-shift operation on a binary number: a >> B means to move a to the binary right and then the B bit, and the sign bit remains unchanged.
For example: 1100100 >> 2 = 11001, that is, >> 2 = 25.


Similar to <<, the B-squared (rounding) of a divided by 2 .

We also often use << to replace divide by 2, such as binary lookup, heap insert operations, and so on.
Finding a way to replace division with << can greatly improve the efficiency of the program. The greatest common divisor binary algorithm uses the 2 operation to replace the surprisingly slow mod operation, which can increase the efficiency by 60%.

7. Unsigned Right Shift
A signed right-shift operation on a binary number: a >> b indicates that the B is shifted to the right after the A is transformed into a binary. The sign bit also participates in the movement. Post-Move high-level 0.


is only meaningful for 32-bit and 64-bit values.

Click to enter Ooppookid's blog

Java asks _03 basic syntax (005) _ binary is how to do bit arithmetic

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.