|
|
public static int unsignedShiftRight(int value, byte shiftBits) {
int result;
if (value >= 0)
result = (int)(value >> shiftBits);
else {
value &= 0x7fff;
result = (int)(value >> shiftBits);
result |= powersOf2[63 - shiftBits];
}
return result;
}
isn't there a better way to do this line
result |= powersOf2[63 - shiftBits];
so that I can pass it 64bit info?
|
Administrator
|
What is this code supposed to do, particularly when shiftBits is negative? What is it for?
--Mark
|
|
the comment in the HACK code says
/**
* Returns the given value shifted right (zero filled) by the given amount of bits.
* (Shift bits is assumed to be <= 63).
*/
|
Administrator
|
Context please!
What hack code? Hack is a 16-bit computer. What are you trying to do with 64 bits?
--Mark
|
|
I changed the hack hardware source code so it would do 32bit but to change it to 64bit I need to change that function
hack utilities shifter
this is the full code of the file
public class Shifter {
// A helper array of powers of two
public static final short[] powersOf2 = {1,2,4,8,16,32,64,128,256,512,1024,2048,4096,
8192,16384,-32768};
/**
* Returns the given value shifted right (zero filled) by the given amount of bits.
* (Shift bits is assumed to be <= 15).
*/
public static short unsignedShiftRight(short value, byte shiftBits) {
short result;
if (value >= 0)
result = (short)(value >> shiftBits);
else {
value &= 0x7fff;
result = (short)(value >> shiftBits);
result |= powersOf2[15 - shiftBits];
}
return result;
}
}
|
|
This is what the changes look like in that file
public class Shifter {
// A helper array of powers of two
public static final int[] powersOf2 = {1,2,4,8,16,32,64,128,256,512,1024,2048,4096,
8192,16384,32768,65536,131072,262144,524288,
1048576,2097152,4194304,8388608,16777216,
33554432,67108864,134217728,268435456,536870912,
1073741824,-2147483648};
/**
* Returns the given value shifted right (zero filled) by the given amount of bits.
* (Shift bits is assumed to be <= 63).
*/
public static int unsignedShiftRight(int value, byte shiftBits) {
int result;
if (value >= 0)
result = (value >> shiftBits);
else {
value &= 0x7fff;
result = (value >> shiftBits);
result |= powersOf2[63 - shiftBits];
}
return result;
}
}
I just need to get rid of the array and use something else in place of the powersof2 array since making it 64bit or bigger would make the numbers too big.
|
Administrator
|
Sorry, I'm afraid you're on your own for this. I know nothing about the internals of the supplied tools, nor am I a Java programmer.
From the looks of this code, Java doesn't support unsigned ints ?!?!
That powersOf2 thing is a nasty kludge to handle the problem caused by sign extension. If I were doing this, I'd right shift once, clear the MSB, and right shift N-1 times.
--Mark
|
|
cadet1620 wrote
That powersOf2 thing is a nasty kludge to handle the problem caused by sign extension. If I were doing this, I'd right shift once, clear the MSB, and right shift N-1 times.
--Mark
In the langaug you do know how wold you right that?
|
Administrator
|
It's still a kludge, but better than using an array.
Note that C/C++ requires a special type for 64-bit integers. I'm guessing that "int" is not correct for Java, either.
Also note the test for shiftBits < 0. The original code runs off the end of the array in this case.
int64_t unsignedShiftRight(int64_t value, int8_t shiftBits) {
if (shiftBits < 0)
return value;
if (shiftBits >= 64)
return 0;
value >>= 1;
value &= 0x7FFFFFFFFFFFFFFF;
value >>= shiftBits-1;
return value;
}
--Mark
|
|
Java does not have unsigned types. But primitive integral types have fixed sizes:
short: 2 bytes
int: 4 bytes
long: 8 bytes
So you would need to change the type of value and result to long. Also you would have to return a long. Additionally you have to change the bitmask in "value &= 0x7fff" to be a long with 63 bits set. Finally, you would need to expand the powersOf2 array to have 64 values, and they would need to be longs. And to write literal longs in java you need to append "L" to the end of the number.
|
|