# Xor resulting byte

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
Asked:
###### Who is Participating?

Commented:
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

Commented:
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 Commented:
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

Commented:
byteA = BinaryToDec("11011111")

byteA = byteA Xor BinaryToDec("11111111")
0

Author Commented:
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

Commented:
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

Commented:
you could always try

a = not a

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

Author Commented:
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 Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

## Already a member? Login.

All Courses

From novice to tech pro — start learning today.