Java bit Operations

Source: Internet
Author: User
Tags bitwise operators

Bitwise MOVE Operators:

<< to move left, move left one to indicate that the original value is multiplied by 2.

Example: 3 <<2 (3 for int type)
1) Convert 3 to binary number 0000 0000 0000 0000 0000 0000 0000 0011,
2) Move the digit high (left) to two 0, and the other numbers are shifted to the left by 2 bits,
3) Two vacancies in the low (right) 0. The resulting result is 0000 0000 0000 0000 0000 0000 0000 1100,
Conversion to decimal is 12.

The same,>> means moving right. Move right one represents except 2.

Bit operations:

Bitwise operators include: With (&), non (~), or (|), XOR (^)

&: When the bit of both operands is 1 o'clock, the result is 1, otherwise 0. such as 1100&1010=1000

| : When the bit on both sides of the operand has a side of 1 o'clock, the result is 1, otherwise 0. such as 1100|1010=1110

~:0 Change to 0

^: Both sides of the bit are not simultaneous, the result is 1, otherwise 0. such as 1100^1010=0110

A scene with bit operations and bit movement operators:

The function of

    HashMap is to quickly find "value" by "key". Below we analyze the basic flow of HASHMAP data:  
    1, when a put (Key,value) is called, first gets the hashcode,int hash of the key = Key.hashcode ( );  
    2, and then the hash through the operation to get an int h. 
Hash ^= (hash >>>) ^ (hash >>> 12) ;  
int h = hash ^ (hash >>> 7) ^ (hash >>> 4);  
Why did you go through such an operation? This is the genius of HashMap. Let's look at an example, a decimal number 32768 (binary 1000 0000 0000 0000), and the result after the above formula operation is 35080 (binary 1000 1001 0000 1000). Did you see it? Maybe it's not going to look like that, give me a number 61440 (binary 1111 0000 0000 0000), the result of the operation is 65263 (binary 1111 1110 1110 1111), it should be obvious now, it's meant to make "1" a little more uniform, The intent of the hash is to distribute the as evenly as possible.

3. After h, the carrying capacity of H and HashMap (HashMap's default load length is 16, can be automatically changed to longer. You can also specify a length when constructing the HashMap. This load is the length of the array described. For logic and arithmetic, H & (length-1), the result is a positive number smaller than length, which we call index. In fact, this index is the position of the value to be inserted in the array. the 2nd step that the meaning of the algorithm is to be able to obtain a uniform index, which is Hashtable improvement, Hashtable in the algorithm is just the key hashcode and length division to take the remainder, that is, hash% length, This may cause the index distribution to be uneven. Another point to note is that the HASHMAP key can be null, and its value is placed in the first position of the array.

4. We use Table[index] to indicate where the element that has been found needs to be stored. First determine if there are any elements in that position (this element is a class entity defined within the HASHMAP, the basic structure contains three classes, Key,value and next for the next entity), and without it, a Entity<k,v> object is created, Table[index] position is inserted, so the insertion ends, if any, by traversing through the list of links, to see if there is an existing key, some words replace the old value with the new value, and if not, insert the entity in Table[index] Assign the entity that was originally in the table[index] position to the new entity next, so that the insertion ends.

Attached: HashMap is a combination of list and linked lists.

Reference: http://www.cnblogs.com/highriver/archive/2011/08/15/2139462.html

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

Transfer from http://www.iteye.com/topic/766461

Shift Operators

The

Java shift operators are all about these three kinds of:<< (left), >> (with symbols right) and >>> (unsigned Right shift).   
1, left-shift operator  
left shift operator << the number of times that all bits of a specified value are shifted left.  
1) Its general format is as follows:  
value << num 
Num Specifies the number of digits to shift the value to move.  
The rule to move left only remembers one point: Discard the highest bit, 0 fill the lowest bit  
If the number of bits moved exceeds the maximum number of digits of that type, the compiler will model the number of bits moved. If you move 33 bits to the int type, you actually move only the 33%32=1 bit.  

2) arithmetic rules  
in binary form, all numbers are shifted to the left by the corresponding number of digits, the high position is shifted out (discarded), and the low vacancy is 0.  
When the left-hand operand is of type int, the 31st bit of each 1-bit shift is moved out and discarded;
when the left-hand operand is a long, the 63rd bit of each 1-bit move is removed and discarded. &NBSP
When the left-hand operand is a byte and a short type, these types are automatically expanded to type int.  

3) mathematical meaning  
In the case of numbers without overflow, for positive and negative, left one is the equivalent of multiplying by 2 of 1 times, left N is the equivalent of multiplying by 2 n  

4) calculation process: 
For example: 3 <<2 (3 for int)  
1) Convert 3 to binary number 0000 0000 0000 0000 0000 0000 0000 0011, 
2) Remove two 0 of the number high (left) The other numbers are shifted to the left by 2 bits,  
3) and two slots in the low (right) 0. The resulting result is 0000 0000 0000 0000 0000 0000 0000 1100, 
is converted to decimal is 12.  
The number of bits moved exceeds the maximum number of digits of the type,  
If you move into a higher-order bit (31 or 63 bits), the value becomes negative. The following procedure illustrates this:  

  1. Left shifting as a quick-to-multiply by 2.
  2. Public class Multbytwo {
  3. Public static void Main (String args[]) {
  4. int i;
  5. int num = 0xFFFFFFE;
  6. For (i=0; i<4; i++) {
  7. num = num << 1;
  8. SYSTEM.OUT.PRINTLN (num);
  9. }
  10. }
  11. }
Left shifting as a quick-multiply by 2.public class Multbytwo {public static void main (String args[]) {   int i;   int num = 0xFFFFFFE;    for (i=0; i<4; i++) {       num = num << 1;      SYSTEM.OUT.PRINTLN (num);}}}  



The output of the program is as follows:

536870908
1073741816
2147483632
-32
Note: n-bit binary, the highest bit is the sign bit, so the value range represented by -2^ (n-1)--2^ (n-1)-1, so the modulo is 2^ (n-1).

2. Right shift operator
Right-shift operator << causes all bits of the specified value to move to the right by the required number of times.
1) Its general format is as follows:
Value >> num
NUM Specifies the number of bits to shift the value to move.
The rule that moves right only remembers one point: the symbol bit is unchanged, the left side is the symbol bit

2) Operational rules:
In binary form, all numbers are shifted to the right by the corresponding number of digits, the lower position is removed (discarded), the high position of the empty fill sign bit, that is, positive 0, negative complement 1
These types are automatically expanded to int when the operands to the right move are of byte and short type.
For example, if the value to be removed is negative, each right shift is 1 on the left, and if the value to be moved is a positive number, each right shift is 0 on the left, which is called the sign bit extension (sign extension), which moves right

The symbol used to hold negative numbers during operation.


3) Mathematical significance
Move right one is equivalent to 2, and the right shift n is the equivalent of dividing by 2 of the n-th side.

4) Calculation process
One >>2 (11 for int type)
1) 11 in binary form: 0000 0000 0000 0000 0000 0000 0000 1011
2) Move the last two digits of the low position out, since the number is positive, so it is 0 at the high.
3) The final result is 0000 0000 0000 0000 0000 0000 0000 0010.
Conversion to decimal is 2.

>> 2 (35 for INT type)
35 conversion to binary: 0000 0000 0000 0000 0000 0000 0010 0011
Move the last two digits of the low: 0000 0000 0000 0000 0000 0000 0000 1000
Convert to decimal: 8

5) do not retain symbols when moving to the right
The values after the right shift are bitwise AND operation with 0x0f so that any sign bit extension can be discarded so that the resulting value can be used as the subscript for the definition array, resulting in the hexadecimal character represented by the corresponding array element.
For example

  1. Public class HexByte {
  2. Public static public void Main (String args[]) {
  3. Char hex[] = {
  4. ' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ',
  5. ' 8 ', ' 9 ', ' a ', ' B ', ' C ', ' d ', ' e ', ' F '
  6. };
  7. byte B = (byte) 0xf1;
  8. System.out.println ("b = 0x" + hex[(b >> 4) & 0x0f] + hex[b & 0x0f]);
  9. }
  10. }



(b >> 4) & 0x0f operation Process:
The binary form of B is: 1111 0001
4-digit number is removed: 0000 1111
Bitwise AND operations: 0000 1111
to 10 in binary form: 15

B & 0x0f Operation process:
The binary form of B is: 1111 0001
The binary form of the 0x0f is: 0000 1111
Bitwise AND operations: 0000 0001
to 10 in binary form: 1

Therefore, the output of the program is as follows:
b = 0xf1


3, unsigned right shift
Unsigned Right shift operator >>>
Its general format is as follows:
Value >>> num
NUM Specifies the number of bits to shift the value to move.
Unsigned right-shift rule remembers only one point: the symbol bit extension is ignored, and the top bit of the 0 complement
The unsigned Right shift operator >>> is only meaningful for 32-bit and 64-bit values

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