Home >>VB.Net Programs >VB.Net program to count the total number of high bits in a given integer number
Here, we'll check the total number of high bits, and print the count on the console screen.
Program
Below is the source code for counting the total number of high bits in a given integer number. The program given is compiled and successfully executed.
Example
Module Module1
Sub Main()
Dim iLoop As Integer
Dim num As Integer
Dim countHighBits 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
countHighBits = countHighBits + 1
End If
Next
Console.Write("Total high bits are:{0}", countHighBits)
Console.ReadLine()
End Sub
End Module
Explanation:
We generated a Module1 module in the program above that contains the Main() method. Three local variables, iLoop, num, and countHighBits, were generated here. After that, we entered a number and then, using bitwise AND and bitwise left-shift operators, checked each bit of a given number and counted the total high bits in the given number and then print the count on the console screen.