Skip to content
Related Articles

Related Articles

bits.ReverseBytes() Function in Golang with Examples
  • Last Updated : 28 Apr, 2020

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 ReverseBytes() function which is used to find the reversed order of the value of a. To access ReverseBytes() function you need to add a math/bits package in your program with the help of the import keyword.

Syntax:

func ReverseBytes(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:



filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

Reverse order of 7: 11100000000000000000000000000000000000000000000000000000000

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Golang program to illustrate
// bits.ReverseBytes() Function
package main
  
import (
    "fmt"
    "math/bits"
)
  
// Main function
func main() {
  
    // Finding the reverse order of a
    // Using ReverseBytes() function
    a1 := bits.ReverseBytes(3)
    fmt.Printf("ReverseBytes(%b) := %b\n", 3, a1)
  
    a2 := bits.ReverseBytes(7)
    fmt.Printf("ReverseBytes(%b) := %b\n", 7, a2)
  
}

chevron_right


Output:

ReverseBytes(11) := 1100000000000000000000000000000000000000000000000000000000
ReverseBytes(111) := 11100000000000000000000000000000000000000000000000000000000
My Personal Notes arrow_drop_up
Recommended Articles
Page :