VB.Net program to count the total number of high bits in a given integer number

Here, we are going to learn how to count the total number of high bits in a given integer number in VB.Net?
Submitted by Nidhi, on November 26, 2020 [Last updated : February 18, 2023]

Counting the total number of high bits in an integer

Here, we will check the total high bits in a number and print the count to the console screen.

VB.Net code to count the total number of high bits in a given integer number

The source code to count the total number of high bits in a given integer number is given below. The given program is compiled and executed successfully.

'VB.Net program to count the total high bits 
'in a given number.

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

Output:

Enter Number: 7
Total high bits are:3

Explanation:

In the above program, we created a module Module1 that contains a Main() method. Here, we created three local variables iLoop, num, and countHighBits. 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 high bits in the given number and then print the count on the console screen.

VB.Net Basic Programs »





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.