Go Bitwise Operators

Introduction

Bitwise operators are used to perform operations at the bit level. These operators work on binary representations of integers and are useful for low-level programming tasks, such as working with hardware or optimizing performance. In this chapter, you will learn the different bitwise operators in Go, with examples for each type.

Bitwise Operators in Go

AND (&)

The bitwise AND operator compares each bit of two integers and returns a new integer. The bits in the result are set to 1 only if both corresponding bits in the operands are 1.

Syntax:

result = operand1 & operand2

Example:

package main

import "fmt"

func main() {
    var a uint = 5  // 0101 in binary
    var b uint = 3  // 0011 in binary
    var result uint = a & b
    fmt.Println("a & b:", result) // Output: a & b: 1 (0001 in binary)
}

OR (|)

The bitwise OR operator compares each bit of two integers and returns a new integer. The bits in the result are set to 1 if either of the corresponding bits in the operands is 1.

Syntax:

result = operand1 | operand2

Example:

package main

import "fmt"

func main() {
    var a uint = 5  // 0101 in binary
    var b uint = 3  // 0011 in binary
    var result uint = a | b
    fmt.Println("a | b:", result) // Output: a | b: 7 (0111 in binary)
}

XOR (^)

The bitwise XOR operator compares each bit of two integers and returns a new integer. The bits in the result are set to 1 if the corresponding bits in the operands are different.

Syntax:

result = operand1 ^ operand2

Example:

package main

import "fmt"

func main() {
    var a uint = 5  // 0101 in binary
    var b uint = 3  // 0011 in binary
    var result uint = a ^ b
    fmt.Println("a ^ b:", result) // Output: a ^ b: 6 (0110 in binary)
}

AND NOT (&^)

The bitwise AND NOT operator compares each bit of two integers and returns a new integer. The bits in the result are set to 1 only if the corresponding bit in the first operand is 1 and the corresponding bit in the second operand is 0.

Syntax:

result = operand1 &^ operand2

Example:

package main

import "fmt"

func main() {
    var a uint = 5  // 0101 in binary
    var b uint = 3  // 0011 in binary
    var result uint = a &^ b
    fmt.Println("a &^ b:", result) // Output: a &^ b: 4 (0100 in binary)
}

Left Shift (<<)

The left shift operator shifts the bits of the first operand to the left by the number of positions specified by the second operand. Bits shifted out on the left are discarded, and zero bits are shifted in on the right.

Syntax:

result = operand1 << operand2

Example:

package main

import "fmt"

func main() {
    var a uint = 5  // 0101 in binary
    var result uint = a << 1
    fmt.Println("a << 1:", result) // Output: a << 1: 10 (1010 in binary)
}

Right Shift (>>)

The right shift operator shifts the bits of the first operand to the right by the number of positions specified by the second operand. Bits shifted out on the right are discarded, and zero bits are shifted in on the left.

Syntax:

result = operand1 >> operand2

Example:

package main

import "fmt"

func main() {
    var a uint = 5  // 0101 in binary
    var result uint = a >> 1
    fmt.Println("a >> 1:", result) // Output: a >> 1: 2 (0010 in binary)
}

Combining Bitwise Operators

You can combine bitwise operators to perform complex bit-level operations.

Example:

package main

import "fmt"

func main() {
    var a uint = 5  // 0101 in binary
    var b uint = 3  // 0011 in binary

    var result uint = (a & b) | (a ^ b)
    fmt.Println("(a & b) | (a ^ b):", result) // Output: (a & b) | (a ^ b): 7 (0111 in binary)
}

Conclusion

Bitwise operators in Go are powerful tools for performing low-level operations on binary data. By understanding and using these operators?AND (&), OR (|), XOR (^), AND NOT (&^), left shift (<<), and right shift (>>)?you can manipulate bits efficiently and perform complex computations. These operators are essential for tasks that require direct manipulation of binary data, such as working with hardware or optimizing performance.

Leave a Comment

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

Scroll to Top