Java bit operations

Source: Internet
Author: User
Tags bitwise operators

Bitwise moving OPERATOR:

<Indicates left shift, and one left shift indicates that the original value is multiplied by 2.

Example: 3 <2 (3 is int type)
1) Convert 3 to a binary number 0000 0000 0000 0000 0000 0000 0000 0011,

2) Remove the two zeros at the upper (left) of the number. All the other digits are shifted to the left,

3) Fill in the two vacant positions at the low position (right side. The final result is 0000 0000 0000 0000 0000 0000 0000 1100,

Convert to 12 in decimal format.

Similarly,> indicates shift right. Shifts one digit right indicates Division 2.

 

Bitwise operation:

Bitwise operators include: and (&), non (~) , Or (|), exclusive or (^)

&: When the bitwise of both operands is 1, the result is 1; otherwise, the result is 0. Such as 1100 & 1010 = 1000
  

|: When the bitwise of the two operands has one side, the result is 1; otherwise, the result is 0. For example, 1100 | 1010 = 1110
  

~ : 0 to 1, 1 to 0
  

^: If the bits on both sides are different, the result is 1. Otherwise, the result is 0. For example, 1100 ^ 1010 = 0110.

 

Bitwise operations and bitwise moving operators:

The hashmap function is to quickly find the value through the key ". Next we will analyze the basic process of storing data in hashmap:

1. When you call put (Key, value), first obtain the hashcode of the key, int hash = key. hashcode ();

2. Run hash to get an int H.

Hash ^ = (hash >>> 20) ^ (hash >>> 12 );

Int H = hash ^ (hash >>> 7) ^ (hash >>> 4 );

Why is this operation necessary? This is what makes hashmap brilliant. Let's look at an example. A decimal number is 32768 (Binary 1000 0000 0000 ).
0000). After the calculation, the result is 35080 (Binary 1000 1001 0000 ).
1000 ). See it? Maybe there is nothing to see. Let's just give a number 61440 (Binary 1111 0000 0000
0000), the calculation result is 65263 (Binary 1111 1110 1110
1111), it should be obvious now,The purpose is to make "1" more even. The purpose of the hash is to make the distribution as even as possible..

3,
After H is obtained, the carrying capacity of H and hashmap (the default Carrying Capacity of hashmap is 16, which can be automatically extended. You can specify a length when constructing a hashmap.
Degree. The carrying capacity is the length of the described array .) Logic and operation,H &
(Length-1), the result is a positive number smaller than the length. We call this value index. In fact, this index is the value of the index to be inserted in the array.
Location.
The significance of the algorithm in step 1 is to obtain a uniform index, which is an improvement of hashtable. The algorithm in hashtable only sets the key
Division of hashcode and length, that is, hash %
Length, which may cause uneven index distribution. Note that the hashmap key can be null and its value is placed at the first position of the array.

4,
We use table [Index] to indicate the location where the found elements need to be stored. First, determine whether there are any elements at this position (this element is an internal class entity defined by hashmap,
The basic structure contains three classes: Key, value, and next pointing to the next entity. If not, an entity <K, V> object is created.
Insert at the position of table [Index], so that the insertion ends. If there is one, traverse one by one through the linked list to see if there is any existing key. If yes, replace it with the new value.
Change the old value. If no value exists, insert the entity in table [Index] and assign the entity originally located in table [Index] to the new
The next of the entity, so that the insertion ends.

 

Appendix: hashmap is a combination of list and linked list.

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

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

 

From http://www.iteye.com/topic/766461

Shift Operator

Java shift operators are essentially three types: <(left shift),> (shifts right with symbols), and> (shifts right without symbols ).

1. Left Shift Operator

Shift left operator <indicates the number of times that all bits of the specified value are moved left.

1) its general format is as follows:

Value <num

Num specifies the number of digits in which the value to be shifted is moved.

The rule of Left shift only remembers one point: discard the highest bit, 0 fill the second bit

If the number of digits to be moved exceeds the maximum number of digits of this type, the compiler will modulo the number of digits to be moved. For example, if the int type moves 33 bits, only 33% 32 = 1 bits are actually moved.

2) operation rules

In binary format, all numbers are moved to the right to the corresponding number of digits. The high position is removed (discarded), and the low position is filled with zero.

When the number of operations to the Left shift is int type, every 31st bits to be moved will be removed and discarded;

When the number of left-shifted operations is long, the first 63rd bits are removed and discarded.

When the number of left-shift operations is of the byte and short types, these types are automatically extended to the int type.

3) mathematical significance

If the number does not overflow, for positive and negative numbers, moving one to the left is equivalent to multiplying the 1 power of 2, and moving n to the left is equivalent to multiplying the N power of 2.

4) computing process:

Example: 3 <2 (3 is int type)

1) Convert 3 to a binary number 0000 0000 0000 0000 0000 0000 0000 0011,

2) Remove the two zeros at the upper (left) of the number. All the other digits are shifted to the left,

3) Fill in the two vacant positions at the low position (right side. The final result is 0000 0000 0000 0000 0000 0000 0000 1100,

Convert to 12 in decimal format.

The number of digits to be moved exceeds the maximum number of digits of this type,

If you move to a higher-order bit (31 or 63 bits), the value changes to a negative value. The following program illustrates this:

Java code
  1. // Left shifting as a quick way to multiply by 2.
  2. Public class multbytwo {
  3. Public static void main (string ARGs []) {
  4. Int I;
  5. Int num = 0 xffffffe;
  6. For (I = 0; I <4; I ++ ){
  7. Num = num <1;
  8. System. Out. println (Num );
  9. }
  10. }
  11. }
// Left shifting as a quick way to 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 program output is as follows:

536870908

1073741816

2147483632

-32

Note: N-bit binary, the highest bit is the symbol bit, so the value range is-2 ^ (n-1) -- 2 ^ (n-1)-1, so the modulo is 2 ^ (n-1 ).

2. Right Shift Operator

Right Shift Operator <indicates the number of times that all bits of the specified value are shifted to the right.

1) its general format is as follows:

Value> num

Num specifies the number of digits in which the value to be shifted is moved.

Remember only one point for the right shift rule: The symbol bit remains unchanged, and the symbol bit is added on the left.

2) operation rules:

In binary format, all numbers are moved to the right to the corresponding number of digits. The low position is removed (discarded). The high position is filled with the sign bit, that is, the positive number is supplemented with zero, and the negative number is supplemented with 1.

When the right shift operation is of the byte and short types, these types are automatically extended to the int type.

For example, if the value to be removed is a negative number, each right shift is supplemented with 1 on the left. If the value to be removed is a positive number, each right shift is supplemented with 0 on the left, this is called the sign extension (retain the sign extension ).

This operation is used to maintain the negative number.

3) mathematical significance

Shifts one to the right is equivalent to dividing 2, and shifts n to the right is equal to dividing by 2 to the Npower.

4) computing process

11> 2 (11 is int type)

1) 11 binary format: 0000 0000 0000 0000 0000 0000 0000 1011

2) remove the last two digits of the low position. Because the number is positive, it is set to zero at the high position.

3) the final result is 0000 0000 0000 0000 0000 0000 0000 0010.

Convert to 2 in decimal format.

35> 2 (35 is int type)

35 to binary: 0000 0000 0000 0000 0000 0000 0010 0011

Remove the last two digits of the low position: 0000 0000 0000 0000 0000 0000 0000 1000

Convert to decimal: 8

5) do not retain the symbols in the right shift.

Bitwise AND operation are performed on the right-shifted value and 0x0f, which can discard any symbol-bit extension so that the obtained value can be used as the subscript of the defined array, in this way, the hexadecimal characters represented by the corresponding array elements are obtained.

For example

Java code
  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. }
public class HexByte {public static public void main(String args[]) {char hex[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'' };byte b = (byte) 0xf1; System.out.println("b = 0x" + hex[(b >> 4) & 0x0f] + hex[b & 0x0f]);}} 

(B> 4) calculation process of & 0x0f:

The binary format of B is: 1111 0001

Four digits removed: 0000 1111

Bitwise and calculation: 0000 1111

Convert to 10 hexadecimal format: 15

B & 0x0f operation process:

The binary format of B is: 1111 0001

0x0f binary format: 0000 1111

Bitwise and calculation: 0000 0001

Convert to 10 hexadecimal format: 1

Therefore, the output of this program is as follows:

B = 0xf1

3. unsigned right shift

Unsigned right shift operator >>>

Its common format is as follows:

Value >>> num

Num specifies the number of digits in which the value to be shifted is moved.

The rules for unsigned right shift only remember one point:The symbol bit extension is ignored.

Unsigned right shift operator >>> only valid for 32-bit and 64-bit values

 

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.