Golang Program to get total bits required for the given number using library function


In this article we will discuss about how to get total bits required for the given number using library function in Go language.

To get the total number of bits of any number we need to represent that number in binary which is a combination of zeroes and ones and then count the number of zeroes and ones.

For example: 16 can be converted in binary as 10000 and therefore number of bits in number 16 are 5.

65 can be converted in binary as 1000001 and therefore number of bits in number 65 are 7.

Syntax

Functions −

func function_name([parameters]) [return_types]
{
   // Body of the function
}

For loop as a while loop −

for condition {
   // code to be executed
   // increment or decrement the count variable.
}

Finding Total Bits Required For The Given Number Using Library Function

Algorithm

  • Step 1 − Import the package fmt and bits.

  • Step 2 − Initialize and define the getBits() function that will implement the logic.

  • Step 3 − start the main() function

  • Step 4 − Initialize the variables of unsigned int data type and assign value to it.

  • Step 5 − call the getBits() function.

  • Step 6 − Store the results in a variable

  • Step 7 − Print the results on the screen.

Example

Golang Program to get total bits required for the given number using library function.

package main import ( "fmt" "math/bits" ) // fmt package allows us to print anything on the screen. // bits package allows to perform bitwise operations to unsigned integer values like counting number of bits. func getBits(number uint) int { // getting the binary representation of the above chosen value fmt.Printf("Binary representation of %d is: %b\n", number, number) // initializing a variable to store the results var result int // getting the number of bits in the result variable result = bits.Len(number) // returning back the number of bits return result } func main() { // initializing a variable of unsigned integer data type var number uint // assigning value to the above initialized variable whose bits we wish to calculate number = 65 // calling the getBits() function and passing the number to it as the argument and getting back the result. result := getBits(number) // printing the number of bits of the chosen integer value fmt.Printf("Total number of bits in %d are : %d\n", number, result) }

Output

Binary representation of 65 is: 1000001
Total number of bits in 65 are : 7

Description of the Code

  • First, we import the fmt package that allows us to print anything and bits package that allows us use bitwise operations.

  • Then we have created and defined the getBits() function that calculates the total number of bits and returns the result.

  • Then we started the main() function.

  • Initialize and define the unsigned int variable and assign value to it. This is the number whose bits we wish to calculate.

  • Call the getBits() function and pass this number as an argument in it.

  • The getBits() function receives an unsigned integer value and returns the result in integer format.

  • We can then print the binary format of the chosen value and use the inbuilt bits.Len() function to get length of the total bits forming the number.

  • Store the result in a separate variable and return this value.

  • Store the value returned by the function inside main().

  • Print the results on the screen by using fmt.Printf() function.

Conclusion

We have successfully compiled and executed the Go language program to get the total number of bits required for a given program along with examples using library functions.

Updated on: 25-Oct-2022

229 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements