About Java Bitwise operations

Source: Internet
Author: User
Tags binary to decimal

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

Note: For right-shift 32-bit and right-shift 0-bit is the result is the same, I have not been able to understand. Now I can only understand that 32 is more special. Equivalent to the whole shift. Same as move 0 bit. The left shift is the same.

$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

Transferred from: http://aokunsang.javaeye.com/blog/615658

Some notes about binary:

On binary representation of negative numbers

Today we know how to represent a binary method for a negative number: for example, 5
First step: First to turn 5 into a 101 binary form
The second step: the other is to reverse the position, (forming the front is all 1) 010
Step three: At the end add 1 to form: 11111111 11111111 11111111 11111011
Conversely, if the highest bit is 1 of the binary into negative shaping
First step: Reverse, Turn 00000000 00000000 00000000 00000100
Step Two: Add 1 to the lowest bit and form 101
The third step: the formation of plastic 5, plus a minus sign;
How to use code to convert binary to decimal in Java
public int binaryintoint (String str)
{
int j=0,i=0;
char c;
For (I=0;i<str.length (); i++)
{
if (Str.charat (Str.length ()-i) = = ' 1 ')
{
J=j+exp (2*ln (Str.length ()-i));
}

}
Return J;
}
Turn from:
Http://blog.csdn.net/zdp5528/archive/2008/04/10/2278719.aspx

(1) Positive and negative representation method
denoted by the highest bit of bytes: "1" means "positive", "0" means "negative"

(2) in which code is the number stored in the computer?
Complement

(3) The method of converting the binary complement of negative numbers into decimal
1, the complement "take the Counter" (the binary number of the members of the "1" for "0", "0" for "1". For example, "101010" is reversed to "010101")
2. "Add 1" to the inverted binary number
Convert a negative number to a binary

Http://blog.csdn.net/onewalkingman/archive/2009/01/10/3746154.aspx

We already know that all of the data in the computer is ultimately expressed using binary numbers.

We have also learned how to convert a 10 binary number into a binary number.

However, we still do not learn how a negative number is expressed in binary.

For example, suppose you have a number of type int and a value of 5, then we know that it is represented in the computer as:

00000000 00000000 00000000 00000101

The 5 conversion to two is 101, but the number of int is 4 bytes (32 bits), so a heap of 0 is filled in front.

Now you want to know, how is 5 represented in the computer?

In a computer, negative numbers are expressed in the form of a positive complement.

What do you mean, "complement"? This has to be from the original code, anti-code speaking.

The original code: an integer, converted into a binary number according to the absolute size, called the original code.

For example, 00000000 00000000 00000000 00000101 is the original code of 5.

Inverse code: The binary number is reversed, the resulting new binary number is called the original binary number of the inverse code.

Take the counter-operation means: The original is 1, get 0; 0, 1. (1 change 0; 0 Change 1)

For example: 00000000 00000000 00000000 00000101 Each is reversed, the 11111111 11111111 11111111 11111010.

said: 11111111 11111111 11111111 11111010 is a 00000000 00000000 00000000 00000101 anti-code.

The anti-code is mutual, so it can also be called:

11111111 11111111 11111111 11111010 and 00000000 00000000 00000000 00000101 Each are anti-code.

Complement: The inverse code plus 1 is called the complement.

That is, to get a number of the complement, first get the anti-code, and then add the inverse code 1, the resulting number is called the complement.

For example: 00000000 00000000 00000000 00000101 The inverse code is: 11111111 11111111 11111111 11111010.

Then, the complement is:

11111111 11111111 11111111 11111010 + 1 = 11111111 11111111 11111111 11111011
So, 5 is expressed in the computer as: 11111111 11111111 11111111 11111011. Convert to 16 binary: 0xFFFFFFFB.
For another example, let's see how the integer-1 is represented in the computer.

Assuming this is also an int type, then:
1, first take 1 of the original code: 00000000 00000000 00000000 0000000111111111 11111111 11111111 11111110

2, the reverse code: 11111111 11111111 11111111 11111110

3. Complement: 11111111 11111111 11111111 11111111

Visible, 1 in the computer with the binary expression is all 1. 16 Binary: 0xFFFFFF

Personal Summary:
Complement: The inverse code plus 1 is called the complement.

A negative binary is converted into a decimal
For example: 11111111 11111111 11111111 11111110
First step: minus 1 becomes 11111111 11111111 11111111 11111101 (binary, first bit is less than the second first and 2)
Step two: Take Reverse 00000000 00000000 00000000 00000010
Switch to decimal based on the usual method. Last to add-No.
About the ~ bitwise non in Java
such as:
12 Binary 00000000 00000000 00000000 00000001
To: 11111111 11111111 11111111 11111110
The first 1 shows that the number is--based on the above calculation, the result can be calculated.

About Java Bitwise operations

Related Article

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.