Home >>VB.Net Programs >VB.Net program to print the binary number of a decimal number

VB.Net program to print the binary number of a decimal number

Write a VB.Net program to print the binary number of a decimal number

Here we can use bitwise operators to print the binary number of a decimal number.

Below is the source code for print the binary number of a decimal number. The program given is compiled and successfully executed.

Example


Module Module1
    Sub Main()
        Dim iLoop As SByte
        Dim num As SByte

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

        For iLoop = 9 To 0 Step -1
            If ((1 << iLoop) And num) Then
                Console.Write("1")
            Else
                Console.Write("0")
            End If
        Next

        Console.ReadLine()
    End Sub
End Module

	
Output:
Enter Number: 10
00001000

Explanation:

We created a Module1 module in the program above that contains the Main() process. And, we developed iLoop and num, two local variables. We entered a number after that and then checked each bit of a given number using the bitwise AND and bitwise left-shift operator to print the "1" for a high bit and to print "0" on the console screen for a low bit.


No Sidebar ads