How to toggle a bit at specific position in VB.NET

1 Answer

0 votes
Imports System

Module Program

    Sub print_bits(n As Integer, Optional width As Integer = 8)
        Console.WriteLine(Convert.ToString(n, 2).PadLeft(width, "0"c))
    End Sub

    Sub Main()
        Dim n As Integer = 365
        Dim pos As Integer = 2

        print_bits(n) ' default: 8 bits

        n = n Xor (1 << pos)

        print_bits(n)
    End Sub

End Module



' run:
'
' 101101101
' 101101001
'

 



answered Mar 20, 2019 by avibootz
edited Apr 2 by avibootz
...