Bitwise in Go

Kecci
4 min readJul 5, 2023

--

Bitwise operations are operations that manipulate individual bits of binary data at a low-level, rather than operating on the entire data. These operations treat each bit of a binary number as a separate entity and perform logical or arithmetic operations on them.

Bitwise Use Cases

Bitwise operations have various use cases, particularly in low-level programming, systems programming, and optimization tasks. Here are a few common use cases where bitwise operations are beneficial:

  1. Bit Manipulation
  2. Encoding and Decoding
  3. Optimizing Memory Usage
  4. Cryptography
  5. Performance Optimization
  6. Bit-Level Algorithms

These are just a few examples of use cases where bitwise operations can be applied. Bitwise operations provide fine-grained control over individual bits of binary data, allowing developers to manipulate, encode, optimize, or solve problems at a low-level bit representation.

Bitwise Operators

The common bitwise operators are: AND (&), OR (|), XOR (^), NOT (~), Left Shift (<<), Right Shift (>>).

Bitwise

A bit wise operator perform logical or arithmetic operations on them.

  • AND (&)
  • OR (|)
  • XOR (^)
  • NOT (~)

Implementations in Go /bitwise/bitwise.go:

package bitwise

// And performs the bitwise AND operation between two numbers.
func And(a, b uint8) uint8 {
return a & b
}

// Or performs the bitwise OR operation between two numbers.
func Or(a, b uint8) uint8 {
return a | b
}

// Xor performs the bitwise XOR operation between two numbers.
func Xor(a, b uint8) uint8 {
return a ^ b
}

// Not performs the bitwise NOT operation on a number.
func Not(a uint8) uint8 {
return ^a
}

Bitshift

A bit shift operator is a low-level operator that works on the individual bits of an integer. It takes two operands. One is the integer whose bits we want to shift. The other represents the number of shifts. The result of a bit shift is a new integer. Bit-shift operators are easy to use and execute quickly on modern multicore CPU systems. We use them to reduce memory usage and speed up execution.

  • LeftShift
  • RightShift
  • LeftCircularShift
  • RightCircularShift

Implementations in Go /bitshift/bitshift.go:

package bitshift

import (
"fmt"
)

// LeftShift perform a bitwise left shift
func LeftShift(num, shift int) int {
return num << shift
}

// RightShift perform a bitwise right shift
func RightShift(num, shift int) int {
return num >> shift
}

// LeftCircularShift perform bitwise left circular shift
// numBits is the number of bits in an integer. e.g., numBits := 8
func LeftCircularShift(num, shift, numBits int) (int, error) {
if numBits < shift {
return 0, fmt.Errorf("out of range shift(%d) of numBits(%d)", shift, numBits)
}
return (num << shift) | (num >> (numBits - shift)), nil
}

// RightCircularShift perform a bitwise right circular shift
// numBits is the number of bits in an integer. e.g., numBits := 8
func RightCircularShift(num, shift, numBits int) (int, error) {
if numBits < shift {
return 0, fmt.Errorf("out of range shift(%d) of numBits(%d)", shift, numBits)
}
return (num >> shift) | (num << (numBits - shift)), nil
}

Run Example

Implementation in Go /main.go:

package main

import (
"fmt"

"github.com/kecci/go-bitwise-operations/bitshift"
"github.com/kecci/go-bitwise-operations/bitwise"
)

func main() {
// ===================================================================
// BITWISE
// ===================================================================

a := uint8(12) // Binary: 00001100
b := uint8(10) // Binary: 00001010

// Bitwise AND
fmt.Printf("[Bitwise] AND: %08b\n", bitwise.And(a, b))

// Bitwise OR
fmt.Printf("[Bitwise] OR: %08b\n", bitwise.Or(a, b))

// Bitwise XOR
fmt.Printf("[Bitwise] XOR: %08b\n", bitwise.Xor(a, b))

// Bitwise NOT
fmt.Printf("[Bitwise] NOT: %08b\n", bitwise.Not(a))

println("=============================================================")

// ===================================================================
// BITSHIFT
// ===================================================================

num := 1 // Binary: 1010
shift := 31 // Shift Position: 31
numBits := 31 // N-bit

fmt.Printf("[Bitshift] Original number: %d\n", num)

// Bitshift Left shift by 31 shifts
fmt.Printf("[Bitshift] LeftShift number: %d (Shifts: %d)\n", bitshift.LeftShift(num, shift), shift)

// Bitshift Right shift by 31 shifts
fmt.Printf("[Bitshift] RightShift number: %d (Shifts: %d)\n", bitshift.RightShift(num, shift), shift)

// Bitshift Left circular shift by 31 shifts and 32-bit
leftCircular, err := bitshift.LeftCircularShift(num, shift, numBits)
if err != nil {
panic(err)
}
fmt.Printf("[Bitshift] LeftCircularShift number: %08b (Shifts: %d Bits: %d)\n", leftCircular, shift, numBits)

// Bitshift Right circular shift by 31 shifts and 32-bit
rightCircular, err := bitshift.RightCircularShift(num, shift, numBits)
if err != nil {
panic(err)
}
fmt.Printf("[Bitshift] RightCircularShift number: %08b (Shifts: %d Bits: %d)\n", rightCircular, shift, numBits)
}

Command: go run main.go

Output:

Original number: 1
LeftShift number: 2147483648 (Shifts: 31)
RightShift number: 0 (Shifts: 31)
LeftCircularShift number: 10000000000000000000000000000001 (Shifts: 31 Bits: 31)
RightCircularShift number: 00000001 (Shifts: 31 Bits: 31)

Conclusion

In conclusion, bitwise and bit shift operations are essential tools in low-level programming and optimization tasks, offering precise control over individual bits of binary data. With Go’s support for bitwise operators and bit shifting, developers can leverage these operations to manipulate bits, optimize memory usage, encode and decode data, and perform various bit-level algorithms. Whether it’s manipulating flags, optimizing memory, implementing cryptographic algorithms, or optimizing performance, bitwise and bit shift operations provide a powerful set of tools for working with binary data. By understanding these operations and their use cases, developers can unlock new possibilities in their Go programs, enabling them to tackle a wide range of challenges that involve fine-grained bit manipulation and optimization.

Full Code: https://github.com/kecci/go-bitwise-operations

--

--

Kecci
Kecci

Written by Kecci

Coders + Librarian @Github

No responses yet