go operators
package main
import (
"fmt"
)
func main() {
var a = 10
var b = 20
fmt.Println("a + b =", a+b) // Addition
fmt.Println("a - b =", a-b) // Subtraction
fmt.Println("a b =", ab) // Multiplication
fmt.Println("b / a =", b/a) // Division
fmt.Println("b % a =", b%a) // Modulus
var c = 5
fmt.Println("c << 2 =", c<<2) // Left shift
fmt.Println("c >> 2 =", c>>2) // Right shift
var d = true
var e = false
fmt.Println("d && e =", d && e) // Logical AND
fmt.Println("d || e =", d || e) // Logical OR
fmt.Println("!d =", !d) // Logical NOT
var f = 3
var g = 4
fmt.Println("f & g =", f&g) // Bitwise AND
fmt.Println("f | g =", f|g) // Bitwise OR
fmt.Println("f ^ g =", f^g) // Bitwise XOR
fmt.Println("^f =", ^f) // Bitwise NOT
}
Explanation:
- a + b
: Adds the values of variables a
and b
.
- a - b
: Subtracts the value of b
from a
.
- a * b
: Multiplies the values of a
and b
.
- b / a
: Divides the value of b
by a
.
- b % a
: Finds the remainder of b
divided by a
.
- c << 2
: Shifts the bits of c
to the left by 2 positions.
- c >> 2
: Shifts the bits of c
to the right by 2 positions.
- d && e
: Returns true if both d
and e
are true, otherwise false (Logical AND).
- d || e
: Returns true if either d
or e
is true, otherwise false (Logical OR).
- !d
: Returns the opposite of the value of d
(Logical NOT).
- f & g
: Performs a bitwise AND operation between f
and g
.
- f | g
: Performs a bitwise OR operation between f
and g
.
- f ^ g
: Performs a bitwise XOR operation between f
and g
.
- ^f
: Performs a bitwise NOT operation on f
.