100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Go

Operators in Go

Explore arithmetic, comparison, logical, and bitwise operators in Go, including the unique AND NOT operator.

BasicsBeginner9 min readJul 8, 2026
Analogies

Introduction

Go provides a familiar set of operators for arithmetic, comparison, and logical operations, plus a full suite of bitwise operators for low-level manipulation of integer values. Unlike some languages such as C++ or Python, Go does not support operator overloading, so operators always behave according to their built-in definitions for the operand types involved.

🏏

Cricket analogy: Like a fixed set of umpiring signals (six, four, wide, no-ball) that mean exactly the same thing in every match, Go's operators behave identically for every operand type, with no team allowed to redefine what a "six" signal means.

Syntax

go
// arithmetic
a + b, a - b, a * b, a / b, a % b

// comparison
a == b, a != b, a < b, a <= b, a > b, a >= b

// logical
a && b, a || b, !a

// bitwise
a & b, a | b, a ^ b, a &^ b, a << n, a >> n

Explanation

Arithmetic operators work as expected, with integer division truncating toward zero and % returning the remainder. Comparison operators return a bool and require both operands to be comparable types. Logical operators && (AND), || (OR), and ! (NOT) short-circuit evaluation and operate only on bool values. Bitwise operators act on integer types: & is AND, | is OR, ^ is XOR (or unary bitwise complement/NOT when used as a prefix), and the Go-specific &^ is the 'AND NOT' (bit clear) operator, which clears bits in the left operand wherever the corresponding bit in the right operand is 1. << and >> perform left and right bit shifts. Go deliberately omits operator overloading to keep operator behavior predictable and consistent across the language.

🏏

Cricket analogy: Like calculating overs as balls/6 which truncates toward zero (17 balls = 2 overs, not 2.83), while balls%6 gives the leftover deliveries, and a captain's if wicketsLeft > 0 && oversLeft > 0 short-circuits without checking overs if wickets are already zero.

Example

go
package main

import "fmt"

func main() {
	a, b := 12, 10 // 1100, 1010 in binary

	fmt.Println("a & b  =", a&b)  // AND
	fmt.Println("a | b  =", a|b)  // OR
	fmt.Println("a ^ b  =", a^b)  // XOR
	fmt.Println("a &^ b =", a&^b) // AND NOT (bit clear)
	fmt.Println("a << 1 =", a<<1) // left shift

	fmt.Println(7%3 == 1 && 10 > 5)
}

Output

go
a & b  = 8
a | b  = 14
a ^ b  = 6
a &^ b = 4
a << 1 = 24
true

Key Takeaways

  • Go supports arithmetic, comparison, logical, and bitwise operators, but no operator overloading.
  • && and || short-circuit and only work on bool operands.
  • &^ is Go's distinctive AND NOT (bit clear) operator, not found in many other languages.
  • << and >> perform bitwise left and right shifts on integers.
  • Integer division truncates toward zero; % gives the remainder.

Practice what you learned

Was this page helpful?

Topics covered

#Go#GoProgrammingStudyNotes#Programming#OperatorsInGo#Operators#Syntax#Explanation#Example#StudyNotes#SkillVeris