Java Basics (Bitwise operations)

Source: Internet
Author: User
Tags bitwise

http://aokunsang.iteye.com/blog/615658

Prelude:

Yesterday a buddy asked me Java displacement you will, I said no, think about displacement so troublesome, generally have displacement Java code will not look, there are a few people would ah, right? But the man's answer, let me depressed half a day: "This will not Ah, are Java-based things Ah!" "I fainted, listening to the old unhappy, so I decided to the displacement of a probe, originally also so casual ah, haha." Therefore, the following summary of the experience, welcome to watch.

Thank you here first, Javaeye on the experts on the Technical blog pointing. TKS very much.

Technical Summary:

<1> Before you understand the displacement, look at the binary representations and relationships of positive and negative numbers:
Example 15 and-15:

15 The original code: 00000000 00000000 00000000 00001111
Complement: 11111111 11111111 11111111 11110000
+1 =
-15 of the original code: 11111111 11111111 11111111 11110001

Negative number of the original code is: positive number of the original code inversion, plus 1.

<2> Displacement Operations: (only valid for data of type int, in Java, an int is always 32 bits long, or 4 bytes, and it operates on the binary number of the integer). It can also be used for the following types, i.e. Byte,short,char,long (of course, They are all in integer form). When the four types are, the JVM first converts them into int and then operates.

<< left Shift
>> Right Shift
>>> Unsigned Right Shift

<< and >> are,>>> for the displacement of values. "Note": <<< is not present in Java.

$1>The meaning of the m<<n: the binary number represented by the integer m is shifted to the left n bit, the high shift out n bits are discarded, the low is 0. (A positive number becomes negative in this case)
Instance:
3<<2 Anatomy:
32 binary form: 00000000 00000000 00000000 00000011, in accordance with the principle of "00000000 00000000 00000000 00001100, that is 12."

Left shifts the integer to a negative number:
10737418<<8
107,374,182 binary representation: 00000000 10100011 11010111 00001010, in accordance with the principle of $10100011 11010111 00001010 00000000, that is:-1546188288.

$2>M>>n meaning: The integer m represents the binary number to the right of N-bit, M is positive, high all the complement 0;m negative, high all 1.
Instance:
3>>2 Anatomy:
32 in the form of: 00000000 00000000 00000000 00000011, according to the principle of "$", Get 00000000 00000000 00000000 00000000, that is, 0.
-3>>2 Anatomy:
-32 in the form: 11111111 11111111 11111111 11111101, according to the principle of the "$", get 11111111 11111111 11111111 11111111, that is-1.

Above: Each integer represents the binary is 32-bit, if the right shift 32-bit and right-shift 0-bit effect is the same. And so on, the right-hand 32 is the same as the double-digit.

$3>M>>>n: The integer m represents the binary right shift n bits, whether positive or negative, the high is 0.
Instance:
3>>>2 Anatomy:
32 binary form: 00000000 00000000 00000000 00000011, according to the principle of $ A, get 00000000 00000000 00000000 00000000, that is, 0.
-3>>>2 Anatomy:
-32 binary form: 11111111 11111111 11111111 11111101, according to the principle of $ A, get 00111111 11111111 11111111 11111111, that is, 1073741823.


"Note": For $1,$2,$3, if n is negative: then the JVM will first let n 32 modulo, become an absolute value less than 32 negative, and then add 32, until n becomes a positive number.
Instance:
4<<-10
4 binary form: 00000000 00000000 00000000 00000100,-10 to 32 modulo plus 32, needless to say, get 22, then 4<<-10, which is equivalent to 4<<22.
At this point, according to the principle, get 00000001 00000000 00000000 00000000, the resulting is: 16777216.

OK, you are done.

Sum up:
M<<n that is, if the number does not overflow, for positive and negative numbers, the left-shift n-bit is equal to M times 2 of the N-squared.
M>>n is equivalent to M divided by 2 of the N-square, resulting in an integer, that is, the result. If the result is a decimal, there are two situations: (1) If M is a positive number, the resulting chamber will unconditionally discard the decimal place, (2) if M is negative, discard the fractional part, and then add the integer part +1 to get the value after the displacement.

---------------------------------------------------------------------------------


Next, here is the advantage of bit operation, the speed is very fast, these are the bottom of the binary machine operation instructions.
For example: A*2,

1.JVM allocates space for variable a first;

2. Perform the a*2 operation again;

3. Return the result to the corresponding variable.
And a<<1, like a*2, requires only one instruction and is fast. Of course, the first three kinds of displacement operations are multiples of 2.

Available when the operation is in progress.



To add a little bit more, talk about bits and, of course, four operators: ~ (bitwise non), | (bitwise OR),& (bitwise

and), ^ (bitwise XOR), these are the basic uses of the university computer, operate on the binary form of integers, and then

Convert to an integer, as follows.
1.~ (bitwise NON): "Jiayi" is a bitwise inverse of the binary form of the integer.
~: (unary operator)
The binary form of 4 is: 00000000 00000000 00000000 00000100, bit-wise reverse

To: 11111111 11111111 11111111 11111011, that is-5.
2.| (bitwise OR): "Jiayi" makes a bitwise logical OR operation on the binary form of two integers, the principle is: 1|0=1,0|0=0,1|1=1,0|1=1
such as
4|-5:
The 4 binary form is: 00000000 00000000 00000000 00000100,
-5 binary form: 11111111 11111111 11111111 11111011,
Bitwise logic OR operation: 11111111 11111111 11111111 11111111, that is, get-1.
3.& (Bitwise AND): "Jiayi" the binary form of two integers is bitwise logical AND operation, principle: 1|0=0,0|0=0,1&1=1;0&1=0, etc.
4&-5:
The 4 binary form is: 00000000 00000000 00000000 00000100,
-5 binary form: 11111111 11111111 11111111 11111011,
Bitwise logic AND operation: 00000000 00000000 00000000 00000000, that is, the resulting 0.

Practical application: Can convert bytes to integers, -64&0xff=192, can also be in the form of octal, -64&0377=192,

In fact, both 0xFF and 0377 represent integers 255,
4.^ (Bitwise XOR): "Jiayi" is a bitwise logical XOR of the binary form of two integers, principle: 1^1=0,1^0=1,0^1=1,0^0=0.
4^-5:
The 4 binary form is: 00000000 00000000 00000000 00000100,
-5 binary form: 11111111 11111111 11111111 11111011,
Bitwise logical XOR Operation: 11111111 11111111 11111111 11111111, i.e. get-1.

Practical application: Bitwise XOR can compare two numbers equal, it uses the principle of 1^1=0,0^0=0. 20^20==0

Java Basics (Bitwise operations)

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.