Couple of small refactorings, based on repeated bit patterns

Signed-off-by: Adrian Conlon <Adrian.conlon@gmail.com>
This commit is contained in:
Adrian Conlon
2019-08-07 13:27:03 +01:00
parent dedc340bf5
commit f54ef07057
3 changed files with 18 additions and 14 deletions
+12 -8
View File
@@ -12,6 +12,10 @@ namespace EightBit
{
}
public static byte Bit(int which) => (byte)(1 << which);
public static byte Bit(byte which) => Bit((int)which);
public static byte SetBit(byte input, byte which) => (byte)(input | which);
public static byte SetBit(byte input, byte which, int condition) => SetBit(input, which, condition != 0);
@@ -72,22 +76,22 @@ namespace EightBit
public static bool EvenParity(byte value) => EvenParity((int)value);
public static int FindFirstSet(int x)
public static int FindFirstSet(int value)
{
if (x == 0)
if (value == 0)
{
return 0;
}
var t = 1;
var r = 1;
while ((x & t) == 0)
var test = 1;
var result = 1;
while ((value & test) == 0)
{
t <<= 1;
++r;
test <<= 1;
++result;
}
return r;
return result;
}
}
}