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.

<div class="execute"></div><div class="code-mirror  language-java" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token keyword">package</span> <span class="token namespace">main</span>
<span class="token keyword">import</span> <span class="token punctuation">(</span>
   <span class="token string">"fmt"</span>
   <span class="token string">"math/bits"</span>
<span class="token punctuation">)</span>

<span class="token comment">// fmt package allows us to print anything on the screen.</span>
<span class="token comment">// bits package allows to perform bitwise operations to unsigned integer values like counting number of bits.</span>
func <span class="token function">getBits</span><span class="token punctuation">(</span>number uint<span class="token punctuation">)</span> <span class="token keyword">int</span> <span class="token punctuation">{</span>

   <span class="token comment">// getting the binary representation of the above chosen value</span>
   <span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Printf</span><span class="token punctuation">(</span><span class="token string">"Binary representation of %d is: %b\n"</span><span class="token punctuation">,</span> number<span class="token punctuation">,</span> number<span class="token punctuation">)</span>

   <span class="token comment">// initializing a variable to store the results</span>
   <span class="token keyword">var</span> result <span class="token keyword">int</span>

   <span class="token comment">// getting the number of bits in the result variable</span>
   result <span class="token operator">=</span> <span class="token class-name"><span class="token namespace">bits<span class="token punctuation">.</span></span>Len</span><span class="token punctuation">(</span>number<span class="token punctuation">)</span>

   <span class="token comment">// returning back the number of bits</span>
   <span class="token keyword">return</span> result
<span class="token punctuation">}</span>
func <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

   <span class="token comment">// initializing a variable of unsigned integer data type</span>
   <span class="token keyword">var</span> number uint

   <span class="token comment">// assigning value to the above initialized variable whose bits we wish to calculate</span>
   number <span class="token operator">=</span> <span class="token number">65</span>

   <span class="token comment">// calling the getBits() function and passing the number to it as the argument and getting back the result.</span>
   result <span class="token operator">:</span><span class="token operator">=</span> <span class="token function">getBits</span><span class="token punctuation">(</span>number<span class="token punctuation">)</span>

   <span class="token comment">// printing the number of bits of the chosen integer value</span>
   <span class="token class-name"><span class="token namespace">fmt<span class="token punctuation">.</span></span>Printf</span><span class="token punctuation">(</span><span class="token string">"Total number of bits in %d are : %d\n"</span><span class="token punctuation">,</span> number<span class="token punctuation">,</span> result<span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>

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: 2022-10-25T13:05:51+05:30

631 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements