从按位或组合整数中删除值?
Remove value from bitwise or-combined integer?
使用按位或运算符,您可以将整数(例如 2 的幂的整数)相互组合,然后检查返回的整数是否包含指定值。但是有没有一种正确的方法可以在不合并新值的情况下从返回的整数中删除值?
目前我从组合整数中减去要删除的值,但这是否正常工作或者会导致问题?或者还有其他 "proper" 方法吗?
这是我的代码:
private void button1_Click(object sender, EventArgs e)
{
int combinedints = CombineIntegers(CombineIntegers(2, 8), 32); //Combine three integers.
MessageBox.Show(combinedints.ToString()); //Shows 42.
MessageBox.Show(RemoveInteger(combinedints, 8).ToString()); //Removes 8 - shows 34.
}
private int CombineIntegers(int a, int b)
{
return a | b;
}
private int RemoveInteger(int a, int b)
{
return a -= b;
}
private bool CheckInteger(int a, int b)
{
return (a & b) == b;
}
private int RemoveInteger(int a, int b)
{
return a & ~b;
}
使用按位或运算符,您可以将整数(例如 2 的幂的整数)相互组合,然后检查返回的整数是否包含指定值。但是有没有一种正确的方法可以在不合并新值的情况下从返回的整数中删除值?
目前我从组合整数中减去要删除的值,但这是否正常工作或者会导致问题?或者还有其他 "proper" 方法吗?
这是我的代码:
private void button1_Click(object sender, EventArgs e)
{
int combinedints = CombineIntegers(CombineIntegers(2, 8), 32); //Combine three integers.
MessageBox.Show(combinedints.ToString()); //Shows 42.
MessageBox.Show(RemoveInteger(combinedints, 8).ToString()); //Removes 8 - shows 34.
}
private int CombineIntegers(int a, int b)
{
return a | b;
}
private int RemoveInteger(int a, int b)
{
return a -= b;
}
private bool CheckInteger(int a, int b)
{
return (a & b) == b;
}
private int RemoveInteger(int a, int b)
{
return a & ~b;
}