# scala -- operator + bit operation + inverse code, complement code and original code

### 1. Arithmetic operator

#### 1.1 introduction to operators

The symbol used to splice variables or constants is called: operator, and the formula connected by operators is called: expression. We often use it in practical development

For example:

10 + 3 is an expression, and the + sign is an operator

Note: in Scala, operators are not only operators, but also functions

#### 1.2 classification of operators

• Arithmetic operator

• Assignment Operators

• Relational operator

• Logical operator

• Bitwise Operators

Note: there is no ternary operator in Scala, which is replaced by if else

#### 1.3 arithmetic operators

Arithmetic operators refer to symbols used for arithmetic operations. The following are commonly used:

operatorFunctional interpretation
+Plus sign, with 3 functions. 1) represents a positive number, 2) ordinary addition operation, 3) string splicing
-The minus sign has two functions. 1) represents a negative number and 2) ordinary subtraction operation
*A multiplier sign used to obtain the product of two data
/Division, used to obtain the quotient of two data
%Remainder (also called modulo) is used to obtain the remainder of two data

be careful:

1. Scala does not have the two arithmetic operators + + - which are different from Java

2. The result of integer division is still an integer. If you want to get a decimal, you must have floating point data

For example, the result of 10 / 3 is 3, and the result of 10 / 3.0 is 3.3333 (infinite loop)

3. String splicing with + sign: any type of data and string splicing will result in a new string

4. For the% operation, suppose to find the value of a% B, its underlying principle is actually: a - a/b * b

#### 1.4 code demonstration

Requirements: demonstrate common operations of arithmetic operators

Reference code:

```//Demo + operation
println(+3)
println(10 + 3)
println("hello" + 10)

//Demo - operation number
println(-5)
println(10 - 5)

//Demonstrate * operation
println(5 * 3)

//Demo / operation
println(10 / 3)
println(10 / 3.0)

//Demonstrate% (remainder) operation
println(10 % 3)     //The result is 1. Specific operation process: 10 - 10 / 3 * 3 = 10 - 3 * 3 = 1
println(10 % -3)    //The result is 1. The specific operation process is: 10 - 10 / - 3 * - 3 = 10 -- 3 * - 3 = 10 - 9 = 1
println(-10 % 3)   //The result is - 1. The specific operation process is: - 10 -- 10 / 3 * 3 = - 10 -- 3 * 3 = - 10 + 9 = - 1
```

### 2. Assignment operator

#### 2.1 general

Assignment operator refers to the symbol used for assignment operation. For example, assignment of a constant value, a variable value or even the execution result of a piece of code to a variable requires assignment operator

#### 2.2 classification

• There are two types of assignment operators

• Basic assignment operator

=It is the basic assignment operator. For example, var a:Int = 3 assigns the constant value 3 to variable a

• Extended assignment operator

+=, -=, *=, /=, %=

be careful:

1. The left side of the assignment operator must be: variable, not constant. For example: 3 = 5, this writing is wrong

2. As for the extended assignment operator, it actually performs the specified operation on the left data and the right data, and then assigns the result to the left

For example; a += 3 is to add the value of variable a and constant 3, and then assign the result to variable a

#### 2.3 code demonstration

```//Assign constant value 1 to variable a
var a:Int = 1			//Note: since the subsequent code will modify the value of variable a, variable a should be modified with var
//Add 3 to variable a, and then re assign the result to variable a
a += 3			//The final result of a is: a = 4
//Subtract 2 from variable a, and then re assign the result to variable a
a -= 2			//The final result of a is: a = 2
//Multiply variable a by 3, and then re assign the result to variable a
a *= 3			//The final result of a is: a = 6
//Divide variable a by 2, and then re assign the result to variable a
a /= 2			//The final result of a is: a = 3
//Carry out the remainder operation on variables A and 2, and then re assign the result to variable a
a %= 2			//The final result of a is: a = 1
```

### 3. Relational operators

#### 3.1 general

Relational operators refer to the symbols used for comparison operations, such as whether the data are equal, whether they are unequal, whether the data is 1 large or 2 large, etc

#### 3.2 classification

operatorFunctional interpretation
>It is used to judge whether the data in the front is larger than the data in the back
>=It is used to judge whether the data in front is greater than or equal to the data in back
<It is used to judge whether the data in the front is smaller than the data in the back
<=It is used to judge whether the front data is less than or equal to the rear data
==Used to judge whether two data are equal
!=Used to judge whether the two data are unequal

be careful:

1. Whether the relational expression is simple or complex, the final result must be a Boolean value, either true or false
2. Never write = = as =, otherwise the result may not be what you want

#### 3.3 code demonstration

```//Define two Int type variables A and B, and assign values of 3 and 5 respectively
var a:Int = 3
var b:Int = 5
//Judge whether a is greater than b and print the result
println(a > b)					//false
//Judge whether a is greater than or equal to b, and print the result
println(a >= 3)					//true
//Judge whether a is less than b and print the result
println(a < b)					//true
//Judge whether a is less than or equal to b, and print the result
println(a <= 3)					//true
//Judge whether a and b are unequal and print the results
println(a != b)					//true
//Judge whether a and b are equal and print the results
println(a == b)					//false
//If you write = = as =, you actually assign the value of variable b to variable a
println(a = b)					//The output result is a pair of parentheses "()", that is, there is no printed value
println(a)						//Print variable a again, and the print result is: 5
```

#### 3.4 extension of relational operators

Students who have studied Java will find that the usage of relational operators in Scala is the same as that in Java. Is there anything different from Java?

Requirement descriptionScala codeJava code
Compare data values==Or=equals() method
Compare reference values (address values)eq method==Or=

Example

There is a string "abc", and then create a second string. The value is: splice an empty string after the first string.

Then use to compare whether the two strings are equal, and then see whether their reference values are equal.

Reference code

```val s1 = "abc"
val s2 = s1 + ""
s1 == s2     //The result is: true, because the data value is compared
s1.eq(s2)	 //The result is: false, because the address value is compared
```

### 4. Logical operators

#### 4.1 General

Logical operator refers to the symbol used for logical operation. It can be simply understood as: combination judgment. For example, judge whether multiple conditions are met, or one of them is met, and even reverse a judgment result

#### 4.2 classification

operatorFunctional interpretation
&&Logic and require that all conditions are met (i.e. the result is true). Simple memory: if there is false, the whole is false
||Logical or, as long as any condition is satisfied, simple memory: if there is true, the whole is true
!Logical non, which is used for negation. That is, it used to be true, false after negation, false before, and true after negation

be careful:

1. Whether a logical expression is simple or complex, the final result must be a Boolean value, either true or false
2. In Scala code, you can't perform continuous inversion on a Boolean type of data, but it is possible in Java
• Namely:!! false, it will report an error. This writing method is not supported

#### 4.3 code demonstration

```//Equivalent to: false & & true
println(3 > 5 && 2 < 3)		//The result is: false
//We can abbreviate the code as:
//Logic and: if there is false, it is false as a whole
println(false && true)		//The result is: false
println(true && false)		//The result is: false
println(false && false)		//The result is: false
println(true && true)		//The result is: true

println(false || true)		//The result is: true
println(true || false)		//The result is: true
println(false || false)		//The result is: false
println(true || true)		//The result is: true

println(!false)				//The result is: true
println(!true)				//The result is: false
println(!!true)				//This writing will report an error. Scala does not support this writing method, but Java code supports this writing method
```

### 5. Bitwise operator

#### 5.1 bedding knowledge

To learn bit operators well, you must know three knowledge points:

2. What is size 8421
3. Calculation rules of original code, inverse code and complement of integers

Generally speaking, every few into one is the decimal system. For example, every two into one is the binary system, and every decimal one is the decimal system. The commonly used decimal systems are as follows:

BinaryThe data starts with 0b (both upper and lower case) and consists of numbers 0 and 10b10001001, 0b00101010
octal number systemThe data starts with 0 and consists of numbers 0 ~ 7064, 011
decimal systemThe data can be written directly, without special beginning, and is composed of numbers 0 ~ 910, 20, 333
hexadecimalThe data starts with 0x (both case and case), and is composed of numbers 0 ~ 9 and letters A-F (both case and case)0x123F, 0x66ABC

be careful:

For binary data, the first bit is called sign bit, 0 represents positive number and 1 represents negative number. Other bits are called numeric bits

For example, the result of 0b10001001 is a negative number, and the result of 0b0010010 is a positive number

8421 code is used to describe the relationship between binary bits and decimal data. It can help us quickly calculate the binary or decimal form of data

The corresponding relationship of 8421 code is as follows:

Binary bit 0 0 0 0 0 0 0 0

Corresponding decimal data 128 64 32 16 8 4 2 1

be careful:

```  1. Calculation rules:  Number of binary bits from right to left, Every one more, The corresponding decimal data is multiplied by 2.
2. Tips for binary and decimal conversion:

*  Binary to decimal:  Get the decimal data corresponding to the binary bit, Then add up.
* for example: 0b101 Corresponding decimal data calculation steps:  4 + 0 + 1 = 5
*  Decimal to binary:  Disassemble decimal data, See which numbers add up to it, Then mark it as binary.
* for example: 10 Corresponding binary data calculation steps: 10 = 8 + 2  =   0b1010
```
##### 5.1.3 calculation rules for the original inverse complement of integers

The so-called original inverse complement actually refers to binary data. The decimal data is converted into its corresponding binary data, which is the original code

Note: the bottom storage, operation and operation data of the computer are realized in the form of binary complement of data

• Positive number
• The original code, inverse code and complement of positive numbers are the same, and no special calculation is required
• negative
• Calculation rule of inverse code of negative numbers: the sign bit of the original code remains unchanged, and the value bit is reversed by bit (formerly 0, now 1, formerly 1, now 0)
• Complement calculation rule of negative number: inverse code + 1

#### 5.2 general

Bit operator refers to the fast operation of data values according to bits. It is only for integer data. Because the underlying storage, operation and operation of the computer are in the form of binary complement of data, and we will often deal with massive data in the future. In order to improve the computational efficiency, we can use bit operator to quickly modify data values

#### 5.3 classification

operatorFunctional interpretation
&Bitwise and, rule: 0 if there is 0, 1 if both are 1
|Bitwise OR, rule: 1 if 1, 0 if both are 0
^Bitwise XOR, rule: 0 for the same, 1 for the different
~Reverse by bit, rule: 0 becomes 1, 1 becomes 0
<<Shift left by bit, rule: each shift left is equivalent to multiplying the data by 2, for example: 2 < < 1, the result is 4
>>Shift right by bit, rule: every shift right is equivalent to dividing the data by 2, for example: 6 > > 1, and the result is 3

be careful:

1. Bitwise operators are only for integer data
2. The operator operates on the binary complement form of data
3. Tip: a number is XOR twice by the same number, and the value of the number remains unchanged. That is, 10 ^ 20 ^ 20, the result is still 10

#### 5.4 code demonstration

```//Define two variables a and b with initialization values of 3 and 5 respectively
val a = 3			//Binary data: 0000 0011
val b = 5			//Binary data: 0000 0101

//The result is 0000 0001, converted to decimal, and the result is 1
println(a & b)		//The print result is: 1

//The result is 0000 0111, converted to decimal, and the result is 7
println(a | b)		//The print result is: 7

//The result is 0000 0110, converted to decimal, and the result is 6
println(a ^ b)		//The print result is: 6

//Calculation process: 1111 1100 (complement) - > 1111 1011 (inverse) - > 1000 0100 (original) - > decimal data: - 4
println(~ a)		//The print result is: - 4

//Calculation process: 1000 0011 (- 3 original code) - > 1111 1100 (- 3 inverse code) - > 1111 1101 (- 3 complement) - > 0000 0010 (new complement after inversion) - > decimal data: 2
println(~ -3)		//The print result is: 2

//Calculation process: 0000 0011 (complement of 3) - > 0000 1100 (new complement) - > decimal data: 12
println(a << 2)		//The print result is: 12

//Calculation process: 0000 0011 (complement of 3) - > 0000 0001 (new complement) - > decimal data: 1
println(a >> 1)		//The print result is: 1

println(a ^ b ^ b)	//The print result is: 3
```

### 6. Case: exchange the values of two variables

#### 6.1 requirements

It is known that there are two Int type variables A and b with initialization values of 10 and 20 respectively. Please write code to exchange the values of variable a and variable b

That is, the final result is: a=20, b=10

Note: it is not allowed to directly write such codes as a = 20 and B = 10

#### 6.2 reference code

• Method 1: through arithmetic operators

```//Define two Int type variables a and b with initialization values of 10 and 20 respectively
var a = 10
var b = 20
//Assign the calculation results of variables A and b to variable a
a = a + b	//a = 30, b = 20
//Calculation and assignment
b = a - b	//a = 30, b = 10
a = a - b	//a = 20, b = 10
//Print results
println("a: " + a)		//a: 20
println("b: " + b)		//b: 10
```
• Method 2: by defining temporary variables

```//Define two Int type variables a and b with initialization values of 10 and 20 respectively
var a = 10
var b = 20
//Define the temporary variable temp and record the value of variable a
var temp = a			//a = 10, b = 20, temp = 10
//Assign the value of variable b to a
a = b					//a = 20, b = 20, temp = 10
//Assign the value of the temporary variable temp to b
b = temp				//a = 20, b = 10, temp = 10
//Print results
println("a: " + a)		//a: 20
println("b: " + b)		//b: 10
```
• Mode 3: implemented by bit operator

```//Define two Int type variables a and b with initialization values of 10 and 20 respectively
var a = 10
var b = 20
//Define the temporary variable temp and record the bit exclusive or values of variables a and b (this value does not need to be calculated)
var temp = a ^ b	//Namely: temp = 10 ^ 20
//Exchange variable values by bitwise XOR
a = a ^ temp		//Operation flow: a = a ^ temp = a ^ a ^ b = 10 ^ 10 ^ 20 = 20
b = b ^ temp		//Operation flow: b = b ^ temp = b ^ a ^ b = 20 ^ 10 ^ 20 = 10
//Print results
println("a: " + a)	//a: 20
println("b: " + b)	//b: 10
```

Posted by Scummy12 on Wed, 24 Nov 2021 12:37:04 -0800