Home >>VB.Net Programs >VB.Net program to check the palindrome of the binary number using bitwise operators

VB.Net program to check the palindrome of the binary number using bitwise operators

VB.Net program to check the palindrome of the binary number

We will input an integer number here and then find it's binary and use a bitwise operator to check the palindrome of the binary number.

Note : The number of a palindrome is a number equal to the reverse of the same number.

Program :

The source code for checking the binary number palindrome using bitwise operators is given below. The program given is compiled and successfully executed.

Example


	Module Module1
    Sub Main()
        Dim SIZE As Byte = 8

        Dim n As Byte = 0
        Dim c(SIZE - 1) As Byte
        Dim i As SByte = SIZE - 1
        Dim j As SByte = 0
        Dim k As SByte = 0

        Console.Write("Enter Your  number ( max range should  255): ")
        n = Byte.Parse(Console.ReadLine())

        Console.WriteLine("Here Binary representation is : ")
        While n > 0
            c(i) = (n And 1)
            i = i - 1
            n = n >> 1
        End While

        For j = 0 To SIZE - 1 Step 1
            Console.Write(c(j))
        Next
        Console.WriteLine()
        j = 0
        k = SIZE - 1
        While j < k
            If Not (c(j) = c(k)) Then
                Console.WriteLine("Its Not palindrome number")
                Return
            End If
            j = j + 1
            k = k - 1
        End While

        Console.WriteLine("It's palindrome number")
        Console.ReadLine()
    End Sub
End Module
	
Output:
Enter Your number ( max range should 255) : 153 Binary representation is: 10011001 It's palindrome number

Explanation:

We created a Module1 module in the above program that contains the function Main(). We read the integer number in the Main() function and then find it's the binary equivalent. After that finding, palindrome is or is not the given number.

Console.WriteLine("Binary representation is: ")
While n > 0
    c(i) = (n And 1)
    i = i - 1
    n = n >> 1
End While

For j = 0 To SIZE - 1 Step 1
    Console.Write(c(j))
Next
Console.WriteLine()

We find the binary number of a given integer number in the code above.

j = 0
k = SIZE - 1

While j < k
    If Not (c(j) = c(k)) Then
        Console.WriteLine("Not palindrome")
        Return
    End If
    j = j + 1
    k = k - 1
End While

In the code above, we checked whether or not the calculated binary number is a palindrome.


No Sidebar ads