Java bit operations, Java operations

Source: Internet
Author: User
Tags arabic numbers

Java bit operations, Java operations

One Java bit operation

1. Representation Method:

In Java, the binary number is represented by a complement. The highest bit is the symbol bit, the positive number is 0, and the negative number is 1. The following requirements must be met.

(1) The highest bit of a positive number is 0, and the rest represent the value itself (binary number ).

(2) For a negative number, the bitwise return of the complement code of the absolute value of the number is used to add 1 to the total number.

2. bitwise operators

Bitwise expressions are composed of operands and bitwise operators. bitwise operations are performed on binary numbers of integer types. Bitwise operators can be divided into logical operators (including ~ , &, |, And ^), and shift operators (including >>and <and> ).

1) 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 at the low position ).

2) 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.

3) Java also adds an "unsigned" right shift operator (>>>), which uses "Zero extension": both positive and negative values insert 0 at a high position. This operator is not available in C or C ++.

4) 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.

Pay attention to the following points when performing bitwise operations.

(1) >>>> the difference between >>>: when performing an operation, >>> the operand of the operator is a high complement of 0, and> the operand of the operator moves into the original high value.

(2) shifts one to the right is equal to dividing by 2, and shifts one to the left (without Overflow) is equivalent to multiplying 2; the shift operation speed is higher than the multiplication and division operation.

(3) If the data lengths of the two operands used for bit logical operations are different, the return value should be a data type with a long data length.

(4) bitwise OR exchange two values without using temporary variables, or flip the specific value of an integer.

(5) bitwise AND operation can be used to block specific bits, or to take some specific bits in a certain number.

(6) bitwise OR operation can be used to set the l value of the specific location of an integer.

3. bitwise operator priority

~ Is the highest priority, followed by <,> and>, followed by "&", followed by "^", with the lowest priority |.

 

2. bitwise XOR operators ^

The two values involved in the calculation. If the two corresponding bits are the same, the result is 0. Otherwise, the result is 1. 0 ^ 0 = 0, 1 ^ 0 = 1, 0 ^ 1 = 1, 1 ^ 1 = 0

Example: 10100001 ^ 00010001 = 10110000

0 ^ 0 = 0 0 ^ 1 = 1 0 variance or any number = any number

1 ^ 0 = ^ 1 = 0 1 variance or any number-returns the inverse of any number

Set yourself to 0 for any number difference or yourself =

(1) bitwise exclusive or can be used to flip certain specific bits. For example, if the number is 10100001 or 2nd bits, bitwise exclusive or operation can be performed on the number and 3rd bits. 10100001 ^ 00000110 = 10100111 // 1010 0001 ^ 0x06 = 1010 0001 ^ 6

(2) bitwise exclusive or operation can exchange two values without using temporary variables.

For example, you can use the following statement to exchange the values of two integers, a and B:

A = 10100001, B = 00000110

A = a ^ B; // a = 10100111

B = B ^ a; // B = 10100001

A = a ^ B; // a = 00000110

(3) The unique or operator is characterized by the fact that the number a is two times different or the same number B (a = a ^ B) is still the original value.

 

3. Besides the binary representation in Java:

The representation of data in a computer eventually exists in binary format, so sometimes binary can be used to solve the problem more intuitively.

However, the binary number is too long. For example, the int type occupies 4 bytes and 32 bits. For example, if the value is 100, the value expressed in the binary number of the int type is:

0000 0000 0000 0000 0110 0100

No one would like to think about or operate on such a long number. Therefore, C, C ++, and java do not provide a method for writing binary numbers directly in code.

Octal expression

How can we express an octal number? If the number is 876, we can conclude that it is not an octal number, because the octal number cannot contain more than 7 Arabic numbers. However, if the number is 123, 567, or 12345670, it is possible to set the number to octal or decimal.

Therefore, if you want to specify a number that uses octal, you must add a value of 0 before it. For example, 123 is in decimal format, but 0123 indicates that octal is used. This is the expression of the octal number. Now, for the same number, for example, 100, we can use the normal 10 hexadecimal expression in the Code, for example, during variable initialization:

Int a = 100;

We can also write:

Int a = 0144; // 0144 is 100 of the octal value. How can a 10-digit number be converted into an octal value.

Remember, you cannot lose the first zero when expressing it in octal. Otherwise, the computer is regarded as a 10-digit system. However, when the number of octal characters is used, the value 0 cannot be used, which is the "Escape Character" expression we learned before for expressing characters.

Hexadecimal expression

If no special writing format is used, the hexadecimal number will be mixed with the hexadecimal number. A random number: 9876, it cannot be seen that it is in hexadecimal or 10 hexadecimal format.

The hexadecimal number must start with 0x. For example, 0x1 indicates a hexadecimal number. 1 indicates a decimal number. For example, 0xff, 0xFF, 0X102A, and so on. X is not case sensitive. (Note: 0 in 0x is the number 0, not the letter O)

Some examples are as follows:

Int a = 0x100F;

Int B = 0x70 +;

The last point is very important. There are positive and negative numbers in the decimal system. For example, 12 indicates positive 12, while-12 indicates negative 12 ,; but the octal and hexadecimal values can only be used to express unsigned positive integers. If you enter-078 in the code or write-0xF2, the compiler does not regard it as a negative number.

Sample Code:

Package bit operation; public class Test {public static void main (String [] args) {byte B = 5; int I = 4; long l = 10; judgeIndex (); System. out. println (B <2); // The running result is 20System. out. println (I <2); // The running result is 16System. out. println (l> 2); // The running result is 2 // 1, left shift (<) // 0000 0000 0000 0000 0000 0000 0000 and then left shift after 2 digits, low fill 0: // 0000 0000 0000 0000 0000 0000 0001 0100 converted into 10 hexadecimal system. out. println (5 <2); // The running result is 20 // 2. Right Shift (>) High complement sign bit // 0000 0000 0000 0000 0000 0000 0000 0101 then shifts two places to the right, high complement 0: // 0000 0000 0000 0000 0000 0000 0000 0001System. out. println (5> 2); // The running result is 1 // 3. unsigned right shift (>>>). Add 0 to a high position. // For example, convert-5 to binary and then: 0101 1 is 1011 // 1111 1111 1111 1111 1111 1111 1111 1011 // We shifted 5 three places to the right,-5 to the right, and unsigned to the right, respectively.: system. out. println (5> 3); // The result is 0 system. out. println (-5> 3); // The result is-1System. out. println (-5 >>> 3); // The result is 536870911 // 4, bits and (&) // bits and: The nth of the first operand is located in the nth bit of the second operand. If both are 1, the nth result is also 1; otherwise, it is 0System. out. println (5 & 3); // The result is 1 system. out. println (4 & 1); // The result is 0 // 5, bit, or (|) // The Nth of the first operand is located in the nth bit of the second operand. As long as one is 1, the nth of the result is also 1; otherwise, it is 0System. out. println (5 | 3); // The result is 7 // 6, bitwise exclusive or (^) // the nth of the first operand is opposite to the nth of the second operand, the result n is also 1, otherwise it is 0 System. out. println (5 ^ 3); // The result is 6 // 7, the bit is not (~) // If the nth bit of the operand is 1, the nth bit of the result is 0, and vice versa. System. out. println (~ 5); // The result is-6} // a 64-bit integer and binary location number to determine whether the location is 1 or 0 public static void judgeIndex () {long a = 123; long c = 0; int B = 4; // judge whether the fourth digit is 1 or 0c = a> B; a = c & 1; System. out. println ("c =" + c + "" + "a =" + a); // c = 7 a = 1 }}

Running result:

C = 7 a = 1
20
16
2
20
1
0
-1
536870911
1
0
7
6
-6

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.