Bitwise operators work directly on the binary representation of integer values, manipulating one bit at a time. In Go, these operators are essential for low-level programming, bit masks, flags, and efficient arithmetic (shifts are equivalent to multiplying/dividing by powers of two for non-negative integers). This guide covers the Go bitwise operators with clear binary demonstrations and runnable examples.Documentation Index
Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
Use this file to discover all available pages before exploring further.
- Bitwise AND (&)
- Bitwise OR (|)
- Bitwise XOR (^)
- Left shift (<<)
- Right shift (>>)

- Flags and bit masks (packing multiple boolean flags into one integer)
- Performance-sensitive arithmetic (shift-based multiply/divide by powers of two)
- Low-level protocol parsing, embedded and systems programming
| Operator | Meaning | Example |
|---|---|---|
| & | Bitwise AND — 1 if both bits are 1 | 12 & 25 // 8 |
| | | Bitwise OR — 1 if at least one bit is 1 | 12 | 25 // 29 |
| ^ | Bitwise XOR — 1 if bits differ | 12 ^ 25 // 21 |
| << | Left shift — shift bits left (multiply by 2^n for non-negative) | 212 << 1 // 424 |
| >> | Right shift — shift bits right (divide by 2^n for non-negative) | 212 >> 2 // 53 |

Note: In Go, ^ is also the unary bitwise NOT (complement) when used with a single operand (for example
^x flips all bits of x). In the examples above ^ is used as a binary XOR operator.Be careful with signed integers and right shifts: the fill behavior for the leftmost bits (sign extension vs. zero fill) depends on the type and implementation. For predictable behavior, use unsigned integers (
uint, uint32, uint64) when performing bit-level shifts.- The Go Programming Language Specification — Operators
- Kubernetes Documentation (for system-level use cases)
- Binary and Bitwise Operations — Wikipedia