-1

(Following is a slightly modified version of an article I posted on a previous iteration of this blog.)

At a former job, I spent a fair amount of time porting software. Porting is difficult, and in the product I worked on, which was fairly mature, most of the issues I ran into were configuration or environmental, and were rarely problems with the code.

One unit test revealed an interesting bug when I was porting from 32-bit to a 64-bit compiler in C++.

int value = 0xFFFFFFFF;

The intention was an integer that had all bit fields turned on, which, for a signed value, is equivalent to -1. But when this code is compiled with a 64-bit compiler, reality no longer matches intent. The value is now ’0x00000000FFFFFFFF’, not equivalent to -1 in the 64-bit world.

The fix:

int value = -1;

The moral: Try not to be too smart for your own good!

Leave a Reply

Your email address will not be published. Required fields are marked *