Operators, bits and comparisons

Go has the operators of C with fewer of its traps. Precedence is one short table, comparisons produce a boolean and are not an integer, & and &^ work on integers and on bit sets, and there is no ternary operator: the language has if for that.

Arithmetic and comparison

Integer division truncates, so a floating point division needs one operand converted first. The increment and decrement operators are statements, not expressions: a++ is a line of its own, and b := a++ does not compile, which removes a family of side effects from expressions.

package main import "fmt" func main() { a, b := 17, 5 fmt.Println("+ - * / %:", a+b, a-b, a*b, a/b, a%b) fmt.Println("float division:", float64(a)/float64(b)) ok := a > b && b != 0 fmt.Println("comparison and logic:", ok, a <= b, a == 17) s := "go" + "pher" fmt.Println("concatenation:", s, len(s)) a++ b-- fmt.Println("after ++ and --:", a, b) }

go run arith.go

+ - * / %: 22 12 85 3 2
float division: 3.4
comparison and logic: true false true
concatenation: gopher 6
after ++ and --: 18 4

&& and || short-circuit: the right operand is not evaluated when the left one decides the result. The ! operator negates a boolean, and there is no implicit conversion from an integer to one.

Bits

Six operators work on the bits of an integer: & and, | or, ^ exclusive or, &^ and-not, << and >> shifts. The unary ^x is the complement. A permission set is the classic use, and %04b prints the bits so the result is visible.

package main import "fmt" const ( Read = 1 << iota Write Exec ) func main() { perm := Read | Write fmt.Printf("perm=%03b\n", perm) fmt.Println("has read:", perm&Read != 0) fmt.Println("has exec:", perm&Exec != 0) fmt.Println("without write:", perm&^Write) var x uint8 = 0b1010 fmt.Printf("%04b << 1 = %04b\n", x, x<<1) fmt.Printf("%04b >> 1 = %04b\n", x, x>>1) fmt.Printf("^%04b = %04b\n", x, ^x) fmt.Printf("%04b ^ %04b = %04b\n", x, x, x^x) }

go run bits.go

perm=011
has read: true
has exec: false
without write: 1
1010 << 1 = 10100
1010 >> 1 = 0101
^1010 = 11110101
1010 ^ 1010 = 0000

perm&^Write clears a bit, perm|Exec sets one and perm&Read tests one. A shift count may be a signed integer and is checked at run time: a negative count panics, and a count at or above the width gives zero.

Precedence

Multiplication, shifts and bitwise and bind tightest; then addition, subtraction, or and xor; then comparisons; then &&; then ||. The table is short enough to learn, and parentheses are still cheaper than a mistake.

package main import "fmt" func main() { fmt.Println(2 + 3*4) fmt.Println((2 + 3) * 4) fmt.Println(1<<3 + 1) fmt.Println(1 << (3 + 1)) t, f := true, false fmt.Println(t || f && f) fmt.Println((t || f) && f) }

go run precedence.go

14
20
9
16
true
false

1<<3 + 1 is 1 << 4, because the shift binds tighter than the addition, and the two lines that differ only in parentheses print 9 and 16. The constants lesson used the same expression to build the ladder of byte sizes.

No mixed arithmetic

Go does not convert an integer to a string or a number to another width to make an expression work. The program below adds an untyped integer constant to a string, and the compiler refuses it at the operator.

package main import "fmt" func main() { x := 1 + "a" fmt.Println(x) }

go build mixed.go

# command-line-arguments
./mixed.go:6:7: invalid operation: 1 + "a" (mismatched types untyped int and untyped string)

Exercises

  1. Write a program that sets three flags, clears the middle one with &^ and prints the bits after every step.
  2. Compute 1 << 63 as an int64 and as a uint64. Which one is negative, and why?
  3. Replace if ok { return 1 }; return 0 with an expression. Which operator would a ternary need, and why does Go not have one?

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What is 17 / 5 in Go?

    Answer

    3

    Both operands are integers, so the division truncates.

  2. What does perm &^ Write do?

    Answer

    It clears the Write bits

    AND NOT keeps every bit of the left operand that is not set in the right one.

  3. What is ^uint8(0b1010)?

    Answer

    0b11110101

    The complement flips every bit of the 8-bit value.

  4. Which operators short-circuit?

    Answer

    && and ||

    The logical operators skip the right operand when the left one decides the result.

  5. What does this program print? package main import "fmt" func main() { fmt.Println(2 + 3*4) }

    Answer

    14

    Multiplication binds tighter than addition.

  6. What does this program print? package main import "fmt" func main() { fmt.Println(1<<3 + 1) }

    Answer

    9

    The shift binds tighter than +, so it is (1 << 3) + 1 = 9; the brackets in the next line ask for 1 << (3 + 1), which is 16.

  7. What does this program print? package main import "fmt" func main() { a := 5 a++ fmt.Println(a) }

    Answer

    6

    ++ is a statement that increments its operand by one.

  8. What does the compiler say about x := 1 + "a"?

    Answer

    invalid operation: 1 + "a" (mismatched types untyped int and untyped string)

    Go has no implicit conversion, so the two operands of + must have the same type.

  9. What is true || false && false?

    Answer

    true

    && binds tighter, so the expression is true || false.

  10. Which statement about % is true?

    Answer

    The remainder takes the sign of the dividend

    -7 % 2 is -1 in Go, unlike the mathematical modulo.

The code on this page was run with go 1.26.10 on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Article author: Arthur Isaev