Scala

12 / 53

Scala - Operators

An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations.

Arithmetic Operators Relational Operators Logical Operators Bitwise Operators Assignment Operators

Examples with A = 35 and B = 15

Arithmetic Operators

+      Add                        (A + B) = 50
-      Subtract                   (A - B) = 20
*      Multiply                   (A * B) = 525
/      Divide                     (A / B) = 2
%      Modulus                    (A % B) = 5

Relational Operators

==       Equality check           (A == B) is not true
!=       Inequality check         (A != B) is true
>        Greater than check       (A > B) is true
<        Less than check          (A < B) is not true
>=       Greater than or equal    (A >= B) is true
<=       Less than or equal       (A <= B) is not true

Logical Operators(assume A = true, B = false)

&&       Logical AND              (A && B) is false
||       Logical OR              (A || B) is true
!        Logical NOT              !(A && B) is true

Bitwise Operators
Bitwise operator works on bits and performs bit by bit operation.

a   b   a & b   a | b   a ^ b
0   0   0       0       0
0   1   0       1       1
1   1   1       1       0
1   0   0       1       1

Assume if A = 60; and B = 13; now in binary format, they will be as follows ?

A   = 0011 1100
B   = 0000 1101
-----------------------
A&B = 0000 1100
A|B = 0011 1101
A^B = 0011 0001
~A  = 1100 0011

Assignment Operators

=    Simple assignment                  C = A + B
+=   Add AND assignment                 C += A     (same as C = C + A )
-=   Subtract AND assignment            C -= A     (same as C = C - A )
*=   Multiply AND assignment            C *= A     (same as C = C * A )
/=   Divide AND assignment              C /= A     (same as C = C / A )
%=   Modulus AND assignment             C %= A     (same as C = C % A )
<<=  Left shift AND assignment          C <<= 2    (same as C = C << 2)
>>=  Right shift AND assignment         C >>= 2    (same as C = C >> 2)
&=  Bitwise AND assignment              C &= 2     (same as C = C & 2 )
^=  bitwise exclusive OR and assignment C ^= 2     (same as C = C ^ 2)
|=  bitwise inclusive OR and assignment C |= 2     (same as C = C | 2)

No hints are availble for this assesment

Answer is not availble for this assesment

Loading comments...