View Single Post
Old 19th February 2019, 23:30   #20  |  Link
StainlessS
HeartlessS Usurer
 
StainlessS's Avatar
 
Join Date: Dec 2009
Location: Over the rainbow
Posts: 10,980
Quote:
Originally Posted by spoRv View Post
I'm a bit lost about this:
Code:
If A XOR B = C : Then C XOR B = A [EDIT: Also, C XOR A = B]
Basically, where A XOR B = C, you are mixing A and B with result C, this is reversable so that you can with any two [EDIT: and in any order], recover the 3rd using XOR again.
Code:
BlankClip(width=480,Height=80)

A  = 12345678
B  = 987654
C  = BitXOR(A,B)

A2 = BitXOR(B,C)
B2 = BitXOR(A,C)

A3 = BitXOR(C,B)
B3 = BitXOR(C,A)

S1=RT_String("A  = %d  B  = %d  C = %d",A,B,C)
S2=RT_String("A2 = %d  B2 = %d",A2,B2)
S3=RT_String("A3 = %d  B3 = %d",A3,B3)

S=S1 + "\n" + S2 + "\n" + S3

Subtitle(S,lsp=0,Font="Courier New")


Code:
Truth Table XOR (where A and B differ 1 : where same 0) [for each individual bit]
  A  |    B  | Result |
_____|_______|________|
  0  |    0  |   0    |
  0  |    1  |   1    |
  1  |    0  |   1    |
  1  |    1  |   0    |
_____|_______|________|
XOR, EXCLUSIVE OR.
If Exclusively A, OR Exclusively B, then true, else false

Considering single bits, if A is some single bit number[0,1], we can toggle that bit using XOR where single bit B is 1, but not change if B is 0.
Code:
Truth Table XOR (where A and B differ 1 : where same 0) [for each individual bit]
  A  |    B  |C=Result|
_____|_______|________|
  0  |    0  |   0    |        # B=0, no toggle, so Result C is still 0 same as A
  0  |    1  |   1    |        # B=1, toggle, so Result C now 1, opposite of A
  1  |    0  |   1    |        # B=0, no toggle, so Result C is still 1 same as A
  1  |    1  |   0    |        # B=1, toggle, so Result C now 0, opposite of A
_____|_______|________|
If you now XOR whatever Result C, with the same B, it toggles that bit back to what is was in A, ie is reversable using the same XOR value.
So, with eg 32 bit ints A XOR B, wherever there is a 1 in binary B, that same position in binary A is toggled, 0->1, 1->0, do it again and they are toggle back to how they were.
So eg $AAAAAAAA XOR $FFFFFFFF = $55555555, and $55555555 XOR $FFFFFFFF = $AAAAAAAA. [XOR with $FFFFFFFF toggles every bit, XOR with $00000000 no change]
[ Hex $A = binary 1010 : Hex $5 = binary 0101 ]

Where each pixel of a PC.Levels Y8 plane is XOR'ed with $FF, will change $00->$FF and $FF->$00, and produce a photo negative invert. [$00->$FF, $01->$FE, $02->$FD ... $7F->$80, $80->$7F, etc]


XOR is used in bit addition (very very low level in chip), to generate single bit result of adding two bits (excluding carry into next column).
Eg, 1+1=0 with a carry of 1 into the next most significant bit. [1 XOR 1 = 0]

AND gate, used to generate the carry flag into next column.
eg where 1+1, carry = 1 AND 1 = 1

1+1 = 0 carry 1.
Code:
             | Result |  Carry |     
  A  |    B  |   XOR  |   AND  |
_____|_______|________|________|
  0  |    0  |   0    |    0   |
  0  |    1  |   1    |    0   |
  1  |    0  |   1    |    0   |
  1  |    1  |   0    |    1   |    <<<<<<<<<<
_____|_______|________|________|
XOR Produces result of bit addition, AND produces carry over into next column.
When you already have a carry from a previous column, it gets somewhat more complicated [ADC, Add With Carry, ie add 3 bits at once]
__________________
I sometimes post sober.
StainlessS@MediaFire ::: AND/OR ::: StainlessS@SendSpace

"Some infinities are bigger than other infinities", but how many of them are infinitely bigger ???

Last edited by StainlessS; 24th February 2019 at 17:02.
StainlessS is offline   Reply With Quote