note
sundialsvc4
<p>
Probably the key improvement here <em>is</em> the use of hexadecimal notation: <tt>0xffffffff</tt> instead of the corresponding, “magic” decimal. As long as you are very careful to use <u>unsigned</u> arithmetic and specify bitmasks that are no larger than the integer-size you know that you are using, code like this ought to be transportable. (It might not be <em>readable,</em> heh, but it ought to be <tt>int</tt>-length agnostic.)
</p><p>
I would almost edit my comment to say, <em>don’t use</em> “arithmetic” at all when you are bit-twiddling, as in code like this:
<code>$num = $num - 4294967296 if $num > 4294967295;
$num = $num + 4294967296 if $num < -2147483648;</code> That’s <em>nasty...</em> And I recognize at least one of those numbers, <tt>-2147483648</tt>, as an old, familiar 32-bit friend. I also detect the presence of signed arithmetic, dependent upon the <em>thirty-first</em> bit being the sign-bit. I suggest that this code should be rewritten to use bit-masking operators ... and to do so, perhaps, in a <em>very specific</em> way, as follows:
</p><p>
If you want to mash-off, say, all but the rightmost 31 bits of an integer quantity, you should take: <tt> qty := qty <b>and</b> ( <b>not</b> 0x7fffffff );</tt>. (I am not using Perl notation here, for clarity.) The subexpression, <tt>(<b>not</b> 0x7fffffff)</tt>, will evaluate to an <tt>int</tt> in which all bits are <tt>1</tt> except for the rightmost 31 bits, and it will do so correctly no matter what the <tt>sizeof(int)</tt> may be.
</p><p>
So, I suggest that you need to go through code like this (fortunately, it is a <em>small</em> piece of code), work out what it is doing and then rewrite it. Construct a very thorough set of [mod://Test::More] cases that you can run on both a 32-bit and a 64-bit system to <em>prove</em> that your rewrite is thoroughly correct.
</p>
947353
947353