java shift operator: << (shift left), >> (shift right with sign) and a >>> (unsigned shift).

 1, left-shift operator

  Left shift operator << specified value all bits are left a predetermined number of times.

  1) It is common format is as follows:

  value << num

  num specify the value of the median value shift movement.

  The rule only remember one thing left: to discard the highest level, 0 the lowest level up

  If the number of mobile exceeds the maximum number of digits of the type, the number of bits the compiler will move modulo. The type of int 33 moves, in fact, moving only 332 = 1.

  2) operational rules

  In binary digital form all the corresponding bits shifted to the left, the upper removed (discarded), the lower in zeros.

  When the left operand of type int, the movement of each one of its first 31 should be removed and discarded;

  When the left operand of type long, the movement of each one of its first 63 should be removed and discarded.

  When the operand is shifted left byte and the type of short time, these types will be automatically promoted to int.

  3) mathematical sense

  Under the premise of no digital overflow, for positive and negative numbers, the left one are the equivalent of 1 multiplied by 2 to the power of n-bit left shift is equivalent multiplied by 2 to the power of n

  4) Calculation:

  For example: 3 << 2 (3 to int)

  1) to 3 to a binary number 0000 0000 0000 0000 0,000,000,000,000,011,

  2) to the digital high (left side) zero out of two, the other numbers are left pan 2,

  3) low zero padding in two seats (right). The final result is 0,000,000,000,000,000 0,000,000,000,001,100,

  Converted to decimal is 12.

  Digit movement exceeds the maximum number of bits that type,

  If you moved into the high-order bit (31 or 63), then the value will become negative. The following program illustrates this point:

  Java Code

  // 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 output of the program is as follows:

  536870908

  1073741816

  2147483632

  -32

  Note: n binary bits, the most significant bit is the sign bit, and therefore represents a numerical range -2 ^ (n-1) --2 ^ (n-1) -1, so the modulo 2 ^ (n-1).

  2, right-shift operator

  Right-shift operator << all bits in the specified values ​​are predetermined number of times right.

  1) It is common format is as follows:

  value >> num

  num specify the value of the median value shift movement.

  The rule only remember one thing right: the sign bit the same, left to make up the sign bit

  2) calculation rules:

  In binary digital form all the corresponding bits rightward, low removed (discarded), the high vacancy complement sign bit, i.e., the number of positive zero padding, negative S.1

  When the operand is shifted right byte or short type, these types will be automatically promoted to int.

  For example, if you want to remove the negative value, each time the right to the left are up 1, if you want to remove the value is positive, the left every time in the right complement 0, this is called sign extension (sign bit Reserved ) (sign extension), making the right

  Negative sign for holding operation.

  3) mathematical sense

  In addition to a right equivalent to 2, right by n bits is equivalent to the power of n divided by 2.

  4) calculation process

  11 >> 2 (11 to int)

  Binary form 1) 11 is: 0,000,000,000,000,000 0,000,000,000,001,011

  2) the last two digits of the lower removed because the number is positive, zero padding so high.

  3) The end result is 0,000,000,000,000,000 0,000,000,000,000,010.

  2 is converted to decimal.

  35 >> 2 (35 to int)

  35 is converted to binary: 0,000,000,000,000,000 0,000,000,000,100,011

  The last two numbers of low removal: 0,000,000,000,000,000 0,000,000,000,001,000

  Converted to decimal: 8

  5) does not retain the sign in the right out

  The value 0x0f perform a bitwise right shift, which can discard any sign extension, so that the resulting value may be defined as a subscript of an array, whereby the array element corresponding to the character hexadecimal representation.

  E.g

  Java Code

  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) & 0x0f calculation process:

  b binary form: 11,110,001

  4 digits are removed: 1111 1111

  Bitwise and operations: 0000 1111

  Converted to decimal form: 15

  b & 0x0f calculation process:

  b binary form: 11,110,001

  0x0f binary form as follows: 0000 1111

  Bitwise and operations: 0000 0001

  Converted to decimal form: 1

  Therefore, the output of the program are as follows:

  b = 0xf1

  3, unsigned shift right

  Unsigned right shift operator >>>

  Its general form is as follows:

  value >>> num

  num specify the value of the median value shift movement.

  Unsigned right shift rules only remember one thing: ignore the sign bit extensions, make up the most significant bit 0

  Unsigned right rule and right shift operation is the same, regardless of just numbers left when filling is positive or negative will be filled with 0, unsigned right shift is calculated only for the negative, because for this operation is a positive number does not make sense

  >>> unsigned right shift operator is only meaningful values ​​for 32-bit and 64-bit

 1, left-shift operator

  Left shift operator << specified value all bits are left a predetermined number of times.

  1) It is common format is as follows:

  value << num

  num specify the value of the median value shift movement.

  The rule only remember one thing left: to discard the highest level, 0 the lowest level up

  If the number of mobile exceeds the maximum number of digits of the type, the number of bits the compiler will move modulo. The type of int 33 moves, in fact, moving only 332 = 1.

  2) operational rules

  In binary digital form all the corresponding bits shifted to the left, the upper removed (discarded), the lower in zeros.

  When the left operand of type int, the movement of each one of its first 31 should be removed and discarded;

  When the left operand of type long, the movement of each one of its first 63 should be removed and discarded.

  When the operand is shifted left byte and the type of short time, these types will be automatically promoted to int.

  3) mathematical sense

  Under the premise of no digital overflow, for positive and negative numbers, the left one are the equivalent of 1 multiplied by 2 to the power of n-bit left shift is equivalent multiplied by 2 to the power of n

  4) Calculation:

  For example: 3 << 2 (3 to int)

  1) to 3 to a binary number 0000 0000 0000 0000 0,000,000,000,000,011,

  2) to the digital high (left side) zero out of two, the other numbers are left pan 2,

  3) low zero padding in two seats (right). The final result is 0,000,000,000,000,000 0,000,000,000,001,100,

  Converted to decimal is 12.

  Digit movement exceeds the maximum number of bits that type,

  If you moved into the high-order bit (31 or 63), then the value will become negative. The following program illustrates this point:

  Java Code

  // 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 output of the program is as follows:

  536870908

  1073741816

  2147483632

  -32

  Note: n binary bits, the most significant bit is the sign bit, and therefore represents a numerical range -2 ^ (n-1) --2 ^ (n-1) -1, so the modulo 2 ^ (n-1).

  2, right-shift operator

  Right-shift operator << all bits in the specified values ​​are predetermined number of times right.

  1) It is common format is as follows:

  value >> num

  num specify the value of the median value shift movement.

  The rule only remember one thing right: the sign bit the same, left to make up the sign bit

  2) calculation rules:

  In binary digital form all the corresponding bits rightward, low removed (discarded), the high vacancy complement sign bit, i.e., the number of positive zero padding, negative S.1

  When the operand is shifted right byte or short type, these types will be automatically promoted to int.

  For example, if you want to remove the negative value, each time the right to the left are up 1, if you want to remove the value is positive, the left every time in the right complement 0, this is called sign extension (sign bit Reserved ) (sign extension), making the right

  Negative sign for holding operation.

  3) mathematical sense

  In addition to a right equivalent to 2, right by n bits is equivalent to the power of n divided by 2.

  4) calculation process

  11 >> 2 (11 to int)

  Binary form 1) 11 is: 0,000,000,000,000,000 0,000,000,000,001,011

  2) the last two digits of the lower removed because the number is positive, zero padding so high.

  3) The end result is 0,000,000,000,000,000 0,000,000,000,000,010.

  2 is converted to decimal.

  35 >> 2 (35 to int)

  35 is converted to binary: 0,000,000,000,000,000 0,000,000,000,100,011

  The last two numbers of low removal: 0,000,000,000,000,000 0,000,000,000,001,000

  Converted to decimal: 8

  5) does not retain the sign in the right out

  The value 0x0f perform a bitwise right shift, which can discard any sign extension, so that the resulting value may be defined as a subscript of an array, whereby the array element corresponding to the character hexadecimal representation.

  E.g

  Java Code

  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) & 0x0f calculation process:

  b binary form: 11,110,001

  4 digits are removed: 1111 1111

  Bitwise and operations: 0000 1111

  Converted to decimal form: 15

  b & 0x0f calculation process:

  b binary form: 11,110,001

  0x0f binary form as follows: 0000 1111

  Bitwise and operations: 0000 0001

  Converted to decimal form: 1

  Therefore, the output of the program are as follows:

  b = 0xf1

  3, unsigned shift right

  Unsigned right shift operator >>>

  Its general form is as follows:

  value >>> num

  num specify the value of the median value shift movement.

  Unsigned right shift rules only remember one thing: ignore the sign bit extensions, make up the most significant bit 0

  Unsigned right rule and right shift operation is the same, regardless of just numbers left when filling is positive or negative will be filled with 0, unsigned right shift is calculated only for the negative, because for this operation is a positive number does not make sense

  >>> unsigned right shift operator is only meaningful values ​​for 32-bit and 64-bit

Guess you like

Origin www.cnblogs.com/jpfss/p/11514252.html