## 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.