Related Articles
bits.Len16() 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 Len16() 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 Len16() function you need to add a math/bits package in your program with the help of the import keyword.

Syntax:

func Len16(a uint16) (n int)

Parameters: This function takes one parameter of uint16 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.Len16() Functionpackage main  import (    "fmt"    "math/bits")  // Main functionfunc main() {      // Using Len16() function    a := bits.Len16(4)    fmt.Printf("The minimum number of bits"+      " required to represent %d: %d", 4, a)  }

Output:

The minimum number of bits required to represent 4: 3

Example 2:

 // Golang program to illustrate bits.Len16() Functionpackage main  import (    "fmt"    "math/bits")  // Main functionfunc main() {      // Using Len16() function    a1 := bits.Len16(4)    fmt.Printf("Len16(%016b) = %d\n", 4, a1)      a2 := bits.Len16(12)    fmt.Printf("Len16(%016b) = %d\n", 12, a2)  }

Output:

Len16(0000000000000100) = 3
Len16(0000000000001100) = 4

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