Bit operations in Java

Source: Internet
Author: User

Recently interesting, read some Java source code, found that the source of some of the clever implementation of the code is implemented through bit operations, such as decimal integers into 16 binary form of string output:

1     Public StaticString tohexstring (inti)2 {3         returnTounsignedstring (I, 4);4  }5 6  Private StaticString tounsignedstring (intIintshift) {7         Char[] buf =New Char[32];8         intCharpos = 32;9         intRadix = 1 <<shift;Ten         intMask = radix-1; One          Do { ABuf[--charpos] = digits[i &mask]; -I >>>=shift; -} while(I! = 0); the  -         return NewString (buf, Charpos, (32-charpos)); -     } -  +     Final Static Char[] digits = { -' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ' , +' 6 ', ' 7 ', ' 8 ', ' 9 ', ' A ', ' B ' , A' C ', ' d ', ' e ', ' f ', ' g ', ' H ' , at' I ', ' j ', ' K ', ' l ', ' m ', ' n ' , -' O ', ' P ', ' Q ', ' R ', ' s ', ' t ' , -' U ', ' V ', ' w ', ' x ', ' y ', ' z ' -};

First of all, the Java bit operations summed up, in the future to accumulate more, and then write the application of the bit computing experience and understanding it.

The following is transferred from: http://www.cnblogs.com/zhengtao/articles/1916751.html

One, Java bit operation

1. Presentation method:

In the Java language, the binary number is represented by a complement, the highest bit is the sign bit, the sign bit for positive numbers is 0, and the negative is 1. The complement representation needs to meet the following requirements.

(1) The highest number of positive numbers is 0, the remainder represents the value itself (binary number).

(2) For negative numbers, by the absolute value of the number of the complement of the bitwise negation, and then the whole number plus 1.

2. Bitwise operators

A bitwise operation expression consists of an operand and a bitwise operator that implements a bitwise operation on a binary number of integer types. Bitwise operators can be divided into logical operators (including ~, &, | and ^) and shift operators (including >>, <<, and >>>).

1) The left shift operator (<<) moves the operand to the left of the operator to the left by the number of digits specified to the right of the operator (in the low 0).

2) The "signed" Right Shift operator (>>) moves the operand to the left of the operator to the right by the number of digits specified to the right of the operator. The "signed" right shift operator uses "symbol extension": If the value is positive, insert 0 at the high, and if the value is negative, insert 1 at the high.

3) Java has also added an "unsigned" right shift operator (>>>) that uses the "0 extension": both positive and negative, inserting 0 in the high position. This operator is not in C or C + +.

4) If the Char,byte or short are shifted, they are automatically converted to an int before the shift is made. Only the 5 lows on the right will be used. This prevents us from moving the impractical number of digits in an int number. If a long value is processed, the last result is a long. Only the 6 lows on the right are used to prevent moving beyond the existing number of digits in the long value. However, you may also encounter a problem when you perform an "unsigned" right shift. If you perform a right shift operation on a byte or short value, you may not get the correct result (Java 1.0 and Java 1.1 are particularly prominent). They are automatically converted to the int type and are shifted right. But the "0 extension" will not happen, so in those cases you will get the result of-1.

The following points need to be taken into consideration when doing bit operations.

(1) The difference between >>> and >> is that the operand of the,>>> operator is high-up by 0 while performing the operation, while the operand of the >> operator is moved to the value of the original high.

(2) A right shift is equivalent to dividing by 2, moving left one (in the case of no overflow) equal to multiplying by 2, and the shift operation is faster than the multiplication operation.

(3) If the data length of the two operands of the bitwise logic operation is different, the return value should be a data type with a longer data length.

(4) Bitwise XOR or you can complete the interchange of two values without using a temporary variable, or you can flip the value of a particular bit of an integer number.

(5) Bitwise AND operations can be used to block specific bits or to take certain bits of a number.

(6) A bitwise OR operation can be used to set the value of a particular bit of an integral number to L.

3. Precedence of bitwise operators

~ has the highest priority, followed by <<, >> and >>>, and again is &, then ^, the lowest priority is |.

Second, bitwise XOR OR operator ^

The two values that participate in the operation, if two corresponding bits are the same, the result is 0, otherwise 1. namely: 0^0=0, 1^0=1, 0^1=1, 1^1=0

Example: 10100001^00010001=10110000

0^0=0,0^1=1 0 XOR or any number = any number

1^0=1,1^1=0 1 XOR or any number-any number taken against

Any number different or self = put yourself 0

(1) Bitwise XOR or can be used to make certain specific bits flip, such as the 2nd and 3rd bits of the logarithm 10100001 are flipped, the number and 00000110 can be bitwise XOR OR operation. 10100001^00000110=10100111//1010 0001 ^ 0x06 = 1010 0001 ^ 6

(2) by bitwise XOR, you can exchange two values without having to use a temporary variable.

For example, a value of two integer, A, or a, can be implemented by the following statement:

a=10100001,b=00000110

A=a^b;//a=10100111

B=b^a;//b=10100001

A=a^b;//a=00000110

(3) XOR operator is characterized by: number a two XOR or the same number B (A=a^b^b) is still the original value A.

Third, in Java in addition to the binary representation method:

Because the data is represented in the computer and eventually in binary form, sometimes binary is used to solve the problem more intuitively.

However, the binary number is too long. For example, an int type occupies 4 bytes and 32 bits. For example, 100, a binary number expressed in int type would be:

0000 0000 0000 0000 0110 0100

Faced with such a long number of thinking or operation, no one would like it. Therefore, c,c++, as well as Java, does not provide a way to write binary numbers directly in code.

An approach to the expression of eight decimal numbers

How to express an octal number? If this number is 876, we can conclude that it is not an octal number because it is not possible to have more than 7 Arabic numerals in octal numbers. But if this number is 123, is 567, or 12345670, then it is octal or 10 binary, it is possible.

So, a number, if you want to indicate that it uses octal, you must precede it with a number 0, such as: 123 is decimal, but 0123 means octal. This is how the octal number is expressed. Now, for the same number, such as 100, we can use the usual 10 in the code, for example, when the variable is initialized:

int a = 100;

We can also write this:

int a = 0144; 0144 is the octal 100, and how a 10 binary number is converted to 8 binary.

Remember, when you're using octal, you can't have the last 0 less. Otherwise the computer will be 10 binary. However, there is a place where octal numbers are used, but you cannot use Add 0, which is the "escape character" expression that we learned before to express a character.

How to express hexadecimal numbers

If a special writing form is not used, the 16 binary number is mixed with the 10 binary. Any number: 9876, you can not see it is 16 binary or 10 binary.

16 binary number must start with 0x. For example, 0x1 denotes a 16 binary number. and 1 represents a decimal. In addition, such as: 0xff,0xff,0x102a, and so on. The x is also not case sensitive. (Note: 0 in 0x is the number 0, not the letter O)

Here are some examples of usage:

int a = 0x100f;

int B = 0x70 + A;

The last point is important, 10 binary number has positive and negative points, such as 12 means positive 12, and 12 for negative 12, but 8 and 16 binary can only be used to express unsigned positive integers, if you in the code:-078, or write: -0xf2, the compiler does not treat it as a negative number.

Bit operations in Java

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.