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

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

Check Nth bit of an integer number is SET or not in VB.Net

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

Program :

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

Example


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

        Console.Write("Enter Your 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 Your Number: 15
Enter Bit position between 0-31: 3
bit 3 is set

Explanation:

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


No Sidebar ads