Open In App

bits.Reverse() Function in Golang with Examples

Last Updated : 19 Apr, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

Go language provides inbuilt support for bits to implement bit counting and manipulation functions for the predeclared unsigned integer types with the help of bits package. This package provides Reverse() function which is used to find the reversed order of the value of a. To access Reverse() function you need to add a math/bits package in your program with the help of the import keyword.

Syntax:

func Reverse(a uint) uint

Parameters: This function takes one parameter of uint type, i.e., a.

Return Value: This function returns the value of a with its bits in reversed order.

Example 1:




// Golang program to illustrate bits.Reverse() Function
package main
  
import (
    "fmt"
    "math/bits"
)
  
// Main function
func main() {
  
    // Using Reverse() function
    a := bits.Reverse(5)
    fmt.Printf("Reverse order of %d: %b", 5, a)
  
}


Output:

Reverse order of 5: 1010000000000000000000000000000000000000000000000000000000000000

Example 2:




// Golang program to illustrate bits.Reverse() Function
package main
  
import (
    "fmt"
    "math/bits"
)
  
// Main function
func main() {
  
    // Using Reverse() function
    a1 := bits.Reverse(23)
    fmt.Printf("Reverse(%b) := %b\n", 23, a1)
  
    a2 := bits.Reverse(13)
    fmt.Printf("Reverse(%b) := %b\n", 13, a2)
  
}


Output:

Reverse(10111) := 1110100000000000000000000000000000000000000000000000000000000000
Reverse(1101) := 1011000000000000000000000000000000000000000000000000000000000000



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads