[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
Solved

# Xor resulting byte

Posted on 2005-04-18
Medium Priority
1,167 Views
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
0
Question by:TerryLindquist2
• 4
• 3
• 2

LVL 85

Expert Comment

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

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

Author Comment

ID: 13810820
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")

Terry
0

LVL 85

Expert Comment

ID: 13810885
byteA = BinaryToDec("11011111")

byteA = byteA Xor BinaryToDec("11111111")
0

Author Comment

ID: 13816766
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.
0

LVL 85

Expert Comment

ID: 13817599
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
Loop
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
0

LVL 2

Expert Comment

ID: 13818402
you could always try

a = not a

not in vb is a bitwise flip on a byte.
0

Author Comment

ID: 13818858
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
0

Author Comment

ID: 13819864
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
0

LVL 2

Accepted Solution

msroberts earned 750 total points
ID: 13819945
binary style operations on strings   Yuck.

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

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

0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn different types of Android Layout and some basics of an Android App.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, itās also ā¦
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in aā¦
Six Sigma Control Plans
###### Suggested Courses
Course of the Month18 days, 12 hours left to enroll