Go Bitwise Operators

1. Introduction

Bitwise operators perform operations on the binary representations of integers in Go. These operations are performed bit by bit, hence the name. In this post, we will look at the various bitwise operators provided by Go and see an example of how they can be used to manipulate data at the bit level.

Definition

Bitwise operators are used to manipulate individual bits of integer data types. Go provides several bitwise operators: & (bitwise AND), | (bitwise OR), ^ (bitwise XOR), &^ (bit clear), << (left shift), and >> (right shift).

2. Program Steps

1. Use the bitwise AND operator to compare two numbers.

2. Use the bitwise OR operator to combine two numbers.

3. Use the bitwise XOR operator to find bits that are different.

4. Use the bit clear operator to turn off bits in a number.

5. Use the left shift operator to shift bits to the left.

6. Use the right shift operator to shift bits to the right.

3. Code Program

package main

import "fmt"

func main() {
	// Define two integers for bitwise operations
	a := 9  // binary: 1001
	b := 10 // binary: 1010

	// Step 1: Bitwise AND
	andResult := a & b // Will be 1000 (8 in decimal)

	// Step 2: Bitwise OR
	orResult := a | b // Will be 1011 (11 in decimal)

	// Step 3: Bitwise XOR
	xorResult := a ^ b // Will be 0011 (3 in decimal)

	// Step 4: Bit clear
	andNotResult := a &^ b // Will be 0001 (1 in decimal)

	// Step 5: Left shift
	leftShift := a << 1 // Shifts all bits of 'a' left by 1, resulting in 18 (10010 in binary)

	// Step 6: Right shift
	rightShift := b >> 1 // Shifts all bits of 'b' right by 1, resulting in 5 (101 in binary)

	// Print the results
	fmt.Printf("Bitwise AND: %d & %d = %d\n", a, b, andResult)
	fmt.Printf("Bitwise OR: %d | %d = %d\n", a, b, orResult)
	fmt.Printf("Bitwise XOR: %d ^ %d = %d\n", a, b, xorResult)
	fmt.Printf("Bit clear: %d &^ %d = %d\n", a, b, andNotResult)
	fmt.Printf("Left shift: %d << 1 = %d\n", a, leftShift)
	fmt.Printf("Right shift: %d >> 1 = %d\n", b, rightShift)
}

Output:

Bitwise AND: 9 & 10 = 8
Bitwise OR: 9 | 10 = 11
Bitwise XOR: 9 ^ 10 = 3
Bit clear: 9 &^ 10 = 1
Left shift: 9 << 1 = 18
Right shift: 10 >> 1 = 5

Explanation:

1. package main - Declares the package name for the Go program.

2. import "fmt" - Imports the Format package for formatted output.

3. Variables a and b are initialized to 9 (1001 in binary) and 10 (1010 in binary), respectively.

4. andResult uses & to perform a bitwise AND on a and b.

5. orResult uses | to perform a bitwise OR on a and b.

6. xorResult uses ^ to perform a bitwise XOR on a and b.

7. andNotResult uses &^ to clear the bits in a where b has bits set to 1.

8. leftShift uses << to shift the bits in a one position to the left.

9. rightShift uses >> to shift the bits in b one position to the right.

10. The fmt.Printf function calls output the results, showing the decimal representations of the bitwise operations.

11. The output demonstrates the effect of each bitwise operation between the binary representations of the variables a and b.


Comments