0

I have a doubt in usage of Boolean variable in below expression.

bool x,y;
x &= y;    // expression 1 [which means x = x & y]
x &&= y;   // expression 2 [which means x = x && y]

In above 2 expressions which one is correct and why ? Where x and y both are Boolean and I want to perform x = x && y operation.

Also if I use expression 2 in my C code I get a compilation error saying syntax error.

Doubts: Can we use bitwise AND on boolean variables ? Why I am getting compilation error in second expression?

Jack
  • 694
  • 8
  • 20
  • https://stackoverflow.com/questions/5051349/what-is-the-difference-bettween-bitwise-and-and-logical-and – Chris Jul 06 '17 at 13:54
  • @Chris, Diff between logical and bitwise AND is understood but my question here is can we use bitwise AND on boolean variables? and why I get a compilation error in expression 2. – Jack Jul 06 '17 at 13:57
  • Yes you can use them on boolean variables, depending on the language I suppose. It works in Java for me. The explanation for your second question is in the answer below. – Chris Jul 06 '17 at 13:59
  • 4
    Because there is no `&&=` operator in C. – Eugene Sh. Jul 06 '17 at 13:59
  • 2
    `_Bool` (typedef'd to `bool`) is one of the standard unsigned integer types, so it's legal to use the bitwise `&` and `|` operators with `bool` operands. C does not define a `&&=` operator, which is why you get the compile error. – John Bode Jul 06 '17 at 14:02
  • after some googling found this - https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/and-assignment-operator The & operator performs a bitwise logical AND operation on integral operands and logical AND on bool operands. – Jack Jul 06 '17 at 14:06
  • 2
    @JohnBode It's a thin line here. Some might take just two non-zero integers, and knowing they are evaluating to truth, consider them equivalent to `true`, and then perform the bitwise operation. But suddenly discover that `1 & 2` evaluate to `false`. – Eugene Sh. Jul 06 '17 at 14:06
  • 1
    @Viki C# and C are *very different* languages. So using docs of one as a reference for the other might lead to a very wrong conclusions. – Eugene Sh. Jul 06 '17 at 14:11
  • Agreed @EugeneSh. But i guess similar thing applies to C for this question. – Jack Jul 06 '17 at 14:14
  • @EugeneSh.: Yeah, there's that. I should have clarified that "legal" simply means "not a constraint violation". – John Bode Jul 06 '17 at 14:17
  • @Eugene Sh. you are shocking me. Someone with you reputation should know the distinction between logical & bitwise operators. so everything not zero is true in C and zero false. 1 & 2 is a bitwise operation (0b01 & 0b10) so the result is 0. 1 && 2 is a logical operation and the result is 1 (true). Result of the logical operation is always 1 or 0. One of the tricks is using of !! operators to get 1 if not zero or zero id zero. – 0___________ Jul 06 '17 at 14:18
  • 1
    @PeterJ Reread and understand my comment. – Eugene Sh. Jul 06 '17 at 14:22
  • @PeterJ: Eugene is pointing out that `&&` and `&` are not interchangeable, and that you shouldn't use `&` for logical operations, – John Bode Jul 06 '17 at 14:29
  • 1
    "`which i guess`" - A simple search for "c operators" and a bit of reading to be sure was too much? That way you would also have found there is no `&&=` operator (as there is no `||=` operator either, just in case the next question pops up). Post3ers are required to show their research. We are not a tutoring site for such basic stuff found in every C book. – too honest for this site Jul 06 '17 at 14:31
  • @Olaf agreed I did bit of research but totally missed that &&= operator doesnt exists in C. – Jack Jul 06 '17 at 14:36

0 Answers0