Upcoming SlideShare
×

# iVSwap

433 views

Published on

iVSwap is a simple function which can be used as a library and swaps values between two variables in a mathematical way using a classic Boolean algebra law without the need of a buffer!The advantages of this technique is speed and portability.

1 Like
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

Views
Total views
433
On SlideShare
0
From Embeds
0
Number of Embeds
14
Actions
Shares
0
2
0
Likes
1
Embeds 0
No embeds

No notes for slide

### iVSwap

1. 1. Intelligent Values Swap (iVSwap) Documentation Version 1.0 Programmed and documented by George Delaportas [Computer Engineer]Conformed according to the GNU/LGPL Copyright © 2006 1
2. 2. PrologueIntelligent Values Swap (iVSwap) is a small function which is, by default, compiledas a static library. What iVSwap tries to achieve is to swap (exchange) two valuesbetween two variables without the need of a buffer.Based on mathematical techniques that were introduced by George Boole, iVSwapcan handle with any data type and swap any value!iVSwap might be useful for better memory management and C.P.U utilization or itmight be handy for those who need to save memory and C.P.U cycles whenprogramming a Microcontroller. 2
3. 3. ContentsChapter 1 1.1 Introduction............................................................................................. 05Chapter 2 2.1 iVSwap philosophy................................................................................. 07 2.2 Functionality........................................................................................... 07Chapter 3 3.1 Boolean algebra and XOR.................................................................... 09 3.2 iVSwap usage......................................................................................... 09Conclusions.............................................................................................................. 11GNU/LGPL.............................................................................................................. 12 3
4. 4. Chapter 1 4
5. 5. 1.1 IntroductionIntelligent Values Swap (iVSwap) is a very small function that swaps two valueswithout the need of a buffer. iVSwap uses Boolean laws to do this. This mathematicaltechnique has three big advantages:1) iVSwap lowers the temporary memory consumption from R.A.M.2) The swap is faster because iVSwap needs less C.P.U cycles, less Read/Write operations to R.A.M and finally if the values are not bigger than 32bit (integer), this will change in the near future to support 64bit systems too, the values will directly be located into one of the x86 C.P.U registers (AX, BX, CX and DX).3) iVSwap is simple and highly portable.To the following chapters we will see more of iVSwap and we will talk about itsphilosophy and functionality. We will also talk about Boolean algebra and XOR andfinally we will make some references on the possible usages of iVSwap. 5
6. 6. Chapter 2 6
7. 7. 2.1 iVSwap PhilosophyIntelligent Values Swap philosophy is to find a way to exchange values betweenvariables on different machine architectures independently.This might be seen worthless for just a simple swap but its true usage is far from that.Unnumbered applications can be optimized with this technique and many others cango a step further to true portability.2.2 FunctionalityIntelligent Values Swap is based on Boolean algebra laws. The basic idea of howiVSwap works is very simple and we are going to explain it.Most of us recognize XOR as a very useful logical operator that its importance is wellknown form the cryptography.Well, not only any more!XOR is capable of solving problems and giving us handy solutions for many differentthings such as iVSwap!So to see how iVSwap works we will just have to describe a few XOR operations!To the next chapter we will discuss the way that XOR works and we will see someapplications that iVSwap can be applied on. 7
8. 8. Chapter 3 8
9. 9. 3.1 Boolean algebra and XORIn this section we will analyze the XOR operator and see its properties with the helpof the truth table below. X Y Result 0 0 0 0 1 1 1 1 0 1 0 1According to the laws of Bool, you can figure out that XOR is as exclusive operatorand with its help we have the ability to make decisions or to produce results that areunique or actual! Taking that in mind we may use XOR as a filter for encrypting anddecrypting data. If we look further than that, we may also use it as a buffer!This is very easy to be done and I shall give you a simple example:Let a = 4 (0100) and b = 8 (1000).If we use XOR operator between them we will get a new variable n = 12 (1100)We do not care for the actual number in variable n. What we really need and now wehave it, is the fact that inside n we have stored numbers 4 and 8. The most importantthing though, is that we have the ability to find the second variable if we know thefirst one and vice versa. So, if I do a XOR between n and b, I will get back a. If I dothe same with a, I will get back b.This is it! We may now use XOR to swap two values without the need of a bufferbecause we have just found a way to reproduce an overwritten value.The example of how this can happen is as follows:a=a b (a is now n = 12)b=a b (b is now a = 4)a=a b (a is now b = 8)----------------------------------Where is XOR operatorAs you can see the procedure is really fast and machine independent.3.2 iVSwap usageIn this section we will discuss some examples for applications that may use iVSwapso as to get optimized. Remember though, that if we want to achieve betterperformance we have to use each and every piece of code really wisely. This isneeded for iVSwap too. 9
10. 10. Let’s get on with the examples.Example 1:We have an old PC and we want to use it as a fileserver. We have programmed theapplication that does that but we have a problem. Our PC has only a few Megabytesof available R.A.M and we have to find a way to minimize the cost. In that caseiVSwap can solve our problems!We will have the ability to free more memory and also we will gain enough C.P.Ucycles for our application!Example 2:We have a small Microcontroller which has a very limited architecture, let’s say 8 bit.If we want to push it and get the best out of it then we may need to program it inASSEMBLY or we might use smart code in a higher level language. In the secondcase we may use iVSwap. We can have fast swaps and less C.P.U cycles if we use itcarefully and we may even gain more available memory!Both of the examples use iVSwap to optimize different kind of applications so as tolessen the cost of the C.P.U and memory usage. iVSwap will be used in, exactly, thesame way to achieve its goal in any case and practically this is very useful. 10
11. 11. ConclusionsiVSwap is too small but very powerful. As a free library, it can be used by anyone andas a machine independent code, it can be used anywhere.We can optimize our programs, we can make them faster, smaller and finally we canmake them more portable if we use it wisely.I hope you find iVSwap helpful.Enjoy! 11