Namespaces

Variants
Actions

Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries over the next few weeks. Thanks for all your past and future contributions.

(Difference between revisions)

How to swap variable values in Java ME using bitwise operations

From Wiki
Jump to: navigation, search
hamishwillee (Talk | contribs)
m (Automated deletion of category:Level-Intermediate. (This category is too subjective.))
lpvalente (Talk | contribs)
m (Lpvalente -)
 
(6 intermediate revisions by one user not shown)
Line 1: Line 1:
{{ReviewerApproved}}
+
[[Category:General Programming on Java ME]][[Category:Code Snippet]]
[[Category:Java ME]][[Category:How To]][[Category:Code_Examples]]
+
{{ArticleMetaData <!-- v1.1 -->
Heres a sample code that swaps 2 variable values
+
|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]]
 +
}}
 +
 
 +
{{Abstract|This example demonstrates how to swap two variable values.}} Here is the code:
 
<code java>
 
<code java>
 
class test  
 
class test  
Line 22: Line 41:
  
 
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.
 
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 18: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 18:18.
130 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.

×