Skip to content

Operators in Different Languages (ตัวดำเนินการในภาษาต่างๆ)

Arithmetic Operators (ตัวดำเนินการทางคณิตศาสตร์)

js
let a = 10, b = 3;
console.log(a + b); // Addition 13
console.log(a - b); // Subtraction 7
console.log(a * b); // Multiplication 30
console.log(a / b); // Division 3.333...
console.log(a % b); // Modulus 1
console.log(a ** b); // Exponentiation 1000
rust
fn main() {
    let a = 10;
    let b = 3;
    
    println!("{}", a + b);  // Addition 13
    println!("{}", a - b);  // Subtraction 7
    println!("{}", a * b);  // Multiplication 30
    println!("{}", a / b);  // Division 3 (integer division)
    println!("{}", a % b);  // Modulus 1
    println!("{}", a.pow(b as u32));  // Exponentiation 1000
}
python
a, b = 10, 3
print(a + b)   # Addition 13
print(a - b)   # Subtraction 7
print(a * b)   # Multiplication 30
print(a / b)   # Division 3.333...
print(a // b)  # Integer division 3
print(a % b)   # Modulus 1
print(a ** b)  # Exponentiation 1000
go
package main

import (
    "fmt"
    "math"
)

func main() {
    a, b := 10, 3
    
    fmt.Println(a + b)  // Addition 13
    fmt.Println(a - b)  // Subtraction 7
    fmt.Println(a * b)  // Multiplication 30
    fmt.Println(a / b)  // Division 3 (integer division)
    fmt.Println(a % b)  // Modulus 1
    fmt.Println(math.Pow(float64(a), float64(b)))  // Exponentiation 1000
}

Comparison Operators (ตัวดำเนินการเปรียบเทียบ)

js
let a = 10, b = 3;

console.log(a == b); // Equal to false
console.log(a != b); // Not equal to true
console.log(a > b); // Greater than true
console.log(a < b); // Less than false
console.log(a >= b); // Greater than or equal to true
console.log(a <= b); // Less than or equal to false
rust
fn main() {
    let a = 10;
    let b = 3;
    
    println!("{}", a == b);  // Equal to false
    println!("{}", a != b);  // Not equal to true
    println!("{}", a > b);   // Greater than true
    println!("{}", a < b);   // Less than false
    println!("{}", a >= b);  // Greater than or equal to true
    println!("{}", a <= b);  // Less than or equal to false
}
python
a, b = 10, 3

print(a == b)  # Equal to False
print(a != b)  # Not equal to True
print(a > b)   # Greater than True
print(a < b)   # Less than False
print(a >= b)  # Greater than or equal to True
print(a <= b)  # Less than or equal to False
go
package main

import "fmt"

func main() {
    a, b := 10, 3
    
    fmt.Println(a == b)  // Equal to false
    fmt.Println(a != b)  // Not equal to true
    fmt.Println(a > b)   // Greater than true
    fmt.Println(a < b)   // Less than false
    fmt.Println(a >= b)  // Greater than or equal to true
    fmt.Println(a <= b)  // Less than or equal to false
}

Logical Operators (ตัวดำเนินการตรรกะ)

js
let a = true, b = false;

console.log(a && b); // AND false
console.log(a || b); // OR true
console.log(!a); // NOT false
rust
fn main() {
    let a = true;
    let b = false;
    
    println!("{}", a && b);  // AND false
    println!("{}", a || b);  // OR true
    println!("{}", !a);      // NOT false
}
python
a, b = True, False

print(a and b)  # AND False
print(a or b)   # OR True
print(not a)    # NOT False
go
package main

import "fmt"

func main() {
    a, b := true, false
    
    fmt.Println(a && b)  // AND false
    fmt.Println(a || b)  // OR true
    fmt.Println(!a)      // NOT false
}

Assignment Operators (ตัวดำเนินการกำหนดค่า)

js
let x = 10;
x += 5; // x = x + 5 → 15
x -= 3; // x = x - 3 → 12
x *= 2; // x = x * 2 → 24
x /= 4; // x = x / 4 → 6
x %= 5; // x = x % 5 → 1
rust
fn main() {
    let mut x = 10;
    x += 5; // x = x + 5 → 15
    x -= 3; // x = x - 3 → 12
    x *= 2; // x = x * 2 → 24
    x /= 4; // x = x / 4 → 6
    x %= 5; // x = x % 5 → 1
    
    println!("{}", x); // 1
}
python
x = 10
x += 5  # x = x + 5 → 15
x -= 3  # x = x - 3 → 12
x *= 2  # x = x * 2 → 24
x /= 4  # x = x / 4 → 6.0 (becomes float in Python)
x = int(x)  # convert back to integer → 6
x %= 5  # x = x % 5 → 1

print(x)  # 1
go
package main

import "fmt"

func main() {
    x := 10
    x += 5  // x = x + 5 → 15
    x -= 3  // x = x - 3 → 12
    x *= 2  // x = x * 2 → 24
    x /= 4  // x = x / 4 → 6
    x %= 5  // x = x % 5 → 1
    
    fmt.Println(x)  // 1
}

Bitwise Operators (ตัวดำเนินการระดับบิต)

js
let a = 0b1010, b = 0b1100;

console.log((a & b).toString(2)); // AND 1000
console.log((a | b).toString(2)); // OR 1110
console.log((a ^ b).toString(2)); // XOR 110
console.log((~a).toString(2)); // NOT -1011 (2's complement)
console.log((a << 2).toString(2)); // Left shift 101000
console.log((a >> 1).toString(2)); // Right shift 101
rust
fn main() {
    let a = 0b1010;
    let b = 0b1100;
    
    println!("{:b}", a & b);   // AND 1000
    println!("{:b}", a | b);   // OR 1110
    println!("{:b}", a ^ b);   // XOR 110
    println!("{:b}", !a);      // NOT (depends on bit width)
    println!("{:b}", a << 2);  // Left shift 101000
    println!("{:b}", a >> 1);  // Right shift 101
}
python
a, b = 0b1010, 0b1100

print(bin(a & b)[2:])   # AND 1000
print(bin(a | b)[2:])   # OR 1110
print(bin(a ^ b)[2:])   # XOR 110
print(bin(~a))          # NOT -1011 (2's complement)
print(bin(a << 2)[2:])  # Left shift 101000
print(bin(a >> 1)[2:])  # Right shift 101
go
package main

import "fmt"

func main() {
    a, b := 0b1010, 0b1100
    
    fmt.Printf("%b\n", a & b)   // AND 1000
    fmt.Printf("%b\n", a | b)   // OR 1110
    fmt.Printf("%b\n", a ^ b)   // XOR 110
    fmt.Printf("%b\n", ^a)      // NOT (depends on bit width)
    fmt.Printf("%b\n", a << 2)  // Left shift 101000
    fmt.Printf("%b\n", a >> 1)  // Right shift 101
}

Caveats (ข้อควรระวัง)

  • Difference between / and // in Python (ความแตกต่างของ / และ // ใน Python)
  • String comparison in some languages (== vs .equals()) (การเปรียบเทียบ String ในบางภาษา)
  • Operator precedence (ลำดับความสำคัญของตัวดำเนินการ)