If I wanted to find the absolute value of a 24-bit two’s complement integer, would it be best to mask the integer, and if needed negate the original number?
To better illustrate what I mean:
public static int bitwiseAbsoluteValue(int n) {
if (n == 0x800000) {
return 0x000000;
} else {
if ((n & 0x800000) == 0x800000) {
return (~n + 1) & 0x7FFFFF;
} else {
return n;
}
}
}
Would this work?
You’d also need to mask the first return value:
And you’d need to work out what you want to do if the the value passed in is
0x800000. The current function would return0, which is obviously not correct.