From c06f5566691fb86654ac0526a31a8bd801a5733b Mon Sep 17 00:00:00 2001 From: Adrian Conlon Date: Thu, 15 Aug 2019 22:45:56 +0100 Subject: [PATCH] Simplify SignExtend a little. Signed-off-by: Adrian Conlon --- EightBit/Processor.cs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/EightBit/Processor.cs b/EightBit/Processor.cs index dc52968..7ef5fcf 100644 --- a/EightBit/Processor.cs +++ b/EightBit/Processor.cs @@ -42,10 +42,9 @@ namespace EightBit // http://graphics.stanford.edu/~seander/bithacks.html#FixedSignExtend public static sbyte SignExtend(int b, byte x) { - var m = (byte)(1 << (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.) - var result = (x ^ m) - m; - return (sbyte)result; + var m = Bit(b - 1); // mask can be pre-computed if b is fixed + x &= (byte)(Bit(b) - 1); // (Skip this if bits in x above position b are already zero.) + return (sbyte)((x ^ m) - m); } public static sbyte SignExtend(int b, int x) => SignExtend(b, (byte)x);