Open In App

bits.Len64() 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 Len64() function which is used to find the minimum number of bits required to represent a and the result is 0 for a == 0. To access Len64() function you need to add a math/bits package in your program with the help of the import keyword.

Syntax:

func Len64(a uint64) (n int)

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

Return Value: This function returns the minimum number of bits required to represent a.

Example 1:




// Golang program to illustrate bits.Len64() Function
package main
  
import (
    "fmt"
    "math/bits"
)
  
// Main function
func main() {
  
    // Using Len64() function
    a := bits.Len64(1)
    fmt.Printf("The minimum number of bits"+
      " required to represent %d: %d", 1, a)
  
}


Output:

The minimum number of bits required to represent 1: 1

Example 2 :




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


Output:

Len64(0000000000000000000000000000000000000000000000000000000000000101) = 3
Len64(0000000000000000000000000000000000000000000000000000000000001100) = 4


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

Similar Reads