Xor resulting byte

Posted on 2005-04-18
Last Modified: 2008-01-09
I am using VB6 doing bit manipulation

The values of the individual bits correspond to a specific action needed to be performed or to knowledge of equipment status.Example byte 11101000 the last 0 may mean that a piece of equipment is on. The program sending the string of bytes inverts (XOR's) the bit values of some but not all bytes. I wanted to XOR the byte and get the orginal byte so I wrote the following:

byteA = 11111111 Xor byteA 'byteA = 11011111 in this example

my result is 102112

I expected 00100000

I thought I was given the decimal value, however it converts to 11000111011100000 as binary

How do I get the Xor'ed byte as my result

Thanks Terry
Question by:TerryLindquist2
    LVL 84

    Expert Comment

    Public Function BinaryToDec(BinaryValue As String) As Long
    BinaryToDec = BinaryToDec + (Left(BinaryValue, 1) * 2 ^ (Len(BinaryValue) - 1))
    BinaryValue = Mid(BinaryValue, 2)
    Loop Until BinaryValue = ""
    End Function

    byteA = BinaryToDec("11011111") Xor BinaryToDec("11111111")

    Author Comment

    ozo thanks for the very quick response.

    I am very new to this so I must ask:

    1. how does this code work?
    2. how do I add it to my code?
    3. Will it give me the binary 8 bit string I need later in the program?
    4. can I substitue a variable like byteA = BinaryToDec(byteA) Xor BinaryToDec("11111111")

    LVL 84

    Expert Comment

    byteA = BinaryToDec("11011111")
    byteA = byteA Xor BinaryToDec("11111111")

    Author Comment

    My inexperience and the lack of comments has made this difficult for me to understand. I will attempt to run this and see if it will fill my needs. What I am guessing is that Xor cannot accept a byte and perform bit manipulation on the individual bits within that byte thus assembling a new byte. Each bit must be removed, manipulated and then reassembled into the new byte.

    Anyone that can help make this more clear will receive extra points. Please note that byteA will always be changing. It is to be compared to a byte 11111111 which will be constant. As noted in my first post I need to receive the inverted byte in return not a dec value.
    LVL 84

    Expert Comment

    byteA = &HDF
    byteA = byteA Xor &HFF

    stringA = binString(byteA)

    Function binString(N As Long) As String
      Do While N > 0
        a = a + CStr(N Mod 2)
        N = N \ 2
      a = StrReverse(a)
      g = Len(a)
      If g < 8 Then
        t = String$(8 - Len(a), "0")
        a = t + a
      End If
      binString = a
    End Function
    LVL 2

    Expert Comment

    you could always try

    a = not a

    not in vb is a bitwise flip on a byte.

    Author Comment

    Msroberts - I like your simple approach. I tried it and my result is still not what I expect, which is 00100000

    ByteA as string  'if I use byte I get overflow errors

    ByteA = 11011111 'for this example, it will always be different
           InvbyteA = Not byteA   'I inserted your suggestion and I got -11011112 as my result

    Any idea why this is happen? Any good books on byte and bit manipulation with VB6? I have not had time to try "ozo's" most recent solution.

    Thanks Terry

    Author Comment

    Thanks Everyone for all their help but I did the following. It may be ugly but it works

                                ba1 = Left(byteA, 1)
                                    ba1 = ba1 Xor 1
                                ba2 = Mid(byteA, 2, 1)
                                    ba2 = ba2 Xor 1
                                ba3 = Mid(byteA, 3, 1)
                                    ba3 = ba3 Xor 1
                                ba4 = Mid(byteA, 4, 1)
                                    ba4 = ba4 Xor 1
                                ba5 = Mid(byteA, 5, 1)
                                    ba5 = ba5 Xor 1
                                ba6 = Mid(byteA, 6, 1)
                                    ba6 = ba6 Xor 1
                                ba7 = Mid(byteA, 7, 1)
                                    ba7 = ba7 Xor 1
                                ba8 = Mid(byteA, 8, 1)
                                    ba8 = ba8 Xor 1
                            InvbyteA = ba1 & ba2 & ba3 & ba4 & ba5 & ba6 & ba7 & ba8
    LVL 2

    Accepted Solution

    binary style operations on strings   Yuck.

    the compliment of the string 11011111 >> int is -11011112.

    the complement of 10 is -11.  
    00001010       is 11110101

    this is expressed as an integer by default and that means that the left most bit signals the negation of the rest of the number


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Suggested Solutions

    INTRODUCTION We all know how to code. But at times you simply want to insert a common code block into your existing code and amend it as per your requirements. This tool not only saves you time but also saves you the pain of typing it all out aga…
    Purpose To explain how to place a textual stamp on a PDF document.  This is commonly referred to as an annotation, or possibly a watermark, but a watermark is generally different in that it is somewhat translucent.  Watermark’s may be text or graph…
    An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
    In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now