[+] Utility.Bit

[+] Utility.Bit
This commit is contained in:
ALEXTANG
2023-04-25 00:16:55 +08:00
parent 688b76b745
commit e0b42d3dbe
2 changed files with 64 additions and 0 deletions

View File

@@ -0,0 +1,61 @@
namespace TEngine
{
public static partial class Utility
{
/// <summary>
/// 位运算相关的实用函数。
/// </summary>
public static class Bit
{
public static bool HasBit(long val, int idx)
{
return (val & 1L << idx) != 0L;
}
public static void SetBit(ref long val, int idx, bool isSet)
{
if (isSet)
{
val |= (1L << idx);
}
else
{
val &= ~(1L << idx);
}
}
public static bool HasBit(int val, int idx)
{
return (val & 1 << idx) != 0;
}
public static void SetBit(ref int val, int idx, bool isSet)
{
if (isSet)
{
val |= (1 << idx);
}
else
{
val &= ~(1 << idx);
}
}
public static bool HasBit(byte val, byte idx)
{
return (val & 1 << idx) != 0;
}
public static void SetBit(ref byte val, byte idx)
{
val |= (byte)(1 << idx);
}
public static byte ToByte(byte idx)
{
return (byte)(1 << idx);
}
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 95d9fac2a8ff467e864aab4cd2650dfc
timeCreated: 1682352897