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

Semrush - keyword research tool

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth
Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

42,844 questions

55,671 answers

573 users

How to print the numbers 0 to 31 by changing 1 bit each time in VB.NET

2 Answers

0 votes
Imports System

'===========================================================
' Title: Gray‑Code Sequence (One‑Bit‑Change Order)
'
' This program prints the 5‑bit Gray‑code sequence from 0 to 31.
' Gray code guarantees that each successive value differs by
' exactly one bit.
'
' Gray code formula:
'     gray(n) = n Xor (n >> 1)
'
' The program prints the Gray‑code values themselves in the
' natural one‑bit‑change order: 0, 1, 3, 2, 6, 7, 5, 4, ...
'===========================================================

Module GrayCodeSequence1

    ' Convert an integer to a 5‑bit binary string
    Function ToBits(value As Integer) As String
        Dim result As String = ""
        For i As Integer = 4 To 0 Step -1
            If (value And (1 << i)) <> 0 Then
                result &= "1"
            Else
                result &= "0"
            End If
        Next
        Return result
    End Function

    ' Print the Gray‑code sequence in one‑bit‑change order
    Sub PrintGraySequence()
        For n As Integer = 0 To 31
            Dim g As Integer = n Xor (n >> 1)   ' Gray‑code transformation
            Console.WriteLine($"{g,2}  ->  {ToBits(g)}")
        Next
    End Sub

    Sub Main()
        PrintGraySequence()
    End Sub

End Module


'run:
'
' 0  -> 00000
' 1  -> 00001
' 3  -> 00011
' 2  -> 00010
' 6  -> 00110
' 7  -> 00111
' 5  -> 00101
' 4  -> 00100
'12  -> 01100
'13  -> 01101
'15  -> 01111
'14  -> 01110
'10  -> 01010
'11  -> 01011
' 9  -> 01001
' 8  -> 01000
'24  -> 11000
'25  -> 11001
'27  -> 11011
'26  -> 11010
'30  -> 11110
'31  -> 11111
'29  -> 11101
'28  -> 11100
'20  -> 10100
'21  -> 10101
'23  -> 10111
'22  -> 10110
'18  -> 10010
'19  -> 10011
'17  -> 10001
'16  -> 10000
'

 



answered 3 days ago by avibootz
0 votes
Imports System

'===========================================================
' Title: Gray‑Code Table (Numbers 1..31 with Bit Patterns)
'
' This program generates 5‑bit Gray‑code values for numbers 0–31.
' Gray code ensures that each successive value differs by exactly
' one bit.
'
' The program prints numbers 1..31 in normal numeric order,
' while showing their corresponding Gray‑code bit patterns.
'===========================================================

Module GrayCodeSequence2

    ' Convert an integer to a 5‑bit binary string
    Function ToBits(value As Integer) As String
        Dim result As String = ""
        For i As Integer = 4 To 0 Step -1
            If (value And (1 << i)) <> 0 Then
                result &= "1"
            Else
                result &= "0"
            End If
        Next
        Return result
    End Function

    ' Print the Gray‑code table in numeric order
    Sub PrintGrayTable(gray() As Integer)
        For n As Integer = 1 To gray.Length - 1
            Console.WriteLine($"{n,2}  ->  {ToBits(gray(n))}")
        Next
    End Sub

    Sub Main()
        Dim gray(31) As Integer

        ' Generate Gray‑code values for 0..31
        For n As Integer = 0 To 31
            gray(n) = n Xor (n >> 1)
        Next

        ' Print numbers 1..31 with their Gray‑code bit patterns
        PrintGrayTable(gray)
    End Sub

End Module



'run:
'
' 1  -> 00001
' 2  -> 00011
' 3  -> 00010
' 4  -> 00110
' 5  -> 00111
' 6  -> 00101
' 7  -> 00100
' 8  -> 01100
' 9  -> 01101
'10  -> 01111
'11  -> 01110
'12  -> 01010
'13  -> 01011
'14  -> 01001
'15  -> 01000
'16  -> 11000
'17  -> 11001
'18  -> 11011
'19  -> 11010
'20  -> 11110
'21  -> 11111
'22  -> 11101
'23  -> 11100
'24  -> 10100
'25  -> 10101
'26  -> 10111
'27  -> 10110
'28  -> 10010
'29  -> 10011
'30  -> 10001
'31  -> 10000
'

 



answered 3 days ago by avibootz
...