You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
79 lines
1.8 KiB
79 lines
1.8 KiB
1 month ago
|
using System;
|
||
|
using System.Collections.Generic;
|
||
|
using System.Linq;
|
||
|
using System.Threading.Tasks;
|
||
|
using System.IO;
|
||
|
|
||
|
namespace Sog
|
||
|
{
|
||
|
public static class BitUtils
|
||
|
{
|
||
|
/// <summary>
|
||
|
/// value的某一位是否是1
|
||
|
/// </summary>
|
||
|
/// <param name="value"></param>
|
||
|
/// <param name="bit">从1开始,1-32</param>
|
||
|
/// <returns></returns>
|
||
|
public static bool IsBitSet(uint value, int bit)
|
||
|
{
|
||
|
uint bitValue = ((uint)1) << (bit - 1);
|
||
|
|
||
|
return (value & bitValue) > 0;
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// value的某一位是否是1
|
||
|
/// </summary>
|
||
|
/// <param name="value"></param>
|
||
|
/// <param name="bit">从1开始,1-32</param>
|
||
|
/// <returns></returns>
|
||
|
public static bool IsBitSet(ulong value, int bit)
|
||
|
{
|
||
|
ulong bitValue = ((ulong)1) << (bit - 1);
|
||
|
|
||
|
if ((value & bitValue) > 0)
|
||
|
{
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
public static uint BitSet(uint value, int bit)
|
||
|
{
|
||
|
uint bitValue = ((uint)1) << (bit - 1);
|
||
|
|
||
|
value |= bitValue;
|
||
|
|
||
|
return value;
|
||
|
}
|
||
|
|
||
|
public static ulong BitSet(ulong value, int bit)
|
||
|
{
|
||
|
ulong bitValue = ((ulong)1) << (bit - 1);
|
||
|
|
||
|
value |= bitValue;
|
||
|
|
||
|
return value;
|
||
|
}
|
||
|
|
||
|
public static uint BitClear(uint value, int bit)
|
||
|
{
|
||
|
uint bitValue = ((uint)1) << (bit - 1);
|
||
|
|
||
|
value &= (~bitValue);
|
||
|
|
||
|
return value;
|
||
|
}
|
||
|
|
||
|
public static ulong BitClear(ulong value, int bit)
|
||
|
{
|
||
|
ulong bitValue = ((ulong)1) << (bit - 1);
|
||
|
|
||
|
value &= (~bitValue);
|
||
|
|
||
|
return value;
|
||
|
}
|
||
|
}
|
||
|
}
|