Menu Close

Go – How to rotate bits of number in go

Here, We will learn how to rotate bits of number in go. We can rotate bits of number either left or right. RotateLeft() is used most frequently.

You would be thinking how to rotate bits of number left and right and what kind of functions are supported in math/bits package. All the queries coming to your mind would be sorted out given below.

You can relate it to left shift (<<) and right shift (>>) in C language.

There are multiple functions to rotate bits based on requirement.

  • RotateLeft
  • RotateLeft16
  • RotateLeft32
  • RotateLeft64
  • RotateLeft8

Let us discuss about RotateLeft() function of math/bits package.

RotateLeft()

RotateLeft() function rotates left by (k mod UintSize) bits of number let us say x. To rotate x right by k bits, use RotateLeft(x, -k).

Function proto type:

func RotateLeft(x uint, k int) uint

Input parameters:

x: number
k: k bits to be rotated

Return value:

RotateLeft() function returns the value of x rotated by (k mod UintSize) bits. 

1: If k is positive - Rotation left side
2: If k is negative - rotation right side

Example bit rotation:

package main

import (
  "fmt"
  "math/bits"
)


func main() {

    no := uint(16)

    // Rotated left bits by 2
    no = bits.RotateLeft(no, 2)

    fmt.Println("Number after left bits rotation: ", no)

    // Rotated right bits by 3
    no = bits.RotateLeft(no, -3)

    fmt.Println("Number after right bits rotation: ", no)

}

Output:

$ go run sample.go
 Number after left bits rotation:  64
 Number after right bits rotation:  8

x = 16 , in bits: 10000
k = 2

After 2 bits left rotation:
x = 1000000 i.e 64

After 3 bits right rotation:
x = 1000 i.e 8

RotateLeft16()

RotateLeft16() function rotates left by (k mod 16) bits of number let us say x. To rotate x right by k bits, use RotateLeft(x, -k).

Function proto type:

func RotateLeft16(x uint16, k int) uint16

Input parameters:

x: number
k: k bits to be rotated

Return value:

RotateLeft() function returns the value of x rotated by (k mod 16) bits. 

1: If k is positive - Rotation left side
2: If k is negative - rotation right side

Example bit rotation:

package main

import (
  "fmt"
  "math/bits"
)


func main() {

    no := uint16(16)

    // Rotated left bits by 2
    no = bits.RotateLeft16(no, 2)

    fmt.Println("Number after left bits rotation: ", no)

    // Rotated right bits by 3
    no = bits.RotateLeft16(no, -3)

    fmt.Println("Number after right bits rotation: ", no)

}

Output:

$ go run sample.go
 Number after left bits rotation:  64
 Number after right bits rotation:  8

x = 16 , in bits: 10000
k = 2

After 2 bits left rotation:
x = 1000000 i.e 64

After 3 bits right rotation:
x = 1000 i.e 8

RotateLeft32()

RotateLeft32() function rotates left by (k mod 32) bits of number let us say x. To rotate x right by k bits, use RotateLeft(x, -k).

Function proto type:

func RotateLeft32(x uint32, k int) uint32

Input parameters:

x: number
k: k bits to be rotated

Return value:

RotateLeft() function returns the value of x rotated by (k mod 32) bits. 

1: If k is positive - Rotation left side
2: If k is negative - rotation right side

Example bit rotation:

package main

import (
  "fmt"
  "math/bits"
)


func main() {

    no := uint32(16)

    // Rotated left bits by 2
    no = bits.RotateLeft32(no, 2)

    fmt.Println("Number after left bits rotation: ", no)

    // Rotated right bits by 3
    no = bits.RotateLeft32(no, -3)

    fmt.Println("Number after right bits rotation: ", no)

}

Output:

$ go run sample.go
 Number after left bits rotation:  64
 Number after right bits rotation:  8

x = 16 , in bits: 10000
k = 2

After 2 bits left rotation:
x = 1000000 i.e 64

After 3 bits right rotation:
x = 1000 i.e 8

RotateLeft64()

RotateLeft64() function rotates left by (k mod 64) bits of number let us say x. To rotate x right by k bits, use RotateLeft(x, -k).

Function proto type:

func RotateLeft64(x uint64, k int) uint64

Input parameters:

x: number
k: k bits to be rotated

Return value:

RotateLeft() function returns the value of x rotated by (k mod 64) bits. 

1: If k is positive - Rotation left side
2: If k is negative - rotation right side

Example bit rotation:

package main

import (
  "fmt"
  "math/bits"
)


func main() {

    no := uint64(16)

    // Rotated left bits by 2
    no = bits.RotateLeft64(no, 2)

    fmt.Println("Number after left bits rotation: ", no)

    // Rotated right bits by 3
    no = bits.RotateLeft64(no, -3)

    fmt.Println("Number after right bits rotation: ", no)

}

Output:

$ go run sample.go
 Number after left bits rotation:  64
 Number after right bits rotation:  8

x = 16 , in bits: 10000
k = 2

After 2 bits left rotation:
x = 1000000 i.e 64

After 3 bits right rotation:
x = 1000 i.e 8

RotateLeft8()

RotateLeft8() function rotates left by (k mod 8) bits of number let us say x. To rotate x right by k bits, use RotateLeft(x, -k).

Function proto type:

func RotateLeft8(x uint8, k int) uint8

Input parameters:

x: number
k: k bits to be rotated

Return value:

RotateLeft() function returns the value of x rotated by (k mod 8) bits. 

1: If k is positive - Rotation left side
2: If k is negative - rotation right side

Example bit rotation:

package main

import (
  "fmt"
  "math/bits"
)


func main() {

    no := uint64(16)

    // Rotated left bits by 2
    no = bits.RotateLeft64(no, 2)

    fmt.Println("Number after left bits rotation: ", no)

    // Rotated right bits by 3
    no = bits.RotateLeft64(no, -3)

    fmt.Println("Number after right bits rotation: ", no)

}

Output:

$ go run sample.go
 Number after left bits rotation:  64
 Number after right bits rotation:  8

x = 16 , in bits: 10000
k = 2

After 2 bits left rotation:
x = 1000000 i.e 64

After 3 bits right rotation:
x = 1000 i.e 8

To learn more about golang, Please refer given below link:

https://www.techieindoor.com/go-lang-tutorial/

References:

https://golang.org/doc/
https://golang.org/pkg/
Posted in golang, math

Leave a Reply

Your email address will not be published.

Contact Us