mirror of
https://github.com/MoleskiCoder/EightBitNet.git
synced 2024-06-12 06:29:30 +00:00
Simplify SignExtend a little.
Signed-off-by: Adrian Conlon <Adrian.conlon@gmail.com>
This commit is contained in:
parent
9ca85428e0
commit
c06f556669
|
@ -42,10 +42,9 @@ namespace EightBit
|
||||||
// http://graphics.stanford.edu/~seander/bithacks.html#FixedSignExtend
|
// http://graphics.stanford.edu/~seander/bithacks.html#FixedSignExtend
|
||||||
public static sbyte SignExtend(int b, byte x)
|
public static sbyte SignExtend(int b, byte x)
|
||||||
{
|
{
|
||||||
var m = (byte)(1 << (b - 1)); // mask can be pre-computed if b is fixed
|
var m = Bit(b - 1); // mask can be pre-computed if b is fixed
|
||||||
x = (byte)(x & ((1 << b) - 1)); // (Skip this if bits in x above position b are already zero.)
|
x &= (byte)(Bit(b) - 1); // (Skip this if bits in x above position b are already zero.)
|
||||||
var result = (x ^ m) - m;
|
return (sbyte)((x ^ m) - m);
|
||||||
return (sbyte)result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static sbyte SignExtend(int b, int x) => SignExtend(b, (byte)x);
|
public static sbyte SignExtend(int b, int x) => SignExtend(b, (byte)x);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user