Using Logical AND 1 when setting a variable in C


Using Logical AND 1 when setting a variable in C



While looking through some code today, I came across an interesting(unecessary?) method for setting a variable: Adding a logical AND to the value.


LED_GRN = (ivLEDGrnSequence & ivLEDSlot) && 1;



I looked around a bit more for some of these occurrences and found them throughout the code, but in different forms:



As an argument for a function:


isoAgCmdHideShow(iObjectID,( (ecu.l & sVar->mask) && 1), (uint8_t *)TxMsg.buf);



In a conditional:


if( (usbQueue.selection & USB_SELECTION_CAN_1) && 1 ) {return TRUE;}



Does this extra logical AND actually change anything about the code, or is it just superfluous? I tried searching for this online, but the closest I found to an answer is Short-Circuit Evaluation which doesn't seem to apply in these situations because short-circuiting a 1 is useless.



In short, what does Logical AND 1 do for variable declaration?




4 Answers
4



This appears to be a trick to force any non-zero number to 1, while keeping zeros - alongside a more common !!(expr) idiomatic construct.


1


!!(expr)



The idea is to set LED_GRN to 1 or 0 based on the value of ivLEDGrnSequence & ivLEDSlot.


LED_GRN


1


0


ivLEDGrnSequence & ivLEDSlot



Other ways to do the same thing are as follows:


LED_GRN = !!(ivLEDGrnSequence & ivLEDSlot);
LED_GRN = (ivLEDGrnSequence & ivLEDSlot) != 0;





That makes more sense. Thanks for the alternatives to that approach! edit: Is there any use for it in the conditional, as above?
– krchrlt
Jun 29 at 19:04






Another way: LED_GRN = (ivLEDGrnSequence & ivLEDSlot) ? 1 : 0;
– Jim Rhodes
Jun 29 at 19:08


LED_GRN = (ivLEDGrnSequence & ivLEDSlot) ? 1 : 0;





Or simply: bool LED_GRN = ivLEDGrnSequence & ivLEDSlot.
– user3386109
Jun 29 at 19:14


bool LED_GRN = ivLEDGrnSequence & ivLEDSlot





@user3386109 I was surprised it is working: ideone.com/QsGdIQ
– Eugene Sh.
Jun 29 at 19:16





@EugeneSh. It should work on modern compilers (certainly when conforming to C11). bool or more specifically _Bool was a late addition to the language. So out-of-date compilers may display quirky behavior when dealing with bool.
– user3386109
Jun 29 at 19:24


bool


_Bool


bool



It's converting the result of the bitwise AND to either 0 or 1. The result of the bitwise AND can be 0 or any non-zero number. But after the logical AND, the result can only be 0 or 1.



So the first two examples may be useful. The third example with the if statement is definitely not useful, since if converts the expression to a boolean.


if


if



Doing x && 1 produces either 1 or 0, regardless of what non-zero value the left operand evaluates to.


x && 1



From the C standard:



§6.5.13 Logical AND operator



The && operator shall yield 1 if both of its operands compare unequal
to 0; otherwise, it yields 0. The result has type int.



The result of logical operation (in this case &&) is either 0 or 1. The result of arithmetic or bitwise operation (& in this case) is 0 or non-0. If we want to convert any non-0 to 1 we perform a logical operation on it. The more common and idiomatic way to accomplish this is the double negation:


&&


0


1


&


0


0


0


1


LED_GRN = !!(ivLEDGrnSequence & ivLEDSlot);






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

Export result set on Dbeaver to CSV

Opening a url is failing in Swift