How to swap variable values in Java ME using bitwise operations
Article Metadata
Heres a sample code that swaps 2 variable values
The above code can be considered quite optimized. It does not use any temporary variables. Secoundly it uses bitwise operators which also do internally require temp varibales to do the operation. And thirdly it will be faster than the normal swap operations as bitwise operations are considered to be faster then other normal operations.


i do seriously doubt that this code is any faster than swapping using a temporary variable... i think that variable assignement is at least as fast as a binary operation, if not faster. your version is making 3 binary xor, a version using temp variable will be using 3 assignement and so the 2 will execute as fast.
Ya it's right but this concept is useful for memory utilization. because the third variable increases the memory size little bit.--vkmunjpara 10:18, 19 September 2009 (UTC)19 Sep
2009
Level: basic
This is a simple code but it uses a Bitwise XOR operator for swapping the values between two variables. So the extra third variable is not needed for swapping the values between variables.