c# - How Bitwise AND "&" Works Logically? -


i need understand bit of code please 1 have experience operators, have open source code , need understand part:

public static bool containsdestroywholerowcolumn(bonustype bt) {     return (bt & bonustype.destroywholerowcolumn)          == bonustype.destroywholerowcolumn; } 

and bonustype enum:

[flags] public enum bonustype {     none,     destroywholerowcolumn } 

please, explain how part works ?

return (bt & bonustype.destroywholerowcolumn) == bonustype.destroywholerowcolumn;

why not write : return bt == bonustype.destroywholerowcolumn; ?

thanks in advance

[testclass] public class enumtest {     [testmethod]     public void flagstest()     {         var test1 = bonustype.none;         assert.that(containsdestroywholerowcolumn(test1), is.false);         var test2 = bonustype.destroywholerowcolumn;         assert.that(containsdestroywholerowcolumn(test2));         var test3 = bonustype.none | bonustype.destroywholerowcolumn;         assert.that(containsdestroywholerowcolumn(test3));          assert.that(test3 == bonustype.destroywholerowcolumn);          assert.that(containsdestroywholerowcolumn((bonustype)5));     }  }  [flags] public enum bonustype {     none,     destroywholerowcolumn }  public static bool containsdestroywholerowcolumn(bonustype bt) {     return (bt & bonustype.destroywholerowcolumn)         == bonustype.destroywholerowcolumn; } 

as can see in example, case when behavior different equality operator if int cast bonustype.

it possible == operator overloaded bonustype change expected behavior.

both of these very, bad things (imo).


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -