mirror of
https://github.com/netchx/netch.git
synced 2026-04-01 19:15:09 +08:00
29 lines
808 B
C#
29 lines
808 B
C#
namespace DNS.Protocol.Utils
|
|
{
|
|
public static class ByteExtensions
|
|
{
|
|
public static byte GetBitValueAt(this byte b, byte offset, byte length)
|
|
{
|
|
return (byte)((b >> offset) & ~(0xff << length));
|
|
}
|
|
|
|
public static byte GetBitValueAt(this byte b, byte offset)
|
|
{
|
|
return b.GetBitValueAt(offset, 1);
|
|
}
|
|
|
|
public static byte SetBitValueAt(this byte b, byte offset, byte length, byte value)
|
|
{
|
|
int mask = ~(0xff << length);
|
|
value = (byte)(value & mask);
|
|
|
|
return (byte)((value << offset) | (b & ~(mask << offset)));
|
|
}
|
|
|
|
public static byte SetBitValueAt(this byte b, byte offset, byte value)
|
|
{
|
|
return b.SetBitValueAt(offset, 1, value);
|
|
}
|
|
}
|
|
}
|