Skip to content
Related Articles

Related Articles

bits.LeadingZeros() Function in Golang With Examples

Improve Article
Save Article
  • Last Updated : 19 Apr, 2020
Improve Article
Save Article

bits.LeadingZeros() Function in Golang is used to find the number of the leading zero bits in the given number. If the given number is equal to zero, then this function will return UintSize. To access this function, one needs to imports the math/bits package in the program.

Syntax:

func LeadingZeros64(x uint) int

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

Return Value: This function returns a total number of leading zero bits in x.

Example 1:




// Golang program to illustrate
// bits.LeadingZeros() Function
package main
   
import (
    "fmt"
    "math/bits"
)
   
// Main function
func main() {
   
    // Using LeadingZeros() function
    x := bits.LeadingZeros(5)
    fmt.Println("Total number of leading zero bits: ", x)
   
}


Output:

Total number of leading zero bits:  61

Example 2:




// Golang program to illustrate
// bits.LeadingZeros() Function
package main
   
import (
    "fmt"
    "math/bits"
)
   
// Main function
func main() {
   
    // Using LeadingZeros() function
    x := bits.LeadingZeros(0)
    fmt.Println("Total number of leading zero bits: ", x)
   
}


Output:

Total number of leading zero bits:  64

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!