Using the Bitwise Or operator you can combine integers, for example integers that are powers of 2, with each other, and later check if the returned int contains a specified value. But is there a proper way to remove values from the returned integer without combining a new one?
Currently I subtract the value that I want to remove from the combined integer, but does this work fine or can it cause issues? Or is there any more "proper" way of doing it?
Here's my code:
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;
}