×
Namespaces

Variants
Actions
(Difference between revisions)

How to swap variable values in Java ME using bitwise operations

From Nokia Developer Wiki
Jump to: navigation, search
adilb (Talk | contribs)
(New page: How to swap variable values using bitwise operations. Heres a sample code that swaps 2 variable values <code> class test { public static int a = 10,b = 20; public static void main(Stri...)
 
lpvalente (Talk | contribs)
m (Lpvalente -)
 
(13 intermediate revisions by 7 users not shown)
Line 1: Line 1:
How to swap variable values using bitwise operations.
+
[[Category:General Programming on Java ME]][[Category:Code Snippet]]
 +
{{ArticleMetaData <!-- v1.1 -->
 +
|sourcecode= <!-- Link to example source code e.g. [[Media:The Code Example ZIP.zip]] -->
 +
|installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) -->
 +
|devices= <!-- Devices tested against - e.g. ''devices=Nokia 6131 NFC, Nokia C7-00'') -->
 +
|sdk= <!-- SDK(s) built and tested against (e.g. [http://linktosdkdownload/ Qt SDK 1.1.4]) -->
 +
|dependencies= <!-- Any other/external dependencies e.g.: Google Maps Api v1.0 -->
 +
|signing= <!-- Signing requirements - empty or one of: Self-Signed, DevCert, Manufacturer -->
 +
|capabilities= <!-- Capabilities required by the article/code example (e.g. Location, NetworkServices. -->
 +
|language= <!-- Language category code for non-English topics - e.g. Lang-Chinese -->
 +
|translated-by= <!-- [[User:XXXX]] -->
 +
|translated-from-title= <!-- Title only -->
 +
|translated-from-id= <!-- Id of translated revision -->
 +
|review-by= <!-- After re-review: [[User:username]] -->
 +
|review-timestamp= <!-- After re-review: YYYYMMDD -->
 +
|update-by= <!-- After significant update: [[User:username]]-->
 +
|update-timestamp= <!-- After significant update: YYYYMMDD -->
 +
|creationdate= 20070523
 +
|author= [[User:Giridharn]]
 +
}}
  
Heres a sample code that swaps 2 variable values
+
{{Abstract|This example demonstrates how to swap two variable values.}} Here is the code:
<code>
+
<code java>
 
class test  
 
class test  
 
{
 
{
Line 21: Line 40:
 
</code>
 
</code>
  
The above code can be considered as much more optimized in terms of other swapping code it does not use any temp variables,secoundly it uses bitwise operators which also do internally require temp varibales to do the operation and thirdly it will be faster then the normal swap operations as bitwise operations are considered to be faster then other normal operations.
+
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.
 +
<!-- Translation --> [[pt:Como trocar os valores das variáveis utilizando operações bit a bit.]]

Latest revision as of 21:18, 5 April 2014

Article Metadata
Article
Created: giridharn (23 May 2007)
Last edited: lpvalente (05 Apr 2014)

This example demonstrates how to swap two variable values. Here is the code:

class test 
{
public static int a = 10,b = 20;
public static void main(String[] args)
{
test t = new test();
t.swap();
}
 
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.

This page was last modified on 5 April 2014, at 21:18.
146 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.

×