Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

273
Views
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.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

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.

over 4 years ago · Santiago Trujillo Report

0

Per the documentation (emphasis mine):

An expression using the &= assignment operator, such as

x &= y

is equivalent to

x = x & y

except that x is only evaluated once. The & operator performs a bitwise logical AND operation on integral operands and logical AND on bool operands.

And similar for |=.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!