Go Operators
Estimated reading: 3 minutes 29 views

🧠 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

OperatorSymbolDescriptionExample (a=10, b=7)Binary ExplanationOutput
AND&Bitwise ANDa & b1010 & 0111 = 00102
OR``Bitwise OR`ab`
XOR^Bitwise XORa ^ b1010 ^ 0111 = 110113
AND NOT&^Bit clear (AND NOT)a &^ b1010 &^ 0111 = 10008
LEFT SHIFT<<Shift bits lefta << 11010 → 1010020
RIGHT SHIFT>>Shift bits righta >> 11010 → 01015

📊 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 TipExplanation
Only works on integersCannot use with floats, strings, etc.
Always check bit lengthUse uint8, int32, etc., for control
Use fmt.Printf("%b", num) to debugPrints binary representation of a number
&^ is Go-specificNot 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 1s.

❓ 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 :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

Go Bitwise Operators

Or Copy Link

CONTENTS
Scroll to Top