Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,885 questions

51,811 answers

573 users

How to count the number of bits to be flipped to convert N1 to N2 in VB.NET

1 Answer

0 votes
Imports System

Public Class Program
	Public Shared Function flipped_count(ByVal a As Integer, ByVal b As Integer) As Integer
        Dim n As Integer = a Xor b
        Dim count As Integer = 0

        While n > 0
            count += 1
            n = n And (n - 1)
        End While

        Return count
    End Function

    Public Shared Sub Main()
        Dim a As Integer = 7, b As Integer = 10
		
        Console.WriteLine("a = " & Convert.ToString(a, 2).PadLeft(4, "0"c))
        Console.WriteLine("b = " & Convert.ToString(b, 2))
        Console.WriteLine("a ^ b = " & Convert.ToString(a Xor b, 2))
		
        Console.Write(flipped_count(a, b))
    End Sub
End Class




' run:
'
' a = 0111
' b = 1010
' a ^ b = 1101
' 3
'

 



answered Dec 27, 2021 by avibootz
...