Detail the bit operator of Java

  • 2020-10-23 20:07:19
  • OfStack

The bit operator of Java is used to manipulate a single "bit" (bit) in the basic data type of an integer, that is, the decimal bit. And we know that bits are 0's and 1's, so bitwise operations are basic operations on the data. If a value of type char, byte, or short is shifted, it is converted to type int and then shifted

Bitwise operator for Java

The bitwise operator performs Boolean algebra on the bitwise of the two parameters, resulting in 1 result. This operator has the difference between ( & ), not (~), or (|), xor (^). We know that the unit "bits" (bit), that is, the decimal bits, are both 0 and 1, and the xor (^) may be a bit more complicated, in two operand bits, the same result is 0, the different result is 1. So the basic logic would look something like this.


1&1  -  1
1&0  -  0
~1  -  0
~0  -  1
1|1  -  1
1|0  -  1
1^0  -  1   ( 1 for 01 . 0 for 00 , so the result is 01 , or 1 ) 
1^1  -  0

Shift operator of Java

The shift operator of Java is nothing more than a shift to base 2.

for < < So we're going to move to the left, so we're going to move all of base 2 to the left one place, 0010, 0000 < < 1 is equal to 0100, 0000

for > > We're going to move to the right, so we're going to move all of base 2 to the right by 1 bit, 0010, 0000 > > 1 is equal to 0001, 0000.

Try the following example, where integer values are also converted to base 2:


class Test{
   public static void main(String[] args){
   int numInt1 = 3;
   int numInt2 = -3;
   System.out.println(numInt1<<1);
   System.out.println(numInt1>>1);
System.out.println(numInt2<<1);
 System.out.println(numInt2>>1);

  } 
 }
/*  The output is as follows (www.breakyizhan.com)
6   ---> 0000 0011<<1 ,  into 0000 0110
1   ---> 0000 0011>>1 ,  into 0000 0001...1 At the back of the 1 It's pushed out, so it's going to be zero 1
-6   --> 1111 1101<<1 ,  into 1111 1010  The not +1= 0000 0110  -6
-2   --> 1111 1101>>1 ,  into 1111 1110  The not +1= 0000 0010  -2 
*/

The $3 operator for Java, ES39en-ES40en

The 3-yuan operator, also known as the conditional operator, is best used for simple logical judgments because it is concise and efficient. Of course, you can replace the 3-yuan operator with ES43en-ES44en, but the 3-yuan operator will eventually generate a value. The 3-element operator expression is as follows:


boolean-exp? exp0: exp1

If the result of boolean-ES49en (Boolean expression) is true, the value of the expression exp0 is calculated without the value of exp1; if it is false, the value of the subsequent expression exp1 is calculated without the value of the previous exp0, and then the value of the calculated result is returned. Take a look at 1 for a detailed example:


public class sanyuantest{
  static int changeNo(int 1){
  return i < 5 ? i*100:i*10;
  }
 public static void main(String[] args){
 println(changeNo(3));
 println(changeNo(6));
 }
}
/* The output is as follows (www.breakyizhan.com)
300
60
*/

The above code can also be implemented as ES59en-ES60en, although some people think that ES61en-ES62en makes the code look more logical. But using 3-element expressions makes the code much cleaner.

conclusion


Related articles: