Related Articles

Related Articles

bits.TrailingZeros() Function in Golang with Examples
  • Last Updated : 19 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 TrailingZeros() function which is used to find the number of trailing zero bits in a and the result is UintSize for a == 0. To access TrailingZeros() function you need to add a math/bits package in your program with the help of the import keyword.

Syntax:

func TrailingZeros(a uint) int

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

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

Example 1:



filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

Total number of trailing zero bits in 6: 1

Example 2 :

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

TrailingZeros(1000)) := 3
TrailingZeros(1101) := 0



My Personal Notes arrow_drop_up
Recommended Articles
Page :