Java bit Operations Summary: bit operation uses a wide range of "turn"

Source: Internet
Author: User

The day before yesterday to study the next JDK collection interface, was prepared to continue to study the map interface, but a look at the HashMap class source dumbfounded, everywhere is the implementation of bit operations, so I think it is necessary to fill the knowledge of the bit, otherwise the code looks a bit laborious. Today the system has been studied, and is now recorded as follows.

The first thing to understand is that the Java bit operation is for the integer typeThe shift operation of the data type binary. Mainly includes bit and, bit or, bit non, signed left shift, signed right shift, unsigned right shift, and so on. It is important to note that there is no unsigned left shift <<< operator. According to the concept of bitwise operation, we need to understand the first two problems,What data types Java has is the integer data type and the conversion problem between the various digital binaries. Java integer data types are: Byte, char, short, int,Long To convert them into binary primitive form, it must be understood that they account for a few bytes. As we all know, one byte occupies 8 bits.
Number of digits in the data type
BYTE 8
Boolean 8
Short 16
int 32
Long 64
Float 32
Double 64
Char 16

It is also important to understand that the computer indicates that the number is not represented by the +-plus minus sign, but by the highest number, 0 means positive, 1 is negative

So for example, 4 is represented by the binary source code is 1111 1111 1111 1111 1111 1111 1111 1100

The following is a description of the operation rules for each bit operation according to one instance:
Bit and & (True True for true and false for fake fake fake)
4&6
0000 0000 0000 0000 0000 0000 0000 0100
0000 0000 0000 0000 0000 0000 0000 0110
0000 0000 0000 0000 0000 0000 0000 0100
Results: 4

Bit or | (True True or False for True or false for false)
4|6
0000 0000 0000 0000 0000 0000 0000 0100
0000 0000 0000 0000 0000 0000 0000 0110
0000 0000 0000 0000 0000 0000 0000 0110
Results: 6

bit non ~ (inverse code) "Note: The highest number of positive digits in Java is 0, the highest negative number is 1, that is, the highest bit determines the sign"
~ ~
0000 0000 0000 0000 0000 0000 0000 0100
1111 1111 1111 1111 1111 1111 1111 1011

Decoding: First to take the reverse code, and then complement
0000 0000 0000 0000 0000 0000 0000 0100
0000 0000 0000 0000 0000 0000 0000 0101
Results: 5

Bit xor ^ (true and false for fake or false)
4^6
0000 0000 0000 0000 0000 0000 0000 0100
0000 0000 0000 0000 0000 0000 0000 0110
0000 0000 0000 0000 0000 0000 0000 0010
Results: 2

Signed Right Shift >> (if positive, high 0, negative, high 1)
-4>>2
1111 1111 1111 1111 1111 1111 1111 1100 Original code
1111 1111 1111 1111 1111 1111 1111 1111 Right shift, leftmost left out two digits by rule negative NULL fill 1
0000 0000 0000 0000 0000 0000 0000 0000 Decoding
0000 0000 0000 0000 0000 0000 0000 0001 complement (complement i.e. last + 1)
Results: 1


Signed left << (if positive, high fill 0, negative, high 1)
-4<<2
1111 1111 1111 1111 1111 1111 1111 1100 Original code
1111 1111 1111 1111 1111 1111 1111 0000 Left shift, right-most empty two-bit 0
0000 0000 0000 0000 0000 0000 0000 1111 Decoding
0000 0000 0000 0000 0000 0000 0001 0000 Complement
Results: 16

Unsigned Right Shift >>> (both positive and negative, high 0)
-4>>>2
1111 1111 1111 1111 1111 1111 1111 1100 Original code
0011 1111 1111 1111 1111 1111 1111 1111 Right shift (due to high-level all 0, so the result of >>> must be positive)
Results: 1073741823

Because the data type occupies a limited number of bytes, and the size of the displacement can be arbitrary, so there may be a displacement after the representation of the data type, so there is a provision:
If the int data type, and the displacement number is greater than 32 bits, then the displacement of the number of positions to 32 modulo, or displacement than the total number of meaningless. So 4>>32 and 4>>0 are equivalent.

If the long type, and the number of displacement is greater than 64 bits, then the displacement of the number of positions to 64 modulo, if not more than 64 bits do not have to modulo the number of digits.

If they are byte, char, short, they are first expanded to 32 bits, and then the rules are handled according to the int type.

Learning this, I think you might also ask, what is the use of bit arithmetic or what scenarios canApplicationto it. Because the operation efficiency of the bit operation is much higher than the direct subtraction of the number, the bit operation can be considered when the following scenario is required and the efficiency of the operation is higher. But the actual work, rarely used it, I do not know why very few people use it, I think it is more obscure, if it is used to perform some operations, the estimated written code readability will not be strong, after all, we write the code is not only for ourselves to see.
1. Determine if the int variable A is an odd or an even number
a&1 = 0 Even
a&1 = 1 Odd
2. Averaging, for example there are two int type variables x, y, first requires x+y sum, and then divided by 2, but it is possible that the result of X+y will exceed the maximum representation range of int, so the bitwise operation will come in handy.
(x&y) + ((x^y) >>1);
3. For a whole number greater than 0, judge if it is not 2 of several parties
((x& (x-1)) ==0) && (x!=0);
4. For example, there are two int type variable x, y, require the two digital exchange, bit operation implementation method: performance is absolutely efficient
x ^= y;
Y ^= x;
x ^= y;
5. Seek absolute value
int abs (int x)
{
int y;
y = x >> 31;
Return (x^y)-y; Or: (x+y) ^y
}
6. Modulo operation, using bit operation to achieve:
A% (2^n) equivalent to A & (2^n-1)
7. Multiplication operation is implemented by bit operation
A * (2^n) equivalent to a << n
8. Divide operations into bitwise operations
A/(2^n) equivalent to a>> n
9. Seek the opposite number
(~x+1)
2 A% is equivalent to A & 1

Wait a minute
And of course, the Bulls. Use bit arithmetic to implement permission control, encryptiontechnology, inside the mystery unfathomable Ah! Ha ha.

Original link: http://www.52ij.com/jishu/102.html

Java bit Operations Summary: bit operation uses a wide range of "turn"

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.