Home »
VB.Net »
VB.Net Programs
VB.Net program to print the total bits required to store a given integer number
By Nidhi Last Updated : November 16, 2024
Printing the total bits required to store an integer
Here, we will calculate how many bits required storing a given number into memory using bitwise operators and printing that on the console screen.
VB.Net code to print the total bits required to store a given integer number
The source code to print the total bits required to store a given integer number is given below. The given program is compiled and executed successfully.
'VB.Net program to print the total bits required
'to store a given integer number.
Module Module1
Sub Main()
Dim iLoop As Integer
Dim num As Integer
Dim countBits As Integer = 0
Console.Write("Enter Number: ")
num = Integer.Parse(Console.ReadLine())
For iLoop = 0 To 31 Step 1
If ((1 << iLoop) And num) Then
countBits = iLoop + 1
End If
Next
Console.Write("Total bits required to store {0} are:{1}", num, countBits)
Console.ReadLine()
End Sub
End Module
Output:
Enter Number: 127
Total bits required to store 127 are:7
Explanation:
Here, we created a module Module1 that contains a Main() method. And, we created three local variables iLoop, num, and countBits. After that, we entered a number and then we checked each bit of a given number using bitwise AND, and bitwise left-shift operators and count the total bits required to store a given number and then print the count on the console screen.
VB.Net Basic Programs »