Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bitwise join two if's into one

I have this,

public enum Condition : uint // bitwise
{
    None = 0,
    NewLine = 1,
    Space = 2
}

Rule.Condition someCondition = Rule.Condition.Space | Rule.Condition.NewLine;

I'd like to convert this,

if ((Rule.Condition.Space & condition) == Rule.Condition.Space) return true;
if ((Rule.Condition.NewLine & condition) == Rule.Condition.NewLine) return true;

Into something like,

if((someCondition & condition) == someCondition) return true;

But it isn't working. What am I forgetting?

like image 751
Chuck Savage Avatar asked Dec 21 '22 14:12

Chuck Savage


1 Answers

Well, if you're just wanting to test for none, then check for > 0. But, if you're looking for a less specific solution, something like this would combine the two and remove the if altogether:

return (int)(someCondition & (Condition.Space | Condition.NewLine)) > 0
like image 135
drharris Avatar answered Jan 05 '23 21:01

drharris