How to resolve the algorithm Bitwise operations step by step in the Go programming language

Published on 12 May 2024 09:40 PM
#Go

How to resolve the algorithm Bitwise operations step by step in the Go programming language

Table of Contents

Problem Statement

Write a routine to perform a bitwise AND, OR, and XOR on two integers, a bitwise NOT on the first integer, a left shift, right shift, right arithmetic shift, left rotate, and right rotate. All shifts and rotates should be done on the first integer with a shift/rotate amount of the second integer. If any operation is not available in your language, note it.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Bitwise operations step by step in the Go programming language

This Go program demonstrates bitwise operations on two 16-bit integers (int16 data type):

  • Bitwise Logical Operations:

    • and (&): Performs a bitwise AND operation, resulting in 1 if both bits are 1, 0 otherwise.
    • or (|): Performs a bitwise OR operation, resulting in 1 if either bit is 1, 0 otherwise.
    • xor (^): Performs a bitwise XOR operation, resulting in 1 if only one bit is 1, 0 if both bits are 0 or 1.
    • not (~): Inverts all bits of the operand.
  • Logical Shifts:

    • shl (<<): Shifts the bits of the unsigned operand to the left by the specified distance, filling with zero bits.
    • shr (>>): Shifts the bits of the unsigned operand to the right by the specified distance, filling with zero bits.
  • Arithmetic Shifts:

    • las (<<): Shifts the bits of the signed operand to the left by the specified distance, preserving the sign bit.
    • ras (>>): Shifts the bits of the signed operand to the right by the specified distance, preserving the sign bit.
  • Rotations:

    • rol (<< and |): Rotates the bits of the operand to the left by the specified distance, wrapping the overflowed bits.
    • ror (>> and |): Rotates the bits of the operand to the right by the specified distance, wrapping the overflowed bits.

The main function calls the bitwise function with two example values, -460 and 6. The function prints out the bitwise operations performed on these values, formatted as 16-bit binary representations.

For negative operands, the logical shifts are performed on the unsigned version of the operand, as shift operations require an unsigned right operand. The arithmetic shifts, however, are performed on the signed operand, preserving the sign bit.

Source code in the go programming language

package main

import "fmt"

func bitwise(a, b int16) {
	fmt.Printf("a:   %016b\n", uint16(a))
	fmt.Printf("b:   %016b\n", uint16(b))

	// Bitwise logical operations
	fmt.Printf("and: %016b\n", uint16(a&b))
	fmt.Printf("or:  %016b\n", uint16(a|b))
	fmt.Printf("xor: %016b\n", uint16(a^b))
	fmt.Printf("not: %016b\n", uint16(^a))

	if b < 0 {
		fmt.Println("Right operand is negative, but all shifts require an unsigned right operand (shift distance).")
		return
	}
	ua := uint16(a)
	ub := uint32(b)

	// Logical shifts (unsigned left operand)
	fmt.Printf("shl: %016b\n", uint16(ua<<ub))
	fmt.Printf("shr: %016b\n", uint16(ua>>ub))

	// Arithmetic shifts (signed left operand)
	fmt.Printf("las: %016b\n", uint16(a<<ub))
	fmt.Printf("ras: %016b\n", uint16(a>>ub))

	// Rotations
	fmt.Printf("rol: %016b\n", uint16(a<<ub|int16(uint16(a)>>(16-ub))))
	fmt.Printf("ror: %016b\n", uint16(int16(uint16(a)>>ub)|a<<(16-ub)))
}

func main() {
	var a, b int16 = -460, 6
	bitwise(a, b)
}


  

You may also check:How to resolve the algorithm Quine step by step in the GAP programming language
You may also check:How to resolve the algorithm GSTrans string conversion step by step in the Java programming language
You may also check:How to resolve the algorithm Primality by Wilson's theorem step by step in the Ruby programming language
You may also check:How to resolve the algorithm Literals/Integer step by step in the NetRexx programming language
You may also check:How to resolve the algorithm Cartesian product of two or more lists step by step in the Java programming language