🧠 Go Bitwise Operators – Perform Binary Calculations in Go (2025 Guide)
🧲 Introduction – What Are Bitwise Operators in Go?
Bitwise operators allow you to manipulate individual bits of integers directly. They’re powerful tools for low-level programming, such as setting flags, toggling bits, optimizing storage, or working with hardware and network protocols.
🎯 In this section, you’ll learn:
- The full list of bitwise operators in Go
- How each operator works at the binary level
- Examples with binary outputs
- Bit shifting and real-world use cases
🧮 List of Bitwise Operators in Go
Operator | Symbol | Description | Example (a=10 , b=7 ) | Binary Explanation | Output |
---|---|---|---|---|---|
AND | & | Bitwise AND | a & b | 1010 & 0111 = 0010 | 2 |
OR | ` | ` | Bitwise OR | `a | b` |
XOR | ^ | Bitwise XOR | a ^ b | 1010 ^ 0111 = 1101 | 13 |
AND NOT | &^ | Bit clear (AND NOT) | a &^ b | 1010 &^ 0111 = 1000 | 8 |
LEFT SHIFT | << | Shift bits left | a << 1 | 1010 → 10100 | 20 |
RIGHT SHIFT | >> | Shift bits right | a >> 1 | 1010 → 0101 | 5 |
📊 Bitwise Operation Example
package main
import "fmt"
func main() {
a := 10 // binary: 1010
b := 7 // binary: 0111
fmt.Println("a & b =", a & b) // 2
fmt.Println("a | b =", a | b) // 15
fmt.Println("a ^ b =", a ^ b) // 13
fmt.Println("a &^ b =", a &^ b) // 8
}
📤 Output:
a & b = 2
a | b = 15
a ^ b = 13
a &^ b = 8
🔀 Bit Shift Operators in Go
✅ Left Shift (<<
)
Multiplies a number by 2^n
x := 4 // binary: 0100
fmt.Println(x << 1) // Output: 8 (1000)
✅ Right Shift (>>
)
Divides a number by 2^n
x := 16 // binary: 10000
fmt.Println(x >> 2) // Output: 4 (0100)
🧠 Shift operations are fast alternatives to multiplication/division when working with powers of 2.
🧪 Real-World Example – Bit Flags
const (
Read = 1 << iota // 0001
Write // 0010
Exec // 0100
)
perm := Read | Write
fmt.Println(perm & Read != 0) // true
fmt.Println(perm & Exec != 0) // false
This pattern is commonly used to manage access control, feature flags, or configuration toggles.
🚫 Limitations & Tips
Rule or Tip | Explanation |
---|---|
Only works on integers | Cannot use with floats, strings, etc. |
Always check bit length | Use uint8 , int32 , etc., for control |
Use fmt.Printf("%b", num) to debug | Prints binary representation of a number |
&^ is Go-specific | Not present in many other languages |
📌 Summary – Recap & Next Steps
Bitwise operators in Go are incredibly useful for memory-efficient operations and low-level control. They are especially powerful in embedded, systems, and network programming.
🔍 Key Takeaways:
- Use
&
,|
,^
, and&^
for bit-level logic - Use
<<
and>>
for fast multiplication/division - Bitwise flags can simplify complex boolean configurations
- Only integers can be used with bitwise operators
⚙️ Next: Learn about Go Conditional Statements like if
, else
, and switch
to control program flow.
❓ FAQs – Bitwise Operators in Go
❓ What is &^
in Go?
✅ It’s the AND NOT operator. It clears the bits in the left operand where the right operand has 1
s.
❓ Can I use bitwise operators on floats or strings?
✅ No. Bitwise operations only work on integer types.
❓ How do I print a binary representation of a number?
✅ Use:
fmt.Printf("%b\n", 10) // Output: 1010
❓ What’s the difference between ^
and &^
?
✅ ^
is XOR (exclusive OR), while &^
is bit clear (AND NOT).
❓ Are bit shifts better than multiplication/division?
✅ Bit shifts are faster and more efficient only when dealing with powers of two.
Share Now :