Revision as of 06:28, 15 August 2013 by hamishwillee (Talk | contribs)

How to swap variable values in Java ME using bitwise operations

From Nokia Developer Wiki
Jump to: navigation, search
Article Metadata
Created: giridharn (23 May 2007)
Last edited: hamishwillee (15 Aug 2013)

Heres a sample code that swaps 2 variable values

class test 
public static int a = 10,b = 20;
public static void main(String[] args)
test t = new test();
public void swap(){
a= a^b;
b = b^a;
a = a^b;
System.out.println("a "+a+" b "+b);

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.

178 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.


Thank you!

We appreciate your feedback.