Thursday, September 09, 2010

Modulus Operator on negative numbers in C

I had been introduced to the C language in the very first day of my B.Sc. course back in 2003. Since then I have been using it for doing quite some varied kinds of programming jobs. But even after so many years, its every now and then that I find out something I did not know before. Here is one such thing that I encountered few days back.

I was taught by both my C teachers in B.Sc. as well as in B.Tech. that the modulus operator in C (denoted by the “%” sign) behaves exactly similar to the mod function that is defined in Mathematics. However here is something weird that I saw recently. Better to show it by code:

   1: #include <stdio.h>
   2:  
   3: #define PRINT_int(x) printf(#x " = %d\n",x)
   4:  
   5: int main() {
   6:  
   7:     PRINT_int( ( 1)%( 6) );    
   8:     PRINT_int( (-1)%( 6) );
   9:     PRINT_int( ( 1)%(-6) );
  10:     PRINT_int( (-1)%(-6) );
  11:  
  12:     return 0;
  13: }

The output given is:


( 1)%( 6) = 1
(-1)%( 6) = -1
( 1)%(-6) = 1
(-1)%(-6) = -1

This does not match at all with the definition of the modulus operation that we have learnt in Mathematics classes. Specifically, modulus can never be negative. So something must be wrong. As it turns out C’s modulo operator behaves differently from the mathematically defined ones when we apply it for negative numbers.

The C90 standard does not define the result of modulus applied on negative numbers, so the result is compiler dependant. But C99 clearly says that the sign of the result is the same is that of the dividend, so in essence for something like:

c = a%b;

The actual operation done is:

c = sign(a) * ( abs(a) % abs(b) );

Where:

  • sign(a)=1 for a>=0
  • sign(a)=-1 for a<0

I figured the rule by myself and later clarified it from the Wikipedia page on Modulo Operation and also from the comp.lang.c group’s "Division and Modulus of Negative Integers” topic.

Wednesday, September 08, 2010

How to use Skydrive folders in Windows Explorer

I am a big fan of free online storage for my backups. So far Microsoft Skydrive seems to be the best one in terms of space. But the one thing which I hate about Skydrive is its incompatibility with any application. Ideally I would have liked to use the online folders as easily as my local folders. For example had the folder been openable in Explorer, then we could have used the “copy-paste” and “save in” features available for any local directory. Surely the latency would be higher, but at least there would be no need to do thing separately.

Anyways, here is a hack (not yet perfected but works so far) devised from various sources which allows anybody to mount his/her Skydrive folders in a local directory. This is formally called network mounting. The trick is simple enough for novice users too. Just follow the simple steps:

  1. Log-in to your sky-drive account from http://skydrive.live.com/
  2. Look into the address bar, you should see something like “https://cid-xxxxxxxxxxxxxxxx.skydrive.live.com/”, where the ‘x’s would be replaced by your unique customer-id. Copy the 16 characters of customer-id.
  3. Now open Notepad and do the following very carefully:
    1. and paste this 16 characters
    2. Note down the name of the folder that you would like to mount, say “MyOwn”
    3. In notepad create this URL (without quotes): “\\docs.live.net@SSL\xxxxxxxxxxxxxxxx\MyOwn”, replace the ‘x’s by your unique 16 character customer-id and replace “MyOwn” by the folder name (as it appears in Skydrive) that you want to mount.
    4. If you are trying to mount the default folders “Public” or “Documents”, then you have to use “^2Public” and “^2 Documents” in place of “MyOwn”.
    5. Now copy the final URL that you made.
  4. Open Explorer
  5. On the left side-bar, right click on “Network”, and select “Map network drive”
  6. Paste the URL you copied from Notepad.
  7. Select a drive letter, preferably something at the end of the English alphabet like “Z”, and hit the “Finish” button.
  8. You would be asked for your Windows Live-ID and password, which you must provide.
  9. Voila, you can now see the contents of that drive inside explorer by going to the drive you selected.
  10. Now you can use this folder just like any other local hard-drive folders. Open files from this drive by double clicking, save files, cut-copy-paste files to and from this directory.

Try it out, I’m sure you will stop using the online version after using this trick.

Now, a word of caution. This is NOT an official method, so Microsoft does not give you any guarantees of any kind for this method to work. So one fine morning this method might just give a lot of errors and rendered ineffective. But till then (and I believe chances of that happening is quite slim), utilize this feature.

Lastly the file size restriction still applies (50Mb per file). So unless you are felling adventurous, do not try this out with files lager than 50Mb. Sadly this thing does not work for non-Microsoft OS (Linux, to be specific). If I get something working on Linux, I will post it here.

If you have any questions/recommendations please post it here. I usually reply within a day.