GetPiecewiseInteger()?
Posted: Thu Mar 08, 2007 11:12 am
This is a function Ronan coded to pull an integer out of a range of bits in a larger integer, used frequently in the spawn system to cut down the number of LocalInt() reads and writes.
However, whenever it tries to read a value that requires the full range of bits, it's resulting in a negative number somehow. say, for a 5-bit range (position 0 to position 5), of 10001 (should be 17), it's returning "-15". Any idea on how it can be fixed? The bit shifting operands are pretty close to greek to me, and I didn't even have latin in school, so... help would be appreciated.
Code: Select all
const int INT_SIZE = 32;
int GetPiecewiseInteger(int nNum, int nStartBit, int nEndBit) {
int nShift = INT_SIZE - 1 - nEndBit;
return ((nNum << nShift) >>> (nShift + nStartBit));
}