Files
netch/Netch/3rd/DNS/Protocol/Utils/ByteExtensions.cs
Connection Refused b2ea730984 done
2019-12-02 19:51:12 +08:00

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);
}
}
}