Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# bitwise equal bool operator

Boolean in C# are 1 byte variables. And because bool are shortcuts for the Boolean class, I would expect that the &=, |= operations have been overridden to let them work with the boolean types but I'm not so sure about it. There is nothing like &&= or ||= to be used.

Here's my question, doing something like:

bool result = condition;
result &= condition1;
result &= condition2;

will work but is it just because the bool will be 00000000 for false and 00000001 for true or because underneath the bool class will use something like &&= and ||= when the previous are called? Is it actually safe to use the previous notations or is better to use

bool result = condition;
result = result && condition1;
result = result && condition2;

to avoid any strange behavior?

Please note that conditions could be something like A >= B or any other possible check that will return a bool.

like image 310
Apache81 Avatar asked Jul 31 '15 09:07

Apache81


People also ask

What does %c mean in C?

%d is used to print decimal(integer) number ,while %c is used to print character . If you try to print a character with %d format the computer will print the ASCII code of the character.

What is %d in C programming?

In C programming language, %d and %i are format specifiers as where %d specifies the type of variable as decimal and %i specifies the type as integer. In usage terms, there is no difference in printf() function output while printing a number using %d or %i but using scanf the difference occurs.

How old is the letter C?

The letter c was applied by French orthographists in the 12th century to represent the sound ts in English, and this sound developed into the simpler sibilant s.


1 Answers

As far as I can read in this SO post, the only real difference between bitwise operators and logical operators on booleans is that the latter won't evaluate the right side operand if the left side operand is false.

That means that if you have two booleans, there won't be much difference. If you have two methods, the last one doesn't get executed using logical operators, but it does with binary operators.

like image 127
Patrick Hofman Avatar answered Sep 22 '22 21:09

Patrick Hofman