The article directories

  • [% more than take]
  • [Logical operation]
  • [& bits and operations]
  • [| or computing]
  • [^ xor operation]
  • [~ inverse operation (bit non operation)]
  • [>> Signed right shift operation]
  • [>>> unsigned right shift operation]
  • [<< left shift operation]
  • [++ increment symbol]
  • [+ =]
  • [?]
  • [Extension: Binary conversion of negative numbers]

% more than take

Remainder of the left operand divided by the right operand

0%3 = 0 
1%3 = 1
2%3 = 2
3%3 = 0
4%3 = 1
Copy the code

To determine whether a number can be divisible by another number, the mod operation equals zero

Logical operations

Am&& : true only if both sides are true. If the left side is false, the right side is ignored (short-circuited and), i.e. the second operation is not judged

| | or operation: as long as there is a side is true is true Be ignored the left after operation is true, in the right (short circuit or)

Exercise: Enter the year number to determine if it is a leap year

Common leap year: the Gregorian calendar year is a multiple of 4, but not a multiple of 100, is a leap year (for example, 2004, 2020, etc.). Century leap year: Gregorian calendar years are numbered in hundreds and must be multiples of 400 to be a leap year (e.g. 1900 is not a leap year, 2000 is a leap year).

Turning the definition of a leap year into a program, the criteria for a leap year are: divisible by 4, but not by 100; It’s divisible by 400

XML code

<EditText
        android:id="@+id/editText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Input year"
        android:inputType="textPersonName" />

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="doClick"
        android:text="Average year/leap year" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
Copy the code

Java code

public void doClick(View view) {
        EditText et = findViewById(R.id.editText);
        Button bt = findViewById(R.id.button);
        TextView tv = findViewById(R.id.textView);

        int year = Integer.parseInt(et.getText().toString());
        if ((year % 4= =0 && year % 100! =0) || (year % 400= =0)) {
            tv.setText("Leap year");
        } else {
            tv.setText("Leap year"); }}Copy the code

Run the program and the result is as follows:

& bit and operation

The operation rules are as follows:

0&0=0
0&1=0
1&0=0
1&1=1
5&3=1
Copy the code

5&3 binary operation procedure 00000101 00000011&11111100000001

| or operation

If one of the two bits is 1, the result is 1, otherwise it is 0

5|1=5
Copy the code

5 | 1 binary operation process is as follows:

00000101

00000001 |

一一一一一一一

00000101

^ Xor operation

In the bits of the two operands, the result is 0 if they are the same, and 1 if they are different

The binary operation of 5^1=4 is as follows:

00000101 00000001 ^ 11111100000100

It can either be true for a Boolean, a to the b, only if ab is different

~ inverse operation (bit non operation)

If the bits are 0, the inverse is 1, and if the bits are 1, the inverse is 0

The binary operation of ~1=-2 is as follows:

00000001 ~ 111-11111110

>> Signed right shift operation

Moves the object to the left of the operator to the right of the number specified by the operator. Use the sign extension mechanism, that is, if the value is positive, 0 is added at the high level, and if the value is negative, 1 is added at the high level.

If 5>>2=1, the binary operation of moving 5 two bits to the right is as follows: 00000101 >>2 00000001 01 1111111100000001 Discard the extra two bits to the right, resulting in 1

>>> unsigned right shift operation

Moves the object to the left of the operator to the right of the number specified by the operator. The 0 expansion mechanism is adopted, that is to say, regardless of the positive and negative values, 0. 5>>>2=1 are added in the high position. The operation process is the same as above, in fact, the positive number moves right n bits, which is equivalent to the quotient divided by 2 to the n power, such as 5>>>2=1 that is 5 ÷ 22 =1

-5>>>2=1073741822 Please look at the final stretch) 11111111111111111111111111111011 > > > 2, 00111111111111111111111111111110, 11

00111111111111111111111111111110 is converted to a decimal or for 1073741822

<< left shift operation

5<<2= 200,000 0101 <<2 0001 0100 0001 0100 is converted to decimal 20

Well, if you move n to the left, that’s the same thing as multiplying by 2 to the n. Because if we look at shifts in decimal terms: >> Right shift by 1 is equal to dividing by 10, << left shift by 1 is equal to multiplying by 10. Similarly, binary >>1 is the same as dividing by 2, and <<1 is the same as multiplying by 2

Exercise: Split int into 4 byte integers

00000001, 00000010, 00000100, 00001000 a b c d

If int is 32 bits, split it into 4 bytes. If byte B is converted to byte B every 8 bits, a is converted to byte B by 24 bits. If byte C is converted to byte C by 8 bits, b is converted to byte D by 0 bits

XML code

    <EditText
        android:id="@+id/editText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Input integer"
        android:inputType="textPersonName" />

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="doClick"
        android:text="Split/Merge" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
Copy the code

Java code

public void doClick(View view) {
        EditText et = findViewById(R.id.editText);
        Button bt = findViewById(R.id.button);
        TextView tv = findViewById(R.id.textView);

        int a = Integer.parseInt(et.getText().toString());
        byte b1 = (byte) (a >> 24);
        byte b2 = (byte) (a >> 16);
        byte b3 = (byte) (a >> 8);
        byte b4 = (byte) (a >> 0);

        tv.setText("" + b1);
        tv.append("\n" + b2);
        tv.append("\n" + b3);
        tv.append("\n" + b4);

    }
Copy the code

Run the program and the result is as follows:

Int int int int int int int int

00000001 A 10000010 B 10000100 C 10001000 D

The final result is 00000001, 10000010, 00000100, 00001000 A B C D

Let’s define the variable r = 0

According to the operation rules in the previous chapter, the byte operation will first convert int A to int, and the sign bit is 0, so the left side will be filled with 24 0 00000000 00000000 00000000 00000001. Then and r or 00000001 00000000 00000000 00000000 00000000 00000000 00000000 00000000 | 111111111111111111111111 00000001 00000000 00000000 00000000 Re-assign the value to R

B goes to int, the sign bit is 1, so I have 24 ones on the left side 1111111111 11111111 11111111 10000010 because the final position is B, so I move 16 to the left, And then I take or with r 11111111 10000010 00000000 00000000 but when I do that, the A position is all 1’s, So B should move 24 bits to the left 10000010 00000000 00000000 00000000 00000000 00000000 and then move 8 bits to the right, and then take or with r, and the whole point of doing that is to get rid of the sign

00000000 10000010 00000000 00000000 00000001 00000000 00000000 00000000 111111111111111111111111 00000001 10000010 00000000 00000000 Re-assign the value to R

C to int, same procedure as B, First move to the right >>24 11111111 11111111 11111111 10000100 then move to the left <<24 10000100 00000000 00000000 00000000 >>>16 and R seek or 00000000 00000000 10000100 00000000 00000001 10000010 00000000 00000000 | 111111111111111111111111 00000001 10000010 10000100 00000000 Re-assign the value to R

D turn int, >>24 11111111 11111111 11111111 10001000 <<24 10001000 00000000 00000000 00000000 00000000 >>>24 00000000 00000000 00000000 10001000 00000001 10000010 10000100 00000000 | 111111111111111111111111 00000001 10000010 10000100 10001000 Re-assign the value to R

Add after the original Java code

/ * * * * 1, r = 0 * 2, define a variable b1 left 24, then moves to the right with no symbols or 0 * and r, the results to assign a value to r * 3, b2 left 24, then moves to the right with no symbols or 8 * and r, the results to assign a value to r * 4, b3 left 24, Then unsigned 16 bits * to the right and r or, the result is reassigned to r * 5, b4 to the left 24 bits, then unsigned 24 bits * to the right and r or, the result is reassigned to r */
        int r = 0;
        r = r | (b1 << 24 >>> 0);
        r = r | (b2 << 24 >>> 8);
        r = r | (b3 << 24 >>> 16);
        r = r | (b4 << 24 >>> 24);
        tv.append("\n" + r);
Copy the code

Run the program and the result is as follows:

++ increment symbol

A ++ and ++a are both increment operators. The difference is that the value of a is incremented at different times. A++ is evaluated first and then incremented. ++a indicates that the value is incremented first and then evaluated

int a = 10;
System.out.println(a++);// output 10; A increases to 11, printing using the original value of A 10;
System.out.println(a);// output 11; Because a has increased to 11;
Copy the code
int a = 10;
System.out.println(++a);// output 11; If a increases to 11, print 11 after a increases;
System.out.println(a);// output 11; A increases to 11;
Copy the code
int a = 10;
int b = a++;// Assign b the same value as a, and then a increments by 1
System.out.println(b);/ / output 10
System.out.println(a);/ / output 11
Copy the code
int a = 10;
a = a++;//a increases to 11; And then I reassign a to a, so A goes back to 10
System.out.println(a);/ / output 10
Copy the code

The last one, just to explain a little bit: first a++ is 10, then a increments, then a=11, and then assignment, so the final output of a is 10.

+ =

A plus b is equal to a plus b

Byte a = 3; a = a + 3; A is a byte type, but is automatically converted to an int during operation. Therefore, an int plus 3 is an int, and the assignment to the left byte is an error. So you have to write a =(byte) a + 3; It can also be written as follows to omit the strong type a+=3;

? :

Ternary operators (also called ternary operators) are often used to replace if-then-else statements of a certain type. The general syntax is result =

?

:

expression is a Boolean expression. Statement1 is executed if expression is true, statement3 is executed otherwise


Exercise: Find the maximum

XML code

3 input fields +1 button button and add doClick to button buttonCopy the code

Java code

public void doClick(View view) {
        EditText et1 = findViewById(R.id.editText1);
        EditText et2 = findViewById(R.id.editText2);
        EditText et3 = findViewById(R.id.editText3);
        Button bt = findViewById(R.id.button);
        TextView tv = findViewById(R.id.textView);

        int a = Integer.parseInt(et1.getText().toString());
        int b = Integer.parseInt(et2.getText().toString());
        int c = Integer.parseInt(et3.getText().toString());

        int max = a > b ? a : b;
        max = max > c ? max : c;
        tv.setText("The maximum is" + max);
    }
Copy the code

Run the program and the result is as follows:

Extension: binary conversion of negative numbers

So let’s see how we get the binary of -5: first of all, the binary of 5 we’re familiar with 0000, 0101 and then we get the inverse, which is the inverse of each digit: 1111, 1010 and then we add 1 to get the complement, which is 1111, 1011, right

Because int 32 bits, so we write is 11111111111111111111111111111011

Because we’ve always counted positive numbers, and we’ve always had 0’s in front of us, so it doesn’t matter if we don’t write all of them, it doesn’t affect the operation