static int power(int a, int n) { int i = 0; int ret = 1; while (i < n) { ret *= a; i++; } return ret; } static void Main(string[] args) { byte b = 0x00; //设置第0位 b ^= (byte)power(2, 0); //要判断第几位为1 int n = 0; if ((b & (byte)power(2, n)) == power(2, n)) { //第n位为1 } else { //第n位不为1 } }
{
int i = 0;
int ret = 1;
while (i < n)
{
ret *= a;
i++;
}
return ret;
}
static void Main(string[] args)
{
byte b = 0x00;
//设置第0位
b ^= (byte)power(2, 0); //要判断第几位为1
int n = 0; if ((b & (byte)power(2, n)) == power(2, n))
{
//第n位为1
}
else
{
//第n位不为1
}
}
参考