Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Xor - What exactly is going on?

Status
Not open for further replies.

grnzbra

Programmer
Mar 12, 2002
1,273
US
I have the following from the VB.net 2003 help file:

This example uses the Xor operator to perform logical exclusion of the individual bits of two numeric expressions. The bit in the result pattern is set if only one of the corresponding bits in the operands are set.
Dim A As Integer = 10
Dim B As Integer = 8
Dim C As Integer = 6
Dim myCheck As Integer
myCheck = (A Xor B) ' Returns 2.
myCheck = (A Xor C) ' Returns 12.
myCheck = (B Xor C) ' Returns 14.

What do they mean by a bit and a bit being set? What is going on in each of these examples to cause it to produce the results?
 
Hi there. Take this representation of the above:

16 8 4 2 1
A. 10 = 0 1 0 1 0
B. 08 = 0 1 0 0 0
C. 06 = 0 0 1 1 0

So (A Xor B) = (10 Xor 8) = (1010 Xor 1000)

16 8 4 2 1
0 1 0 1 0
0 1 0 0 0
n n n y n

The only Xor that is true is in the 2 field, therefore (10 Xor 8) is 2.

Does this help?

There's a thin line between genius, and insanity!
 
All values in the various datatypes used in computer languages are represented with a series of bits -- each bit can be 0 or 1.

There is a convention started by the IBM System 360 mainframe that 8 bits make a Byte, 16 bits make a Word, 32 bits make a DoubleWord, and 64 bits make a QuadWord. There are other names for other groups of bits -- 4 bits is a Nibble, and 6 bits is a Nybble (comes from the Apple ][ disk subsystem) but they aren't used much.

Whole numbers (again, by convention) use the highest bit as a sign indicator. 0 means a positive number, 1 means a negative number. These are called signed numbers. You can also have unsigned numbers which don't use the sign bit, and can only hold positive values.

In .NET, an Integer (or System.Int32) is (from the online help):

[tt]Integer variables are stored as signed 32-bit (4-byte) integers ranging in value from -2,147,483,648 through 2,147,483,647.[/tt]

See ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/vblr7/html/vagrpdatatype.htm for a full summary. I think VB only uses signed numbers, but you can probably use unsigned numbers by using the types from the System namespace, and not the VB aliases.

So you've got this integer which has 16 bits in it, of which 1 is the sign bit, and 15 are used to store the value. Storing the decimal value 10 results in a bit pattern of: [tt]0000000000001010[/tt]

As you've already read, the XOR operator returns a bit pattern that's the result of setting bits only where one and only one bit is set in both operands. So 8 XOR 10 is:

[tt] 0000000000001000[/tt]
[tt]XOR 0000000000001010[/tt]
[tt] ----------------[/tt]
[tt] 0000000000000010[/tt]

Which is the value 2 in decimal.

Hope this helps.
Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
I know XOR best as "one or the other but not both" i.e. a result bit becomes 0 when both the corresponding operand bits are the same value, either 0 or 1, otherwise the result bit becones a 1.
Operand Bits
A XOR B = Result
0 0 = 0
0 1 = 1
1 0 = 1
1 1 = 0

Forms/Controls Resizing/Tabbing Control
Compare Code (Text)
Generate Sort Class in VB or VBScript
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top