2019-09-01 19:02:48 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace Toolbox.Library.IO
|
|
|
|
|
{
|
2019-09-09 02:47:19 +02:00
|
|
|
|
public class Bit
|
2019-09-01 19:02:48 +02:00
|
|
|
|
{
|
2019-10-14 03:02:39 +02:00
|
|
|
|
//From https://github.com/AustinHasten/BenzinNX/blob/06a5a600748696beac3555cd48a5837b22b0b026/include/ReadTypes.py#L33
|
2019-09-01 19:02:48 +02:00
|
|
|
|
public static uint ExtractBits(uint val, int numBits, int startBit)
|
|
|
|
|
{
|
|
|
|
|
uint mask = 0;
|
|
|
|
|
for (int i = startBit; i < startBit + numBits; i++)
|
|
|
|
|
mask |= (0x80000000 >> i);
|
|
|
|
|
|
|
|
|
|
return (val & mask) >> (32 - (startBit + numBits));
|
|
|
|
|
}
|
2019-10-14 03:02:39 +02:00
|
|
|
|
|
|
|
|
|
public static uint BitInsert(uint value, int newValue, int numBits, int startBit)
|
|
|
|
|
{
|
|
|
|
|
uint mask = 0;
|
|
|
|
|
for (int i = startBit; i < startBit + numBits; i++)
|
|
|
|
|
mask |= (0x80000000 >> i);
|
|
|
|
|
|
|
|
|
|
value &= mask == 0 ? (uint)1 : (uint)0;
|
|
|
|
|
value |= (uint)(newValue << (32 - (startBit + numBits))) & mask;
|
|
|
|
|
return value;
|
|
|
|
|
}
|
2019-09-01 19:02:48 +02:00
|
|
|
|
}
|
|
|
|
|
}
|