0

I have a pretty rookie question for you guys.

I want to write a bit check. There I want to write a Boolean check if a two bit pattern is identical.

Unfortunately I did not understand the bit check completely.

    public bool BitPatternCheck(int number, int operand)
    {
        if (operand >> number != 0)
        {
            false;
        }

    }

Now in the Main method i would like to pass 2 numbers and check if all bits in operand are also present in number.

  • Could you explain what you want that function to do exactly? Is this question answered by [Checking if a bit is set or not](https://stackoverflow.com/q/2431732/555045)? Or by [How can I check if a binary number contains another](https://stackoverflow.com/q/21257165/555045) (C++ but the answers work in C#) – harold Apr 10 '20 at 17:43
  • 1
    Asking if the "Bit pattern is identical" is the same as asking "are the numbers the same?" You'd simply use the `==` operator. That is different from asking "if all bits in operand are also present in number", for which you would use the `&` operator. – John Wu Apr 10 '20 at 17:51

1 Answers1

0

You need to use bitwise and:

(number & operand) == operand

This is called masking. It will bitwise and each bit in number with the corresponding bit in operand. If they are both 1 then the resulting number will have the bit set in that position. For example:

number:  001110111
operand: 001100011
result:  001100011
Sean
  • 60,939
  • 11
  • 97
  • 136