VB.Net program to check Nth bit of an integer number is SET or not

Here, we are going to learn how to check Nth bit of an integer number is SET or not in VB.Net?
Submitted by Nidhi, on November 26, 2020 [Last updated : February 18, 2023]

Checking Nth bit of an integer number is SET or not

Here, we will check the nth bit of an integer number is HIGH or LOW using bitwise operators.

VB.Net code to check Nth bit of an integer number is SET or not

The source code to check the nth bit of an integer number is SET or not is given below. The given program is compiled and executed successfully.

'VB.Net program to check nth bit of an 
'integer number is SET or not.

Module Module1

    Sub Main()
        Dim num As Integer = 0
        Dim bit As Integer = 0

        Console.Write("Enter Number: ")
        num = Integer.Parse(Console.ReadLine())

        Console.Write("Enter Bit position between 0-31: ")
        bit = Integer.Parse(Console.ReadLine())

        If ((1 << bit) And num) Then
            Console.WriteLine("bit {0} is set", bit)
        Else
            Console.WriteLine("bit {0} is not set", bit)
        End If

        Console.ReadLine()
    End Sub

End Module

Output:

Enter Number: 15
Enter Bit position between 0-31: 3
bit 3 is set

Explanation:

In the above program, we created a module Module1 that contains a function Main(). In the Main() function, we declared two integer variables num and bit that are initialized with 0. After that we read values for variables num and bit. Then we checked the given bit of the given number is SET or NOT, and print an appropriate message on the console screen.

VB.Net Basic Programs »





Comments and Discussions!

Load comments ↻





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