Open In App

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

Syntax:

func TrailingZeros32(a uint32) int

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

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

Example 1:




// Golang program to illustrate 
// bits.TrailingZeros32() Function
package main
  
import (
    "fmt"
    "math/bits"
)
  
// Main function
func main() {
  
    // Using TrailingZeros32() function
    a := bits.TrailingZeros32(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.TrailingZeros32() Function
package main
  
import (
    "fmt"
    "math/bits"
)
  
// Main function
func main() {
  
    // Using TrailingZeros32() function
    a1 := bits.TrailingZeros32(8)
    fmt.Printf("TrailingZeros32(%032b)) := %d\n", 8, a1)
  
    a2 := bits.TrailingZeros32(13)
    fmt.Printf("TrailingZeros32(%032b) := %d\n", 13, a2)
  
}


Output:

TrailingZeros32(00000000000000000000000000001000)) := 3
TrailingZeros32(00000000000000000000000000001101) := 0


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads