Sling Academy
Home/Golang/Using Bitwise Operations to Manipulate Binary Numbers in Go

Using Bitwise Operations to Manipulate Binary Numbers in Go

Last updated: November 24, 2024

Bitwise operations are a crucial part of computer science and programming. They allow you to perform operations on binary numbers at the bit level, which can lead to more efficient algorithms and processes. This article will guide you through using bitwise operations in Go, also known as Golang, offering code examples from basic to advanced levels.

Basic Bitwise Operations

In Go, you have at your disposal a set of bitwise operators which include:

  • AND &
  • OR |
  • XOR ^
  • AND NOT &^
  • Left Shift <<
  • Right Shift >>

Bitwise AND

The bitwise AND operator & compares each bit of two numbers. It sets the bit, at each position, to 1 only when both bits in that position are also 1.

package main

import "fmt"

func main() {
    a := 12 // 1100 in binary
    b := 25 // 11001 in binary
    
    result := a & b
    fmt.Printf("%d & %d = %d\n", a, b, result) // 1100 & 11001 = 8
}

Bitwise OR

The bitwise OR operator | compares each bit of two numbers. Sets the bit, at each position, to 1 if at least one of the bits is 1.

package main

import "fmt"

func main() {
    a := 12 // 1100 in binary
    b := 25 // 11001 in binary
    
    result := a | b
    fmt.Printf("%d | %d = %d\n", a, b, result) // 1100 | 11001 = 29
}

Intermediate Bitwise Operations

Let’s dive a bit deeper into some more complex uses of bitwise operations.

Bitwise XOR

The XOR operator ^ compares each bit of two numbers. It sets the bit, at each position, to 1 if only one of the bits is 1 (but not both).

package main

import "fmt"

func main() {
    a := 12 // 1100 in binary
    b := 25 // 11001 in binary
    
    result := a ^ b
    fmt.Printf("%d ^ %d = %d\n", a, b, result) // 1100 ^ 11001 = 21
}

Bitwise AND NOT

The AND NOT operation &^ is another useful operation, equivalent to clearing bits.

package main

import "fmt"

func main() {
    a := 12 // 1100 in binary
    b := 25 // 11001 in binary
    
    result := a &^ b
    fmt.Printf("%d &^ %d = %d\n", a, b, result) // 1100 &^ 11001 = 4
}

Advanced Bitwise Operations

In the advanced section, let's explore bit shifts:

Left Shift

The left shift operator << moves bits to the left and fills vacancies with zero bits, as demonstrated below.

package main

import "fmt"

func main() {
    a := 12 // 1100 in binary
    
    result := a << 2
    fmt.Printf("%d << 2 = %d\n", a, result) // 1100 << 2 = 48
}

Right Shift

Similarly, the right shift operator >> shifts bits to the right, preserving the sign bit (for signed numbers) on the left:

package main

import "fmt"

func main() {
    a := 12 // 1100 in binary
    
    result := a >> 2
    fmt.Printf("%d >> 2 = %d\n", a, result) // 1100 >> 2 = 3
}

Conclusion

Understanding and utilizing bitwise operations can vastly improve the efficiency of your code. Whether you need to perform quick arithmetic, cryptography tasks, or run efficient data compression algorithms, mastering these operations will be of immense value. With this knowledge, you can now explore more complex bitwise patterns and use them effectively in your programming projects.

Next Article: Understanding Binary Floating Point Representation in Go

Previous Article: Parsing Hexadecimal and Binary Input Strings in Go

Series: Numbers and Math in Go

Golang

Related Articles

You May Also Like

  • How to remove HTML tags in a string in Go
  • How to remove special characters in a string in Go
  • How to remove consecutive whitespace in a string in Go
  • How to count words and characters in a string in Go
  • Relative imports in Go: Tutorial & Examples
  • How to run Python code with Go
  • How to generate slug from title in Go
  • How to create an XML sitemap in Go
  • How to redirect in Go (301, 302, etc)
  • Using Go with MongoDB: CRUD example
  • Auto deploy Go apps with CI/ CD and GitHub Actions
  • Fixing Go error: method redeclared with different receiver type
  • Fixing Go error: copy argument must have slice type
  • Fixing Go error: attempted to use nil slice
  • Fixing Go error: assignment to constant variable
  • Fixing Go error: cannot compare X (type Y) with Z (type W)
  • Fixing Go error: method has pointer receiver, not called with pointer
  • Fixing Go error: assignment mismatch: X variables but Y values
  • Fixing Go error: array index must be non-negative integer constant