0
0
GoHow-ToBeginner · 3 min read

How to Use Bitwise Operators in Go: Syntax and Examples

In Go, bitwise operators like &, |, ^, &^, <<, and >> work on integer types to manipulate bits directly. Use these operators to perform AND, OR, XOR, clear bits, and shift bits left or right.
📐

Syntax

Bitwise operators in Go work on integer values and manipulate their bits directly. Here are the main operators:

  • &: bitwise AND
  • |: bitwise OR
  • ^: bitwise XOR (exclusive OR)
  • &^: bit clear (AND NOT)
  • <<: left shift bits
  • >>: right shift bits

All operators require integer operands.

go
var a, b uint = 12, 5

c := a & b   // bitwise AND
c = a | b    // bitwise OR
c = a ^ b    // bitwise XOR
c = a &^ b   // bit clear (AND NOT)
c = a << 2   // left shift by 2 bits
c = a >> 1   // right shift by 1 bit
💻

Example

This example shows how to use each bitwise operator on two numbers and prints the results in decimal and binary form.

go
package main

import (
	"fmt"
)

func main() {
	a := uint(12) // binary: 1100
	b := uint(5)  // binary: 0101

	fmt.Printf("a = %d (binary %04b)\n", a, a)
	fmt.Printf("b = %d (binary %04b)\n", b, b)

	and := a & b
	or := a | b
	xor := a ^ b
	andNot := a &^ b
	leftShift := a << 2
	rightShift := a >> 1

	fmt.Printf("a & b = %d (binary %04b)\n", and, and)
	fmt.Printf("a | b = %d (binary %04b)\n", or, or)
	fmt.Printf("a ^ b = %d (binary %04b)\n", xor, xor)
	fmt.Printf("a &^ b = %d (binary %04b)\n", andNot, andNot)
	fmt.Printf("a << 2 = %d (binary %08b)\n", leftShift, leftShift)
	fmt.Printf("a >> 1 = %d (binary %04b)\n", rightShift, rightShift)
}
Output
a = 12 (binary 1100) b = 5 (binary 0101) a & b = 4 (binary 0100) a | b = 13 (binary 1101) a ^ b = 9 (binary 1001) a &^ b = 8 (binary 1000) a << 2 = 48 (binary 00110000) a >> 1 = 6 (binary 0110)
⚠️

Common Pitfalls

Common mistakes when using bitwise operators in Go include:

  • Using bitwise operators on non-integer types causes compile errors.
  • Confusing ^ (XOR) with ^ as a unary operator for bitwise NOT (Go uses ^ as unary NOT, but it flips all bits, including sign bits).
  • For signed integers, right shift >> is arithmetic (sign-extended), which may cause unexpected results.
  • Not using parentheses properly when combining bitwise and other operators.

Example of a common mistake and fix:

go
package main

import "fmt"

func main() {
	var x int = 8
	var y int = 3

	// Wrong: mixing bitwise and arithmetic without parentheses
	// result := x & y + 1 // This adds 1 to y, not to (x & y)

	// Right:
	result := (x & y) + 1
	fmt.Println(result) // Output: 1
}
Output
1
📊

Quick Reference

OperatorDescriptionExample
&Bitwise AND12 & 5 = 4
|Bitwise OR12 | 5 = 13
^Bitwise XOR12 ^ 5 = 9
&^Bit clear (AND NOT)12 &^ 5 = 8
<<Left shift bits12 << 2 = 48
>>Right shift bits12 >> 1 = 6

Key Takeaways

Use bitwise operators on integer types to manipulate bits directly in Go.
Remember that &^ clears bits set in the second operand from the first operand.
Use parentheses to avoid precedence mistakes when mixing bitwise and arithmetic operators.
Right shift on signed integers is arithmetic and may keep the sign bit.
Bitwise operators are useful for low-level tasks like flags, masks, and performance optimizations.