Answered # what does A^B means???

Narue 5,707 sheva249 -1 Discussion Starter neoraghav

0

**>The following code swaps values between 2 variables without using a temp variable.....**

Actually, it invokes undefined behavior and could really do anything. If you add sequence points between the operations, then the code is technically correct:

```
a ^= b;
b ^= a;
a ^= b;
```

Figure out the bit value for a and b, then work it out yourself using the truth table for XOR:

```
INPUT OUTPUT
A B A XOR B
0 0 0
0 1 1
1 0 1
1 1 0
```

So let's say a == 1010 and b == 0101:

1010 XOR 0101 = 1111 (a = 1111)

0101 XOR 1111 = 1010 (b = 1010)

1111 XOR 1010 = 0101 (a = 0101)

End result, a == 0101 and b == 1010, which is an inversion of the original state. The XOR swap is an archaic trick to avoid the need for a temporary variable. It's still relevant if you're writing assembly or on a severely memory constrained platform, but for the most part not recommended because

- As you've proven, it's not as obvious what's going on.
- Tricks like this can actually hinder a compiler's optimizer, resulting in less efficient machine code.
- It doesn't always work. For example, if you XOR an object with itself, the result is 0 rather than the expected unchanged value. When a common implementation uses pointers, it's not an unexpected situation and needs a special case (which further complicates and slows things down):
`template <typename T> void swap(T& a, T& b) { if (&a != &b) { a ^= b; b ^= a; a ^= b; } }`

*Edited 6 Years Ago by Narue*: n/a

0

Oh I thought it was "A is to power B" thus I was surprised with the both expression and results. Thanks Narue to show it properly.

0

>The following code swaps values between 2 variables without using a temp variable.....

Actually, it invokes undefined behavior and could really do anything. If you add sequence points between the operations, then the code is technically correct:`a ^= b; b ^= a; a ^= b;`

Figure out the bit value for a and b, then work it out yourself using the truth table for XOR:

`INPUT OUTPUT A B A XOR B 0 0 0 0 1 1 1 0 1 1 1 0`

So let's say a == 1010 and b == 0101:

1010 XOR 0101 = 1111 (a = 1111)

0101 XOR 1111 = 1010 (b = 1010)

1111 XOR 1010 = 0101 (a = 0101)End result, a == 0101 and b == 1010, which is an inversion of the original state. The XOR swap is an archaic trick to avoid the need for a temporary variable. It's still relevant if you're writing assembly or on a severely memory constrained platform, but for the most part not recommended because

- As you've proven, it's not as obvious what's going on.
- Tricks like this can actually hinder a compiler's optimizer, resulting in less efficient machine code.
- It doesn't always work. For example, if you XOR an object with itself, the result is 0 rather than the expected unchanged value. When a common implementation uses pointers, it's not an unexpected situation and needs a special case (which further complicates and slows things down):
`template <typename T> void swap(T& a, T& b) { if (&a != &b) { a ^= b; b ^= a; a ^= b; } }`

thank you very much Narue for the detailed explanation

This question has already been answered. Start a new discussion instead.

Recommended Articles

Help! I want to create a java program that finds the highest even integer among the values entered by the user. Stop asking values when a value less than 1 have been entered. If no even integer is entered, display "No Even Integer"

Here is the sample output that I ...

Hello All ...

Iam Getting An Error With try to excecute the stored procedure .

I have Have Sql database , the stored procedure like so :

```
USE [MPRS]
GO
/****** Object: StoredProcedure [dbo].[Search_Licenses_By_Number] Script Date: 26-Nov-16 8:06:52 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE ...
```

I don’t want at this stage work on a big separate project as I've already got plenty ...