What XOR do? And how can it be used? :confused:
Printable View
What XOR do? And how can it be used? :confused:
I posted a checksum function that uses the xor operator in one of your threads, by using xor I prevent the value of the checksum surpassing 255.
It is used to check if one bit or the other bit is set, but not both.
so if I had bit 1 set in a byte, and the bit 1 set in another byte and I compared them with xor, I would get 0 as they are both set on.
Here is MSDN's help file on it.
To see what is happening try:
Code:Private Sub Command1_Click()
Dim b1 As Byte, b2 As Byte
b1 = 0
b2 = 16
MsgBox b1 Xor b2 ' = 16 As bit 5 is Set In only one
End Sub
Private Sub Command1_Click()
Dim b1 As Byte, b2 As Byte
b1 = 16 'bit 5 Set
b2 = 16 'bit 5 Set
MsgBox b1 Xor b2 '0 As bit 5 is Set In both
End Sub
OK..
Then how come this returns 7
shouldnt it return 0?Code:Dim b1 As Byte, b2 As Byte
b1 = 1 'bit 5 Set
b2 = 6 'bit 5 Set
MsgBox b1 Xor b2 '0 As bit 5 is Set In both
This is why
1 is 00000001 in binary (bit 1 set only)
6 is 00000110 in binary (bit 2 and 3 set)
1 xor 6 = 00000111 in binary which equals 7 decimal !:p
See, xor checks the equivalent bits and returns 1 where either of the bits is set but not both.
image operations, checking for keypresses, making flags, etc.
(its how you can have vbyesno and vbcritical passed together in a msg box)
If you and 2 numbers (or or them) is there a way to get them apart again? I haven't got my 'thinking cap' on today.
no.
the only one you can separate again is the XOR,
and thats if you know one of the # already.
Now it makes sense, I suppose it's like you converting 30 back into it's two original factors I was thinking of.
xor stands for exclusive or and is a boolean logic comparation operator defined as:
P != q = (¬p ^ q) v (p ^ ¬q)
and is analogous with <> operator for aritmetical comparations.
true xor false is true
false xor true is true
false xor false is false
true xor true is false
boolean xor operator should not be mixed with the bitwise xor operator, which is used to compare bits of aritmetical numbers.
Hmm So if you use Xor, it always returns false? Looks like a typo to me :DQuote:
Originally posted by kedaman
true xor false is false
false xor true is false
false xor false is false
true xor true is false
false xor true = true
true xor false = true
yeah sorry
You can make xor out of and and or:
(x or y) and not (x and y)
Code:Private Sub Command1_Click()
Dim x As Integer
x = 7
MsgBox x Xor 1
MsgBox (x Or 1) And Not (x And 1)
End Sub
In the 70's when memory was really expensive, it was a challenge to get most out of a given amount of RAM or whatever storage one was using. Consequently, the art of XOR 'ing and ANDOR'ing needed to be mastered, just so that a problem could be solved. You probably understand that XOR/ANDOR allows one to flip the bits in a byte in a logical manner so as to acheive a new status or value within the byte. Logically, a byte can store 8 bits ON or OFF, so in simple terms, we can store up to 8 switch values or True/False values in a single byte - WOW!!!!
Now that we have arrived in 2001, the instruction capability remains in the languages, but the need to apply the instructions is not entirely relevant today. (Saving a few bytes is not the name of the game any more, rather, understandable, preferably self documenting code is the way of success in a piece of software). So, are you an academic or are you a worker?
Understand and explore the world of bits if you must, but do it in your own time - a company employing you will not thank you for the wonders you can acheive with XOR/ANDOR (unless they are trying to fit a massive application on the face of a 4k Chip).
And do spare a thought for those who follow - they may need to fix some problem you create, and working in the underworld of bits ain't much joy.
I do admire some dedicated developers who use the technique of XOR/ANDOR through a controlable subroutine, to acheive massive arrays of switches. Suggest you visit www.Softcircuits.com where you will find one such piece of code.