Log in

View Full Version : XOR woes



ardhuru
- 2nd February 2015, 16:11
Hi, need some help, guys!

I need to set a port to a certain value, depending on another variable which will have only 1 of 2 possible values, 0 or 255.

For example, if the variable is 0, the port should output 45; if the variable is 255, the port should output its complement, 210.

Now, I expected an XOR'ing of the 2 values to give the following results:

0 XOR 45 = 45
255 XOR 45 = 210

However, what I get are pretty random results.

I have tried both operators (XOR as well as ^), with no luck.

Any idea what's happening?

HenrikOlsson
- 2nd February 2015, 16:36
Hi,
You don't want to use the XOR operator, you want the ^ (bitwise XOR).

A VAR BYTE
B VAR BYTE
C VAR BYTE

HSEROUT["Start",13]
A = 0 : B = 45
GOSUB Display

A = 255 : B = 45
GOSUB Display

PAUSE 10
END

Display:
C = A ^ B
HSEROUT[DEC A, " ^ ", DEC B , " = ", DEC C,13]
RETURN

Gives

Start
0 ^ 45 = 45
255 ^ 45 = 210

If it doesn't work at your end, please post the code.

/Henrik.

ardhuru
- 3rd February 2015, 15:03
Henrik, thanks for that. And sure, in its basic form the '^' operator works as expected.

In this specific case, I realize that the complication(s)'s arisen because of the fact that the port being bit-manipulated has some pins which are inputs, and not to be controlled by the XOR.

I'll need to mask off those bits, I guess.

Thanks again