How to print the bits that need to be flipped to convert a number to another number in VB.NET

1 Answer

0 votes
Imports System
 
Public Class Program
     Private Shared Sub printNeedToBeFlippedBits(ByVal num1 As Integer, ByVal num2 As Integer)
        Dim bitNum As Integer = 0
        Dim lsb1 As Integer = 0
        Dim lsb2 As Integer = 0

        While (num1 > 0) OrElse (num2 > 0)
            lsb1 = num1 And 1
            lsb2 = num2 And 1

            If lsb1 <> lsb2 Then
                Console.Write(bitNum & " ")
            End If

			num1 = num1 >> 1
			num2 = num2 >> 1

            bitNum += 1
        End While
    End Sub
 
    Public Shared Sub Main(ByVal args As String())
        Dim num1 As Integer = 2  ' 00000010
        Dim num2 As Integer = 17 ' 00010001
         
        printNeedToBeFlippedBits(num1, num2)
		
		Console.WriteLine()
         
        num1 = 3   ' 00000011
        num2 = 221 ' 11011101
         
        printNeedToBeFlippedBits(num1, num2)
    End Sub
End Class
 
 
   
   
' run:
'
' 0 1 4 
' 1 2 3 4 6 7
'

 



answered Dec 25, 2023 by avibootz
...