Java offset Operations

Source: Internet
Author: User

<1> Before you understand the displacements , understand the binary representations and relationships of positive and negative numbers:
Examples 15 and-15:

15 Original code: 00000000 00000000 00000000 00001111
Complement: 11111111 11111111 11111111 11110000
+1 =
-15 Original code: 11111111 11111111 11111111 11110001

The original code of negative numbers is: positive number of the original code to take counter, plus 1.

<2> Displacement operations: (valid only for data of type int, injava , an int is always 32 bits in length, that is, 4 bytes, and it operates on the binary number of that integer). can also function To the following types, that is, Byte,short,char,long (they are all integers, of course). When for these four types, the JVM first converts them to int and then operates.

<< Move Left
>> Move Right
>>> No sign right shift

<< and >> for numerical displacement ,>>> for logical displacement . "Note": <<< is not present inJava .

The meaning of the

$1>m<<n: To move the binary number represented by the integer m to the left n bit, the high displacement to discard the n bits, the low fill 0.  (the form of a positive number to a negative number)
instance:
  3<<2 Analysis:
  32 in form: 00000000 00000000 00000000 00000011, according to the principle of $00000000 00000000 00000000 00001100, that is, 12. 
  
  left to make the integer negative:
  10737418<<8
  107,374,182 binary representation: 0 0000000 10100011 11010111 00001010, according to the principle of $10100011 11010111 00001010 00000000,
                                                                                      is: -1546188288

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

Above: Each integer represents the binary is 32-bit, if the right to move 32-bit and 0-bit to the right to move the same effect. And so on, the same number of times as the right shift 32.

$3>m>>>n: integer m represents the binary right shift n bit, regardless of positive negative, the high is 0.
Instance:
3>>>2 Analysis:
32 in the form of: 00000000 00000000 00000000 00000011, according to the principle of $, Get 00000000 00000000 00000000 00000000, that is 0.
-3>>>2 Analysis:
-32 in form: 11111111 11111111 11111111 11111101, according to the principle of $, 00111111 11111111 11111111 11111111, that is 1073741823.

"Note": For $1,$2,$3, if n is a negative number: The JVM first takes N to modulo 32, turns into a negative number with an absolute value of less than 32, and then adds 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, that is equivalent to 4<<22.
At this point according to the principle, get 00000001 00000000 00000000 00000000, the resulting is: 16777216.

OK, that's it.

Sum up:
M<<n that is, if the number does not overflow, for positive and negative numbers, the left-shifted n-bit is equal to M times 2 of N-th.
M>>n is the equivalent of M divided by 2 of the n-th side, the resulting integer, that is the result. If the result is a decimal, two things happen: (1) If M is a positive number, the resulting chamber unconditionally discards the decimal position, (2) if M is negative, discards the fractional part, and then the integer part plus +1 gets the value of the displacement .

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

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

1.JVM allocates space first for variable A;

2. To carry out the a*2 operation again;

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

Available when doing the operation.

To add a little bit of the point of operation, of course, there are four operators : ~ (bitwise non), | ( Bitwise OR),& ( bitwise

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

Converted to integers, as follows.
1.~ (bitwise NON): "Jiayi" takes a bitwise counter to the binary form of the integer.
~4: (unary operator)
4 of the binary form is: 00000000 00000000 00000000 00000100, a bit after the reverse

To: 11111111 11111111 11111111 11111011, that is-5.
2.| (bitwise OR): "Jiayi" is a bitwise logical OR operation of the binary form of two integers, and the principle is: 1|0=1,0|0=0

, and so on.
    4|-5:
     4 is in binary form: 00000000 00000000 00000000 00000100,
  The binary form of    5 is: 11111111 11111111 11111111 11111011,
  bitwise logical OR Operational: 11111111 11111111 11111111 11111111, that is to get-1.
3.& (Bitwise AND): "Jiayi" to two integer binary form bitwise logic and operation, principle: 1|0=0,0|0=0,1&1=1 and so on. &NBSP;&NBSP
   4|-5:
     4 is in binary form: 00000000 00000000 00000000 00000100,
The binary form of     5 is: 11111111 11111111 11111111 11111011,
  bitwise Logical AND Operation: 00000000 00000000 00000000 00000000, that gets 0.   
4.^ (Bitwise XOR): "Jiayi" a bitwise-by-bit 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 binary form of the
     4 is: 00000000 00000000 00000000 00000100,
   - The binary form of 5 is: 11111111 11111111 11111111 11111011,
Bitwise logical XOR OR operation: 11111111 11111111 11111111 11111111, that is -1.

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.