ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

VB.Net program to print the total bits required to store a given integer number

Here, we are going to learn how to print the total bits required to store a given integer number in VB.Net?
Submitted by Nidhi, on November 26, 2020

Here, we will calculate how many bits required storing a given number into memory using bitwise operators and printing that on the console screen.

Program/Source Code:

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 »

ADVERTISEMENT
ADVERTISEMENT




Comments and Discussions

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.