Swap Values, no third variable

A classic problem asked in many interviews and such. And almost every genius will tell you that its simple.

swap(int *x,int *y)
  *x += *y;
  *y = *x - *y;
  *x = *x - *y;

Simple right? Wrong!
what happens if one value is 2147483647 and the other is 2147483640 ?
You will definitely overflow x and make a mess of it!

So whats the foolproof way?  Observe!

swap(int *x, int *y)
 *x ^= *y;
 *y ^= *x;
 *x ^= *y;

So how is it done? Bit-wise Xor operation. Faster, better and guaranteed to get you laid. For more info on bit wise xor Google it or click here

Technorati Tags: , ,

Efficient way to print int in binary format

void printBinary(int *intVal){
  register unsigned int mask;
  mask = 1<<((8*sizeof(int))-1);
    print("%d",((*intVal & mask)/mask));
  while((mask >>= 1) & -1);

Ok, Sorry I did not put any comments in the code, simply because I want it to look short and sweet. Now let me elaborate a bit on how this works.

void printBinary(int *intVal){

We are taking a pointer here, so make sure your calling function sends an address and not the value.

  register unsigned int mask;

Register for speed, we need to have an unsigned int.

mask = 1<<((8*sizeof(int))-1);

Ok, let me explain this one step at a time
Step 1: Get Size of integer.
Step 2: Multiply it with 8 ( we need the Size in Bits, not in bytes).
Step 3: Subtract this value by 1
Step 4 : Left Shift 1 by the value obtained by Step 3 calculation.

How does this work?
Assume that the platform where you are gonna run this code represents binary as 4bytes. So
Step 1: 4 Bytes
Step 2 : 4×2 = 32 Bits
Step 3 : 32 -1 = 31
Step 4 : Left shift 1 (binary : 00000000000000000000000000000001) by 31 times = binary : 10000000000000000000000000000000

    print("%d",((*intVal & mask)/mask));
  while((mask >>= 1) & -1);

Now comes the bit tricky part, so how do we do this? Quite simple once you get a hang of it.

First we do a bitwise and on the value we got from the calling function and the mask. We then divide it by mask.

Initially, the mask will be at the leftmost position (which is usually the sign bit, hence will be 1 if the value is negative). Now if the number you passed on was signed and negative, then ‘and’ing with mask will ensure that only signed bit will remain and makes the rest of the bits as ‘0’. Now you divide this value by mask itself. Hence you will get value with 1.

Now if the bit was zero then the result of  ‘and’ing would have been all zeros. Thus dividing the value with mask would have yielded 0.

Ok, now the next step, we right shift the mask by 1. So we will end up with 01000000000000000000000000000000 , first time around. We then do a bitwise and with -1 which is represented as binary all 1’s. Thus this while loop will remain true till we keep right shifting 1 in the mask all the way to the right, and finally dropped. By that time we would have printed the value of  referenced by intVal pointer in binary form.


Tip: If you know the size of integer in your platform then you can get some more performance and simplicity by pre calculating what number will be 1 followed by all zeros. For 4 bytes this value will be 2147483648.

Cscope + Notepad++ Coding nirvana!

Loved Cscope on cygwin, but hated the fact that you anything you select will open up in Vi, and then do some keyboard voodoo to get back to the cscope window?

Or are you one of those fan of notepad++ awesomeness but love the way cscope hunts for stuff for ya?

Fear not, there is a way both your sweethearts can now play with you, all at the same time for an awesome threesome😉

Just export these lines before you fire up Cscope

export CSCOPE_EDITOR="/cygdrive/c/Program Files/Notepad++/notepad++.exe"
export CSCOPE_LINEFLAG="-n%s"

Please note : Change the CSCOPE_EDITOR path to where your notepad++ is currently residing. For your convenience, I have included the path that is default for Normal install of notepad++.

Better still, put it in a script file (or perhaps bashrc).

Got a better suggestion? Post a comment🙂

Windows 7 First Impressions

Just managed to pull up Windows 7 RC1 from microsoft website. I immediately managed to installed it to my laptop ( I had to kill my Fedora 10 install in favour of Windows 7😦 , But I do have a copy of turnkey linux installed in my Vista under virtual box🙂 ). I have kept my vista Intact. Coming from Vista, here are my initial impressions (Approx 4 hours of usage). BTW, I have a Vaio CR35G/R  ( Core2 Duo T8100 2.10 Ghz, 2GB DDR2 RAM, Intel 965 Mother Board, ATI Radeon X2300 Card with 128MB on board).Windows 7 desktop

  • Installation was pretty fast. I have my copy running in about 35 minutes (Including 2 restarts and my retarded slow DVD drive).
  • The new windows 7 taskbar…err… superbar is pretty good, but it will take some time for me to get used to. Plus I don’t like the amount real estate it takes up on the screen (it can be reduced though).
  • Windows 7 art work is very very good. Kudos to Microsoft for coming up with fresh art work. Not quite in the apple league, but getting there, fast.
  • Unlike Vista, Windows 7 does not thrash Hard disk ( Vista has the nasty habit of keeping your hard disk busy for no apparent reason).
  • Bootup, Hibernate – resume, Standby – resume times are a lot faster than Vista.
  • Laptop feels a lot more snappier with Windows 7 than with Vista.
  • The new Windows media player (Windows Media Player 12)  is a lot more polished and a lot better than Vista’s. However, I was not able to get ffdshow running with it though.
  • Changing wallpaper desktop themes are pretty cool. Windows 7 supports wallpaper cycling.
  • Reduced foot print. Windows 7 at bootup takes up about 30% of RAM as compared ~50% on Vista.
  • Builtin monitor colour calibration tool ( I don’t thing Vista had one).
  • I dont know why, but since yesterday night till today morning, I have received at least some 4 updates to my Windows 7. Cant you guys club em and do ONE BIG update? (Pretty annoying, but I guess since this is a RC, they will be fixing up a lot small stuff. I hope they have a better policy for updation cycle in the final release).

All in all Microsoft seems to have go it right this time around. I will seriously consider purchasing this once it comes out. ( And no, Microsoft is not paying me to say this :p, though if they sponsor me a desktop with Nvidia GTX 250 or ATI HD 4750, I can say how well Windows 7 will do on gaming front. ;)  ).

If you have anything to add (Good/bad/ugly things about windows 7), feel free to post em in the comments section.

Push Mail for s60 ( SEVEN )

logo_seven_cc3333 Most of the corporate executives are hooked to their blackberries and its push mail service. However, blackberry service is expensive (at least here in India). Fret not folks, Seven is here to answer our prayers. Seven is a service that enables push mail for most of public domain email service providers (like gmail, yahoo, hotmail etc.). But wait, there is more, Seven can enable push mail for corporate e-mail which has OWA (like the organization that I work🙂 ). 

I now get mails from office quite easily. I have also set up my gmail account for push. Now mails are delivered almost instantly to my Phone. Though I can get push mail with E90’s IMAP support (and gmail IMAP’s IDLE feature), I would still prefer SEVEN’s solution. Here are a few reasons.

  • SEVEN client will re-establish a lost connection. The builtin email client does not.
  • SEVEN integrates with your existing mail/email system of S60 phones (works exactly like the builtin client).
  • SEVEN supports times at which real time push has to work. (with provisions for specifying alternate schedule for Push mail during weekends).
  • SEVEN retrieves all the mails as they arrive (you can specify the mail size to be downloaded, beyond which, message will be trucated). So no need to click on a mail to retrieve it. This alone makes the whole process of checking mails more enjoyable (and since there is a size cap on per message, rest assured that you wont end up having to sell your car for paying the phone bill). 
  • Support for push mails for servers with OWA support.
  • SEVEN will switch off the push mail if the phone battery level goes below a certain threshold (Configurable).
  • SEVEN Push status is clearly shown on the phone standby screen with a neat icon.


Having said, there are a few observations.

  • Seven makes the messaging application a little slow ( Not painfully slow, but noticeably slow). Im not sure if it is a problem with E90’s Messaging application, or Seven’s client.
  • Text only mails. No HTML support. But this mostly due to the fact that messaging application of E90 supports text only mails.
  • I’m using Beta edition, I’m not sure how long it will last.

Overall, I have been using it since yesterday, I think seven is an application worth keeping. If you are interested then head over to www.seven.com for getting a copy of beta client.


Technorati Tags: ,,,,