Results 1 to 9 of 9

Thread: [RESOLVED] Bit shifting properly from 1 array of values to another array of values kinda hard qu

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2009
    Posts
    73

    Resolved [RESOLVED] Bit shifting properly from 1 array of values to another array of values kinda hard qu

    I'm trying to shift the bits from old: to new:, If 2 same values are encountered a second time, then it should skip them I guess, that's why first example is broken.

    Here is my code:

    Code:
        Private Sub Button11_Click(sender As Object, e As EventArgs) Handles Button11.Click
            Dim bitmaskCounter As Integer = 0
    
    
            Dim UniqueList() As Byte = Split("1 2 1 3 2 4 3 8 2 2 1 3 4 2 1 2", " ").[Select](Function(n) Byte.Parse(n)).ToArray()
            Dim OriginalUniqueList() As Byte = Split("1 2 4 3 1 2 3 4 3 2 1 8 2 1 2 2", " ").[Select](Function(n) Byte.Parse(n)).ToArray()
    
            Dim TheValues = New List(Of Byte)(UniqueList)
            bitmask = Split("0 0 1 1 0 0 0 1 0 0 0 0 0 0 0 0", " ").[Select](Function(n) Byte.Parse(n)).ToArray()
    
            Dim newbitmask() As Byte = Nothing
            Array.Resize(newbitmask, bitmask.Length)
            'Array.Copy(bitmask, newbitmask, bitmask.Length)
    
            bitmaskCounter = 0
            Dim i As Integer = 0
            Dim j As Integer = 0
    
            For i = 0 To UniqueList.Length - 1
                For j = (i + 1) To OriginalUniqueList.Length - 1
                    If OriginalUniqueList(i) = UniqueList(j) Then
                        Exit For
                    End If
                Next j
    
                'If OriginalUniqueList(i) = UniqueList(i) Then
                ' bitmaskCounter += 1
                ' Continue For
                ' End If
    
                'If OriginalUniqueList(j) = UniqueList(j) Then
                'bitmaskCounter += 1
                'Continue For
                'End If
    
                If (j < OriginalUniqueList.Length - 1) AndAlso newbitmask(i) = 1 AndAlso newbitmask(j) = 1 AndAlso bitmask(bitmaskCounter) = 1 Then
                    newbitmask(i) = 1
                    newbitmask(j) = 1
                ElseIf (j > OriginalUniqueList.Length - 1) AndAlso bitmask(bitmaskCounter) = 1 Then
                    newbitmask(i) = 1
                ElseIf (j < OriginalUniqueList.Length - 1) AndAlso OriginalUniqueList(i) = UniqueList(i) Then
                    newbitmask(i) = 0
                ElseIf bitmask(bitmaskCounter) = 1 Then
                    newbitmask(j) = 1
                End If
                bitmaskCounter += 1
            Next
    
            'Fixed bits output to textbox.
            txtOutput.Text += "New Bits: "
            For i = 0 To newbitmask.Length - 1
                txtOutput.Text += newbitmask(i) & " "
            Next
            txtOutput.Text += vbCrLf
    
            'Reset bitmaskCounter.
            bitmaskCounter = 0
        End Sub
    Code:
    Broken Example.
    
    value before: 1 2 1 3 2 4 3 8 2 2 1 3 4 2 1 2
    value after:  1 2 4 3 1 2 3 4 3 2 1 8 2 1 2 2
    
    old: 0 0 1 1 0 0 0 1 0 0 0 0 0 0 0 0
    new: 0 0 0 0 1 0 0 0 1 0 0 1 0 0 0 0
    
    2,4 = good
    3,8 = bad [this is broken]
    7,11 = good
    Here are good examples that work properly.

    Code:
    Good Example.
    value before: 1 2 3 1 2 3 4 1 2 3 4
    value after:  1 2 3 4 1 2 3 4 1 2 3
    
    old: 0 0 0 1 0 0 1 0 0 0 0 
    new: 0 0 0 0 1 0 0 1 0 0 0
    
    3,4 = good
    6,7 = good
    Code:
    Good Example.
    
    value before: 1 2 1 3 2 3 2 4
    value after:  1 2 4 1 2 3 2 3
    
    old: 0 0 1 1 0 1 0 0 
    new: 0 0 0 1 0 1 0 1
    
    2,3 = good
    3,5 = good
    5,7 = good
    Last edited by pkedpker; Oct 28th, 2021 at 01:46 PM.

  2. #2
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Bit shifting properly from 1 array of values to another array of values kinda har

    When you step through the code in the debugger, at what point does the expected result from a statement not match the result you get?
    Once you find that point, determine why the code didn't do what you expected it to do at that step, or give us the inputs and the line of code and what you expect the result to be at that step.

    I don't understand your good example and bad result examples, and trying to analyze the code to see if I can understand the examples could be a circular waste of time.

    If you know the expected logic, then stepping through the code yourself and watching the values change at each step to see if they match what you envision the code should do at that point should be much more productive for you than for us.
    "Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930

  3. #3
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Bit shifting properly from 1 array of values to another array of values kinda har

    @passel, I believe that if both values at a segment match, then the OP wants zero in both spots. Otherwise whichever one is the GREATER value in a difference gets the "1" bit set.

    At least that the circle I drew, lol!

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Apr 2009
    Posts
    73

    Re: Bit shifting properly from 1 array of values to another array of values kinda har

    I can pay 5 dollars for this task if anyone is up for the challenge.

    the top values must match up with the bottom values for a 1 bit to be set.
    if the values are the same on values before and after then skip it.
    bit 0 means empty bit no operation.


    Good Example.
    Code:
    value before: 1 2 1 3 2 3 2 4
    value after:  1 2 4 1 2 3 2 3
    
    old: 0 0 1 1 0 1 0 0 
    new: 0 0 0 1 0 1 0 1
    
    2,3 = good
    3,5 = good
    5,7 = good
    Take this example above.. it has a 1 bit on 2th offset (starting at offset 0). Which is value before 1. The values after has a 1 value after on offset 3 thats why offset 3 has a 1 bit now. The values 1 are ignored in offset 0 on both values before and after since they are the same.

    Otffset 3 in values before has a 3.. and 3 occurs in value after at offset 5 (starting at offset 0).

  5. #5
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Bit shifting properly from 1 array of values to another array of values kinda har

    Please realize that we have no idea what you are talking about. You have BEFORE and AFTER and OLD and NEW and none of it makes sense to me at least.

    What is the "data" you are starting with? The "value" before"?

    That "1 2 1 3 2 3 2 4" value?

    Let's answer one or two questions in each back and forth and maybe we can make progress on each step!

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  6. #6
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Bit shifting properly from 1 array of values to another array of values kinda har

    Which is why I think it would be a much better use of time for the OP to learn the great debugging capability of the IDE and step through his code and watch the values change since he apparently understands the logic and is just ignorant of how great the debugger is. If he learns to use the debugger he can probably quickly find and fix this issue and many issues to come in the future.

    If we work long enough to understand the logic and use our capability of debugging to find the issue, we might have learned some arcane bit of an algorithm that we will never need again, and not have improved our debugging capability so there is no gain for us. Only the possibly satisfaction of figuring out a coding error issue.

    If the OP takes this opportunity to lean even the rudiments of the capabilities that debugging in the VB environment provides, the benefit is essentially immeasurable as it will provide returns on investment for the rest of the lifetime the OP uses it.

    The benefit of leaning how to step through the code and watch the variable change as you go to isolate problems is hard to overstate. It is essential and extremely beneficial. It should be the first thing taught, before ever looking at a Form or control in the IDE.

    Anticipate what the code should do for the next line, Step, examine what the code did do, repeat. If it didn't do what you anticipated, figure out why.
    Don't look at the problem as a whole, look at it one step at a time.
    "Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Apr 2009
    Posts
    73

    Re: Bit shifting properly from 1 array of values to another array of values kinda har

    I'm bad at explaining so I made a picture that's worth a million words
    Attachment 182793



    Here is my newest code it's getting pretty close to what I want but still 1 bit off!.

    Code:
        Private Sub Button11_Click(sender As Object, e As EventArgs) Handles Button11.Click
            Dim bitmaskCounter As Integer = 0
    
    
            Dim UniqueList() As Byte = Split("1 2 1 3 2 4 3 8 2 2 1 3 4 2 1 2", " ").[Select](Function(n) Byte.Parse(n)).ToArray()
            Dim OriginalUniqueList() As Byte = Split("1 2 4 3 1 2 3 4 3 2 1 8 2 1 2 2", " ").[Select](Function(n) Byte.Parse(n)).ToArray()
    
            Dim TheValues = New List(Of Byte)(UniqueList)
            bitmask = Split("0 0 1 1 0 0 0 1 0 0 0 0 0 0 0 0", " ").[Select](Function(n) Byte.Parse(n)).ToArray()
    
            Dim newbitmask() As Byte = Nothing
            Array.Resize(newbitmask, bitmask.Length)
            'Array.Copy(bitmask, newbitmask, bitmask.Length)
    
            bitmaskCounter = 0
            Dim i As Integer = 0
            Dim j As Integer = 0
    
            Dim ignoreOldOffsets As New List(Of Short)
            Dim ignoreNewOffsets As New List(Of Short)
    
            Dim found As Boolean = False
    
            While i >= 0
                found = False
                Do While j > 0
                    If j = OriginalUniqueList.Length Then Exit Do
                    If OriginalUniqueList(j) = UniqueList(i) Then
                        found = True
                        Exit Do
                    End If
                    j += 1
                Loop
    
                If bitmaskCounter >= bitmask.Length Then Exit While
    
                If j = i AndAlso OriginalUniqueList(j) = UniqueList(i) OrElse j = OriginalUniqueList.Length Then
                    bitmaskCounter += 1
                    ignoreOldOffsets.Add(i)
                    ignoreNewOffsets.Add(j)
                    j = i + 1
                    i += 1
                    Continue While
                End If
    
                If ignoreOldOffsets.Contains(i) AndAlso ignoreNewOffsets.Contains(j) Then
                    bitmaskCounter += 1
                    j = i
                    Continue While
                End If
    
                'If OriginalUniqueList(j) = UniqueList(j) Then
                'bitmaskCounter += 1
                'Continue For
                'End If
                'newbitmask(i) = 1
                'newbitmask(j) = 1
                'ignoreOffsets.Add(i)
                'ignoreOffsets.Add(j)
    
                If bitmask(i) = 1 Then
                    newbitmask(i) = 1
                    newbitmask(j) = 1
                    ignoreOldOffsets.Add(i)
                    ignoreNewOffsets.Add(j)
                End If
                bitmaskCounter += 1
                i += 1
                j = i
            End While
    
            'Fixed bits output to textbox.
            txtOutput.Text += "New Bits: "
            For i = 0 To newbitmask.Length - 1
                txtOutput.Text += newbitmask(i) & " "
            Next
            txtOutput.Text += vbCrLf
    
            'Reset bitmaskCounter.
            bitmaskCounter = 0
        End Sub
    In the code above
    Code:
    old:      0 0 1 1 0 0 0 1 0 0 0 0 0 0 0 0
    new:      0 0 0 0 1 0 0 0 1 0 0 1 0 0 0 0
    i get:    0 0 1 0 1 0 0 1 0 0 0 1 0 0 0 0
    Last edited by pkedpker; Oct 28th, 2021 at 04:42 PM.

  8. #8
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Bit shifting properly from 1 array of values to another array of values kinda har

    It almost looks like you are writing the bits backwards...if I have more time in a bit I'll look again - got busy right now...

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Apr 2009
    Posts
    73

    Re: Bit shifting properly from 1 array of values to another array of values kinda har

    alright, solved it guys thanks for the helping effort!

    Code:
        Private Sub Button11_Click(sender As Object, e As EventArgs) Handles Button11.Click
            Dim bitmaskCounter As Integer = 0
    
    
            Dim UniqueList() As Byte = Split("1 2 1 3 2 4 3 8 2 2 1 3 4 2 1 2", " ").[Select](Function(n) Byte.Parse(n)).ToArray()
            Dim OriginalUniqueList() As Byte = Split("1 2 4 3 1 2 3 4 3 2 1 8 2 1 2 2", " ").[Select](Function(n) Byte.Parse(n)).ToArray()
    
            Dim TheValues = New List(Of Byte)(UniqueList)
            bitmask = Split("0 0 1 1 0 0 0 1 0 0 0 0 0 0 0 0", " ").[Select](Function(n) Byte.Parse(n)).ToArray()
    
            Dim newbitmask() As Byte = Nothing
            Array.Resize(newbitmask, bitmask.Length)
            'Array.Copy(bitmask, newbitmask, bitmask.Length)
    
            bitmaskCounter = 0
            Dim i As Integer = 0
            Dim j As Integer = 0
    
            Dim ignoreOldOffsets As New List(Of Short)
            Dim ignoreNewOffsets As New List(Of Short)
    
            Dim found As Boolean = False
    
            Dim ignoredDuplicateValues As New List(Of Short)
    
            Dim valuesIgnored As New List(Of Short)
    
            Dim valuesEncounteredOld(255) As Integer
            Dim valuesEncounteredNew(255) As Integer
    
            While i >= 0
                i = Array.IndexOf(bitmask, CByte(1), i + 1)
                j = (i + 1)
                If i = -1 OrElse UniqueList.Length = i Then Exit While
                valuesEncounteredOld(UniqueList(i)) += 1
                found = False
                Do While j >= 0
                    If j = OriginalUniqueList.Length Then Exit Do
    
                    If OriginalUniqueList(j) = UniqueList(j) AndAlso bitmask(j) <> 1 Then
                        j += 1
                        Continue Do
                    End If
    
                    If OriginalUniqueList(j) = UniqueList(i) Then
                        found = True
                        Exit Do
                    End If
                    j += 1
                Loop
    
                If bitmaskCounter >= bitmask.Length Then Exit While
    
                If bitmask(i) = 1 Then
                    newbitmask(j) = 1
                End If
    
                bitmaskCounter += 1
            End While
    
            'Fixed bits output to textbox.
            txtOutput.Text += "New Bits A: 0 0 0 0 1 0 0 0 1 0 0 1 0 0 0 0" & vbNewLine
            txtOutput.Text += "New Bits B: "
            For i = 0 To newbitmask.Length - 1
                txtOutput.Text += newbitmask(i) & " "
            Next
            txtOutput.Text += vbCrLf
    
            'Reset bitmaskCounter.
            bitmaskCounter = 0
        End Sub
    REVISION 2: (MORE FIXES)

    Code:
        Private Sub Button11_Click(sender As Object, e As EventArgs) Handles Button11.Click
            Dim bitmaskCounter As Integer = 0
    
    
            'Dim UniqueList() As Byte = Split("1 2 1 3 2 4 3 8 2 2 1 3 4 2 1 2", " ").[Select](Function(n) Byte.Parse(n)).ToArray()
            'Dim OriginalUniqueList() As Byte = Split("1 2 4 3 1 2 3 4 3 2 1 8 2 1 2 2", " ").[Select](Function(n) Byte.Parse(n)).ToArray()
            'Dim TheValues = New List(Of Byte)(UniqueList)
            'bitmask = Split("0 0 1 1 0 0 0 1 0 0 0 0 0 0 0 0", " ").[Select](Function(n) Byte.Parse(n)).ToArray()
    
            'Dim UniqueList() As Byte = Split("1 2 3 1 2 3 4 1 2 3 4", " ").[Select](Function(n) Byte.Parse(n)).ToArray()
            'Dim OriginalUniqueList() As Byte = Split("1 2 3 4 1 2 3 4 1 2 3", " ").[Select](Function(n) Byte.Parse(n)).ToArray()
            'Dim TheValues = New List(Of Byte)(UniqueList)
            'bitmask = Split("0 0 0 1 0 0 1 0 0 0 0", " ").[Select](Function(n) Byte.Parse(n)).ToArray()
    
            'Dim UniqueList() As Byte = Split("1 2 1 3 2 3 2 4", " ").[Select](Function(n) Byte.Parse(n)).ToArray()
            'Dim OriginalUniqueList() As Byte = Split("1 2 4 1 2 3 2 3", " ").[Select](Function(n) Byte.Parse(n)).ToArray()
            'Dim TheValues = New List(Of Byte)(UniqueList)
            'bitmask = Split("0 0 1 1 0 1 0 0", " ").[Select](Function(n) Byte.Parse(n)).ToArray()
    
            Dim UniqueList() As Byte = Split("1 2 1 3 2", " ").[Select](Function(n) Byte.Parse(n)).ToArray()
            Dim OriginalUniqueList() As Byte = Split("1 2 1 2 3", " ").[Select](Function(n) Byte.Parse(n)).ToArray()
            Dim TheValues = New List(Of Byte)(UniqueList)
            bitmask = Split("0 0 1 1 0", " ").[Select](Function(n) Byte.Parse(n)).ToArray()
    
    
    
    
            Dim newbitmask() As Byte = Nothing
            Array.Resize(newbitmask, bitmask.Length)
            'Array.Copy(bitmask, newbitmask, bitmask.Length)
    
            bitmaskCounter = 0
            Dim i As Integer = 0
            Dim j As Integer = 0
    
            Dim found As Boolean = False
    
            Dim firstDuplicateIndex As Integer = -1
            Dim uniquesFound As New List(Of Byte)
    
            For k = 0 To UniqueList.Length - 1
                If uniquesFound.Contains(OriginalUniqueList(k)) = False Then
                    uniquesFound.Add(OriginalUniqueList(k))
                Else
                    firstDuplicateIndex = k
                    Exit For
                End If
            Next
    
            Dim offsetsComplete As New List(Of Integer)
    
    
            While i >= 0
                i = Array.IndexOf(bitmask, CByte(1), i + 1)
                j = i '(i + 1)
                If i = -1 OrElse UniqueList.Length = i Then Exit While
                found = False
                Do While j >= 0
                    If j = OriginalUniqueList.Length Then Exit Do
    
                    If j < firstDuplicateIndex OrElse OriginalUniqueList(j) = UniqueList(j) AndAlso bitmask(j) <> 1 OrElse offsetsComplete.Contains(j) Then
                        j += 1
                        Continue Do
                    End If
    
                    If OriginalUniqueList(j) = UniqueList(i) Then
                        found = True
                        Exit Do
                    End If
                    j += 1
                Loop
    
                If bitmaskCounter >= bitmask.Length Then Exit While
    
                If j < bitmask.Length AndAlso bitmask(i) = 1 Then
                    newbitmask(j) = 1
                    offsetsComplete.Add(j)
                End If
    
                bitmaskCounter += 1
            End While
    
            'Fixed bits output to textbox.
            txtOutput.Text += "New Bits A: 0 0 0 0 1 0 0 0 1 0 0 1 0 0 0 0" & vbNewLine
            txtOutput.Text += "New Bits B: "
            For i = 0 To newbitmask.Length - 1
                txtOutput.Text += newbitmask(i) & " "
            Next
            txtOutput.Text += vbCrLf
    
            'Reset bitmaskCounter.
            bitmaskCounter = 0
        End Sub
    Last edited by pkedpker; Oct 29th, 2021 at 08:09 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width