scala运算符_Scala中带有示例的运算符

scala运算符

Scala运算符 (Scala operators)

An operator is a symbol that represents an operation that is to be performed in the program. Operators tell the compiler to perform a specific operation, each operator is associated with one and has a unique identification code. Operators play an important role in programming and they are used to make an expression that executes to perform a task. It defines that operation between operands, based on the operator their can be one, two or three operands.

运算符是表示要在程序中执行的操作的符号。 运算符告诉编译器执行特定操作,每个运算符都与一个关联,并且具有唯一的标识码。 运算符在编程中起着重要作用,它们被用来生成执行任务的表达式。 它定义了操作数之间的运算,基于它们的运算符可以是一个,两个或三个操作数。

Scala as a huge range of operator that can be used while programming in Scala. They are:

Scala是在Scala中进行编程时可以使用的大量运算符。 他们是:

  1. Arithmetic operators

    算术运算符

  2. Relational operators

    关系运算符

  3. Logical operators

    逻辑运算符

  4. Bitwise operators

    按位运算符

  5. Assignment operators

    赋值运算符

1)算术运算符 (1) Arithmetic operators)

For performing arithmetic operations in Scala, arithmetic operators are defined. The following arithmetic operators are defined in Scala,

为了在Scala中执行算术运算,定义了算术运算符。 Scala中定义了以下算术运算符,

OperatorDescription
Addition Operator (+)Adds two operands
12 + 45 = 57
Subtraction Operator (-)Subtracts second operator from first
23 - 4 = 19
Multiplication Operator (*)Multiplies the two operands
25 *6 = 150
Division Operator (/)Divides the first operator by second
124/4 = 31
Modulus Operator (%)Gives the remainder when the first operator is divided by the second
123 % 5 = 3
操作员 描述
加法运算符( + ) 加两个操作数
12 + 45 = 57
减法运算符( - ) 从第一个减去第二个运算符
23-4 = 19
乘法运算符( * ) 将两个操作数相乘
25 * 6 = 150
除法运算符( / ) 将第一个运算符除以第二个
124/4 = 31
模运算符( % ) 当第一个运算符除以第二个运算符时给出余数
123%5 = 3

2)关系运算符 (2) Relational operators )

A relational operator is used to define the comparison between the two operators. It is also known as a comparison operator. Following are relational Operators in Scala,

关系运算符用于定义两个运算符之间的比较。 也称为比较运算符。 以下是Scala中的关系运算符,

OperatorDescription
Equal to (==)Compares if the two operands are equal, if yes then outputs TRUE else FALSE
12 == 34 → FALSE
23 == (46/2) → TRUE
Not Equal to (!=)Compares if the two operands are not equal, if yes then outputs TRUE else FALSE
12 != 34 → TRUE
23 != (46/2) → FALSE
Greater than (>)Checks if operand 1 is greater than operand 2. If yes outputs true otherwise false
34 > 2 → TRUE
34 >334 → FALSE
Greater than equal to (>=)Checks if operand 1 is greater than or equal to operand 2. If yes outputs true otherwise false
34 >= 2 → TRUE
34 >=334 → FALSE
34 >= 34 → TRUE
Less than (<)Checks if operand 1 is smaller than operand 2. If yes outputs true otherwise false
34 < 2 → FALSE
34 < 334 → TRUE
Less than equal to (<=)Checks if operand 1 is smaller than or equal to operand 2. If yes outputs true otherwise false
34 <= 2 → FALSE
34 <= 334 → TRUE
34 <= 34 → TRUE
操作员 描述
等于( == ) 比较两个操作数是否相等,如果是,则输出TRUE,否则输出FALSE
12 == 34→否
23 ==(46/2)→是
不等于( != ) 比较两个操作数是否不相等,如果是,则输出TRUE,否则输出FALSE
12!= 34→是
23!=(46/2)→假
大于( > ) 检查操作数1是否大于操作数2。如果是,则输出true;否则,返回false
34> 2→是
34> 334→假
大于等于( > = ) 检查操作数1是否大于或等于操作数2。如果是,则输出true,否则返回false
34> = 2→是
34> = 334→假
34> = 34→是
小于( < ) 检查操作数1是否小于操作数2。如果是,则输出true;否则,返回false
34 <2→假
34 <334→是
小于等于( <= ) 检查操作数1是否小于或等于操作数2。如果是,则输出true;否则,返回false
34 <= 2→假
34 <= 334→是
34 <= 34→是

3)逻辑运算符 (3) Logical operators)

A logical Operator is used to combine the logical output (true / False ) or reverse to the logical output of a condition or operand. Scala defines the following logical operators,

逻辑运算符用于组合逻辑输出(true / False)或反转为条件或操作数的逻辑输出。 Scala定义了以下逻辑运算符,

OperatorDescription
And (&& or and)It outputs TRUE, if the logical value of both operands is TRUE, else outputs FALSE.
OR (|| or or)It outputs FALSE, if the logical value of both operands is FALSE, else outputs TRUE.
NOT (! or not)It reverses the output of the operand.
操作员 描述
和( &&或and ) 如果两个操作数的逻辑值为TRUE,则输出TRUE,否则输出FALSE。
或( ||或或 ) 如果两个操作数的逻辑值为FALSE,则输出FALSE,否则输出TRUE。
不( !或不 ) 它反转操作数的输出。

The results of and , or and, not operator based on different values of operand:

和 , 或和, 而不是基于不同操作数值的运算符的结果:

ABA && BA || B!A
TrueTrueTrueTrueFalse
TrueFalseFalseTrueFalse
FalseTrueFalseTrueTrue
FalseFalseFalseFalseTrue
一个 A && B A || 乙 !一个
真正 真正 真正 真正
真正 真正
真正 真正 真正
真正

4)按位运算符 (4) Bitwise operators)

It works on the bits of the operands. It operates on every bit of the operands to give output. The following are valid bitwise operators in Scala,

它适用于操作数的位。 它对操作数的每一位进行运算以提供输出。 以下是Scala中有效的按位运算符,

OperatorDescription
Bitwise AND (&)It takes every bit of both operands and performs AND operation to it.
2 & 6 = 2 -> 010 & 110 = 010
Bitwise OR (|)It takes every bit of both operands and performs OR operation to it.
2 | 6 = 6 -> 0010 | 0110 = 0110
Bitwise XOR (^)It takes every bit of both operands and performs XOR operation to it.
XOR operation gives 1 output when both operands are different.
2 ^ 6 = 4 -> 0010 ^ 0110 = 0100
Bitwise Left Shift (<<)It shifts left the bits of operand 1 by n bits; n is specified by operand 2.
12 << 3 = 96 -> 0000 1100 << 3 = 0110 0000
Bitwise Right Shift (>>)It shifts right the bits of operand 1 by n bits; n is specified by operand 2.
12 >> 3 = 129 -> 0000 1100 >> 3 = 1000 0001
Bitwise Right Shift zero fill (>>>)It shifts the bits of operand 1 by n bits; n is specified by operand 2. And fills the shifted bit by 0.
12 >>> 3 = 1 ->0000 1100 >>> 3 000 0001
Bitwise Complement (~)It takes every bit of the operand and reverses its bits.
~12 = 243 -> ~ (0000 1100) = 1111 0011
操作员 描述
按位与( & ) 它需要两个操作数的每一位,并对它执行AND操作。
2&6 = 2-> 010&110 = 010
按位或( | ) 它需要两个操作数的每一位并对其执行“或”运算。
2 | 6 = 6-> 0010 | 0110 = 0110
按位XOR( ^ ) 它需要两个操作数的每一位,并对它执行XOR操作。
当两个操作数不同时,XOR运算给出1输出。
2 ^ 6 = 4-> 0010 ^ 0110 = 0100
按位左移( << ) 它将操作数1的位左移n位; n由操作数2指定。
12 << 3 = 96-> 0000 1100 << 3 = 0110 0000
按位右移( >> ) 它将操作数1的位右移n位; n由操作数2指定。
12 >> 3 = 129-> 0000 1100 >> 3 = 1000 0001
按位右移零填充( >>> ) 它将操作数1的位移位n位; n由操作数2指定。并将移位后的位填充0。
12 >>> 3 = 1-> 0000 1100 >>> 3 000 0001
按位补码( 〜 ) 它占用操作数的每一位并反转其位。
〜12 = 243->〜(0000 1100)= 1111 0011

5)赋值运算符 (5) Assignment operators)

The assignment operator is used to assign value to a variable. The variable is at the left of the operator and value is at the right of the operator. The data type needs to be the same for both sides to avoid errors in the program.

赋值运算符用于为变量赋值。 变量在运算符的左侧,而值在运算符的右侧。 双方的数据类型必须相同,以避免程序出错。

The following are valid assignment operators in Scala,

以下是Scala中的有效赋值运算符,

OperatorDescription
Simple Assignment (=)It simply assigns the value at left to the variable at right.
a = 2432;
Add and assign (+=)It adds the left value to the right value.
c += 32 -> c = c + 32
Subtract and assign (-=)It subtracts the left value from the right value.
c -= 32 -> c = c - 32
Multiply and assign (*=)It multiplies the left value with the right value.
c *= 32 -> c = c * 32
Divide and assign (/=)It divides the left value from the right value.
c /= 32 -> c = c / 32
Remainder and assign (%=)It finds the modulus of to the right value divide by left value.
c %= 32 -> c = c % 32
Left Shift and assign (>>=)It shifts the bits of left operands by n , n defined by right operator and assigns this value to left operator.
i >>= 6 -> i = i >> 6
Right Shift and assign (<<=)It shifts the bits of right operands by n , n defined by right operator and assigns this value to left operator.
i <<= 6 -> i = i << 6
Bitwise and assignment (&=)It performs the bitwise and operation of left operand and right operand and assigns this value to left operator.
i &= 6 -> i = i & 6
Bitwise exclusive OR assignment (^=)It performs the bitwise exclusive OR operation of left operand and right operand and assigns this value to left operator.
i ^= 6 -> i = i & 6
操作员 描述
简单分配( = ) 它只是将左侧的值分配给右侧的变量。
a = 2432;
添加并分配( + = ) 它将左值添加到右值。
c + = 32-> c = c + 32
减去并赋值( -= ) 它从右边的值中减去左边的值。
c-= 32-> c = c-32
相乘并赋值( * = ) 它将左边的值与右边的值相乘。
c * = 32-> c = c * 32
划分并分配( / = ) 它将左值与右值相除。
c / = 32-> c = c / 32
剩余并分配( %= ) 求出右值除以左值的模数。
c%= 32-> c = c%32
左移并分配( >> = ) 它将左操作数的位移位n,由右运算符定义n,并将此值分配给左运算符。
我>> = 6->我=我>> 6
右移并分配( << = ) 它将右操作数的位移位n,由右运算符定义n,并将此值分配给左运算符。
我<< = 6->我=我<< 6
按位和赋值( &= ) 它执行左操作数和右操作数的按位和运算,并将此值分配给左运算符。
我&= 6->我=我&6
按位异或分配( ^ = ) 它执行左操作数和右操作数的按位异或运算,并将此值分配给左运算符。
i ^ = 6-> i = i&6

All the operators are applied in Scala as it is and you can use them easily but to avoid error data type of operands should be the same.

所有运算符都按原样应用在Scala中,您可以轻松地使用它们,但要避免错误的操作数数据类型应该相同。

翻译自: https://www.includehelp.com/scala/operators-with-examples-in-scala.aspx

scala运算符

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值