Java programming (bitwise operators)

Source: Internet
Author: User
Tags bitwise operators

Binary Operators
Because data in the computer is in binary format, Java provides binary operators for direct operations. This is the bitwise operators and shift operators to be explained below.
Binary operators can be used to operate numbers directly on the basis of binary. The execution efficiency is much higher than that of ordinary mathematical operators. These operators are widely used in network programming, hardware programming, and other fields.
Binary operators have limited mathematical meaning.
In Java code, the default value for Direct Writing and output is decimal. in Java code, binary values cannot be directly written, but octal and hexadecimal numbers can be written. octal values start with digits 0, for example, 016 and hexadecimal values start with numbers 0 and X, for example, 0x12 and 0xaf.
When calculating binary operations, the Java Execution Environment (JRE) first converts the decimal number to binary, and then performs operations. If the value of the output result is returned, the number is converted to hexadecimal format.
  Note that:
1. The positive number of machines is the original code, and the negative number of machines is the complement code. Be careful when calculating. For details about the calculation of binary and complement code, see Java programming things 7-the concept of hexadecimal, and Java programming Things 8-The data expression inside the computer.
2. integer calculation results are all int type, regardless of whether bytes or short are binary.
  Bitwise operators
There are four major bitwise operators in Java: & (bitwise AND), | (bitwise OR), ^ (XOR), and ~ (Bitwise inversion). The following describes the operation rules and examples in sequence.
L & (and)
Calculation rule: the number involved in the operation. If the value is low and the value is less than zero, the corresponding binary bitwise is 1, and the calculation result is 1. Otherwise, the value is 0. applicable: block one or more digits. Because any number and 0 and 0 are both 0. Sample Code:
Int A = 4;
Int B = 10;
Int c = A & B;
Computing process:
The binary format of 4 is 0000 0000 0000 0000 0000 0000 0000 0100
The binary format of 10 is 0000 0000 0000 0000 0000 0000 0000 1010
According to the calculation rule, the result is 0000 0000 0000 0000 0000 0000 0000
This number is converted to decimal 0 l | (OR)
Calculation rule: number involved in the operation, low-level alignment, Zero Filling for low-level deficiency, the corresponding binary bit is 1, otherwise it is 0. applicable scenario: change one or more digits to 1. because 1 and any number are either 1. sample Code:
Int A = 4;
Int B =-10;
Int c = A | B;
Computing process:
The binary format of 4 is 0000 0000 0000 0000 0000 0000 0000 0100
The binary format of 10 is 1111 1111 1111 1111 1111 1111 1111 0110
Examda prompt: According to the calculation rule, the result is 1111 1111 1111 1111 1111 1111 1111 0110
This binary number is converted to the decimal number-10. L ^ (XOR)
Calculation rules: numbers involved in the operation, low-level alignment, Zero Filling for low-level insufficiency, and the binary phase of the corresponding values are both zero. 1. Applicable scenarios: Determine whether the digits correspond to the same bit.
Sample Code:
Int A = 4;
Int B = 10;
Int c = a ^ B;
Computing process:
The binary format of 4 is 0000 0000 0000 0000 0000 0000 0000 0100
The binary format of 10 is 0000 0000 0000 0000 0000 0000 0000 1010
According to the calculation rule, the result is 0000 0000 0000 0000 0000 0000 0000
This number is converted to a decimal value of 14 L ~ (Not)
Operation Rule: only operate on a number, and change the bitwise of 1 in the number to 0, and the bitwise of 0 to 1. Applicable scenario: reverse the sample code of the number content:
Int A = 4;
Int c = ~ A;
Computing process:
The binary format of 4 is 0000 0000 0000 0000 0000 0000 0000 0100
According to the calculation rule, the result is 1111 1111 1111 1111 1111 1111 1111
This number is converted to decimal number-5. In fact, bitwise operations should be consistent with the actual implementation, that is, the provided limit-level operator number, each operator has a corresponding circuit implementation.
Simple Example:
Convert any number to a positive number.
If n is an arbitrary integer, the code that converts n to a positive number is:
Int M = N & 0x7fffffff;
Determine whether the value of the last third digit of any number is 1. If n is an arbitrary integer, the code is:
Int M = N & 0x4;
Boolean B = (M! = 0 );
Returns the fourth position of any number
If n is an arbitrary integer, the code is:
Int M = n | 0x8;

In the third chapter of thinking in Java:

The operation object oriented to the shift operator is also the binary "bit ". You can use them to process integer types (one of the main types) separately ). The Left shift operator (<) can move the operator object on the left of the operator to the specified number of digits on the right of the operator (0 in the low position ). The "Signed" right shift operator (>) moves the operator object on the left of the operator to the right of the specified number of digits on the right of the operator. The "Signed" right shift operator uses the "symbol extension": If the value is positive, 0 is inserted at the high position; if the value is negative, 1 is inserted at the high position. Java also adds an "unsigned" right shift operator (>>>), which uses "Zero extension": no matter whether it is positive or negative, 0 is inserted at a high position. This operator is not available in C or C ++.
If char, byte, or short is displaced, they are automatically converted to an int before the shift. Only the five low positions on the right can be used. This prevents us from moving the unrealistic digits in an int number. If a long value is processed, the final result is long. At this time, only the six low positions on the right will be used to prevent the long value from being moved beyond the existing ones. However, a problem may also occur during "unsigned" right shift. If you perform the right shift operation on the byte or short value, the result may not be correct (especially highlighted in Java 1.0 and Java 1.1 ). They are automatically converted to the int type and shifted to the right. But "Zero extension" does not happen, so in those cases, the result of-1 will be obtained.

 

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.