Skip to content
Related Articles

Related Articles

bits.TrailingZeros64() Function in Golang with Examples
  • Last Updated : 19 Apr, 2020
GeeksforGeeks - Summer Carnival Banner

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

Syntax:

func TrailingZeros64(a uint64) int

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

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

Example 1:






// Golang program to illustrate bits.TrailingZeros64() Function
package main
  
import (
    "fmt"
    "math/bits"
)
  
// Main function
func main() {
  
    // Using TrailingZeros64() function
    a := bits.TrailingZeros64(15)
    fmt.Printf("Total number of trailing"+
            " zero bits in %d: %d", 15, a)
  
}

Output:

Total number of trailing zero bits in 15: 0

Example 2:




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

Output:

TrailingZeros64(0000000000000000000000000000000000000000000000000000000000001000)) := 3
TrailingZeros64(0000000000000000000000000000000000000000000000000000000000011000) := 3

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :