Fundamentals 13 min read

Scala Operators: Arithmetic, Relational, Logical, Bitwise, and Assignment

This article provides a comprehensive overview of Scala's built-in operators, detailing arithmetic, relational, logical, bitwise, and assignment operators with descriptions, example tables, and runnable code snippets illustrating their usage and precedence in practice.

Big Data Technology & Architecture
Big Data Technology & Architecture
Big Data Technology & Architecture
Scala Operators: Arithmetic, Relational, Logical, Bitwise, and Assignment

An operator is a symbol that tells the compiler to perform a specific mathematical or logical operation. Scala includes a rich set of built‑in operators, which are grouped into arithmetic, relational, logical, bitwise, and assignment categories.

Arithmetic Operators

The following table lists Scala's arithmetic operators with descriptions and examples (assuming A = 10, B = 20).

Operator

Description

Example

+

Addition

A + B = 30

-

Subtraction

A - B = -10

*

Multiplication

A * B = 200

/

Division

B / A = 2

%

Modulo

B % A = 0

object Test {
   def main(args: Array[String]) {
      var a = 10;
      var b = 20;
      var c = 25;
      var d = 25;
      println("a + b = " + (a + b) );
      println("a - b = " + (a - b) );
      println("a * b = " + (a * b) );
      println("b / a = " + (b / a) );
      println("b % a = " + (b % a) );
      println("c % a = " + (c % a) );
   }
}

Running the program produces:

$ scalac Test.scala
$ scala Test
a + b = 30
a - b = -10
a * b = 200
b / a = 2
b % a = 0
c % a = 5

Relational Operators

Scala's relational operators compare values. Assuming A = 10, B = 20:

Operator

Description

Example

==

Equal

(A == B) → false

!=

Not equal

(A != B) → true

>

Greater than

(A > B) → false

<

Less than

(A < B) → true

>=

Greater or equal

(A >= B) → false

<=

Less or equal

(A <= B) → true

object Test {
   def main(args: Array[String]) {
      var a = 10;
      var b = 20;
      println("a == b = " + (a == b) );
      println("a != b = " + (a != b) );
      println("a > b = " + (a > b) );
      println("a < b = " + (a < b) );
      println("b >= a = " + (b >= a) );
      println("b <= a = " + (b <= a) );
   }
}

Output:

$ scalac Test.scala
$ scala Test
a == b = false
a != b = true
a > b = false
a < b = true
b >= a = true
b <= a = false

Logical Operators

Logical operators work on Boolean values (A = true, B = false):

Operator

Description

Example

&&

Logical AND

(A && B) → false

||

Logical OR

(A || B) → true

!

Logical NOT

!(A && B) → true

object Test {
   def main(args: Array[String]) {
      var a = true;
      var b = false;
      println("a && b = " + (a && b) );
      println("a || b = " + (a || b) );
      println("!(a && b) = " + !(a && b) );
   }
}

Output:

$ scalac Test.scala
$ scala Test
a && b = false
a || b = true
!(a && b) = true

Bitwise Operators

Bitwise operators manipulate individual bits. Example with A = 60 (0011 1100) and B = 13 (0000 1101):

A = 0011 1100
B = 0000 1101
A & B = 0000 1100   // 12
A | B = 0011 1101   // 61
A ^ B = 0011 0001   // 49
~A = 1100 0011      // -61 (two's complement)
A << 2 = 1111 0000  // 240
A >> 2 = 0000 1111  // 15
A >>> 2 = 0000 1111 // 15

Operator

Description

Example

&

Bitwise AND

(a & b) → 12

|

Bitwise OR

(a | b) → 61

^

Bitwise XOR

(a ^ b) → 49

~

Bitwise NOT

(~a) → -61

<<

Left shift

(a << 2) → 240

>>

Right shift

(a >> 2) → 15

>>>

Unsigned right shift

(a >>> 2) → 15

object Test {
   def main(args: Array[String]) {
      var a = 60; // 0011 1100
      var b = 13; // 0000 1101
      var c = 0;
      c = a & b; println("a & b = " + c);
      c = a | b; println("a | b = " + c);
      c = a ^ b; println("a ^ b = " + c);
      c = ~a;    println("~a = " + c);
      c = a << 2;println("a << 2 = " + c);
      c = a >> 2;println("a >> 2 = " + c);
      c = a >>> 2;println("a >>> 2 = " + c);
   }
}

Output:

$ scalac Test.scala
$ scala Test
a & b = 12
a | b = 61
a ^ b = 49
~a = -61
a << 2 = 240
a >> 2 = 15
a >>> 2 = 15

Assignment Operators

Scala supports simple and compound assignment operators:

Operator

Description

Example

=

Simple assignment

C = A + B

+=

Add then assign

C += A // C = C + A

-=

Subtract then assign

C -= A // C = C - A

*=

Multiply then assign

C *= A // C = C * A

/=

Divide then assign

C /= A // C = C / A

%=

Modulo then assign

C %= A // C = C % A

<<=

Left shift then assign

C <<= 2 // C = C << 2

>>=

Right shift then assign

C >>= 2 // C = C >> 2

&=

Bitwise AND then assign

C &= 2 // C = C & 2

^=

Bitwise XOR then assign

C ^= 2 // C = C ^ 2

|=

Bitwise OR then assign

C |= 2 // C = C | 2

object Test {
   def main(args: Array[String]) {
      var a = 10;
      var b = 20;
      var c = 0;
      c = a + b; println("c = a + b = " + c);
      c += a;    println("c += a = " + c);
      c -= a;    println("c -= a = " + c);
      c *= a;    println("c *= a = " + c);
      a = 10; c = 15; c /= a; println("c /= a = " + c);
      a = 10; c = 15; c %= a; println("c %= a = " + c);
      c <<= 2;  println("c <<= 2 = " + c);
      c >>= 2;  println("c >>= 2 = " + c);
      c &= a;   println("c &= 2 = " + c);
      c ^= a;   println("c ^= a = " + c);
      c |= a;   println("c |= a = " + c);
   }
}

Running this code yields:

$ scalac Test.scala
$ scala Test
c = a + b = 30
c += a = 40
c -= a = 30
c *= a = 300
c /= a = 1
c %= a = 5
c <<= 2 = 20
c >>= 2 = 5
c &= 2 = 0
c ^= a = 10
c |= a = 10

Operator Precedence

Operator precedence determines the order in which parts of an expression are evaluated. For example, in the expression x = 7 + 3 * 2, multiplication has higher precedence than addition, so the result is 13, not 20. A full precedence table is provided, listing groups from highest (parentheses) to lowest (comma).

Level

Operators

Associativity

1

() []

Left‑to‑right

2

! ~

Right‑to‑left

3

* / %

Left‑to‑right

4

+ -

Left‑to‑right

5

> >>> <<

Left‑to‑right

6

> >= < <=

Left‑to‑right

7

== !=

Left‑to‑right

8

&

Left‑to‑right

9

^

Left‑to‑right

10

|

Left‑to‑right

11

&&

Left‑to‑right

12

||

Left‑to‑right

13

= += -= *= /= %= >>= <<= &= ^= |=

Right‑to‑left

14

,

Left‑to‑right

Understanding these categories, examples, and precedence rules helps developers write correct and efficient Scala code.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

programmingbitwiseArithmeticScalaoperatorsassignmentLogical
Big Data Technology & Architecture
Written by

Big Data Technology & Architecture

Wang Zhiwu, a big data expert, dedicated to sharing big data technology.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.