Is there any preprocessing (lang modification) done by Arduino IDE ?

While, all along, reading various articles, tutorials etc., I've been under the impression that the programming language we program in, in Arduino IDE, is actually C/C++, and not a subset. However, I've been surprised by some findings.

If I use the following expression to check if PINB is set for PB1, it seems to always evaluate to true (from logical expression evaluation standpoint). However, in standard C, I believe that shouldn't be the case. If PINB has PB1 set, the result of expression can only be 'B00010000', which should evaluate to TRUE. If PB1 is not set, the expression should evaluate to zero, which in turn should evaluate to FALSE. However, I could never get that to happen. I had a switch connected to it, which when open, pulled pin LOW, and and when pressed pulled it HIGH. Even without switch pressed, the expression always evaluated to TRUE.

if (PINB & B00000010) {
    PORTB = PORTB | B00010000;
}
else {
    PORTB = PORTB & B11101111;
}

and when I checked the expression to this...

if ((PINB & B00000010) == B00000010) {
    PORTB = PORTB | B00010000;
}
else {
    PORTB = PORTB & B11101111;
}

... lo and behold, it works as expected, i.e. both TRUE and FALSE legs get executed, based on whether I press button or not.

Have you tried printing PINB before the test?
Have you tried using hex notation in place of the #defined value B00000010?

PaulS:
Have you tried printing PINB before the test?
Have you tried using hex notation in place of the #defined value B00000010?

Thanks @Paul for taking time to respond.

To answer your questions --

  1. The code is being run on an ATtiny85 running Arduino-Tiny, and there is no serial console connectivity, so I can't print, however with a decent DMM i've checked the pin voltage to be low, and I am pretty sure that my circuit does keep the pin pulled LOW while I was testing, so the outcome should be fairly deterministic. For the 2 alternative code-segments mentioned in my original post, the outcome is 100% repeatable. Or so I'd believe, since, I've tried several dozen times with same results.

  2. Haven't tried with hex value, but should it really matter ? Now that you mention, I've been trying a bit of AVR Studio 5 in the sidelines, and in all the examples there, they use the notation "0b01010101" instead of "B01010101", i.e. the leading 0 is something I didn't notice in any Arduino documentation, so wondering if there is indeed some preprocessing done by Arduino !

The code is being run on an ATtiny85 running Arduino-Tiny, and there is no serial console connectivity, so I can't print

I think you should test it on a real Arduino, then, so you can be sure where the problem is.

so wondering if there is indeed some preprocessing done by Arduino !

Of course there is. Then, the C++ preprocessor does some more. Finally, the C++ gets a crack at the code.

The 0b00000000 format is standard. The B00000000 format is an Arduino extension. The implementation is via #define statements, and the benefit completely escapes me.

In the arduino core there is a binary.h file that defines those
perhaps if you look through the core you can find more answers

if (PINB & B00000010) is only true if PinB equals exactly B00000010. If any other pin is on it is false.

by using
((PINB & B00000010) == B00000010) you are doing 2 operations -
#1 = you are taking Pinb & PINB & B00000010 and then you are testing it for value -
the same as doing this -
PinB = PinB & B00000010;
if (PinB == B00000010) {

which is what you really want to happen.

The first method looks for an exact match with PinB where the secon looks for only B00000010 and ignore the other bits.

You’re barking up the wrong tree. The generated code is identical…

if (PINB & B00000010) {
  56:	b1 9b       	sbis	0x16, 1	; 22
  58:	02 c0       	rjmp	.+4      	; 0x5e <setup+0x8>
    PORTB = PORTB | B00010000;
  5a:	c4 9a       	sbi	0x18, 4	; 24
  5c:	08 95       	ret
}
else {
    PORTB = PORTB & B11101111;
  5e:	c4 98       	cbi	0x18, 4	; 24
  60:	08 95       	ret
if ((PINB & B00000010) == B00000010) {
  56:	b1 9b       	sbis	0x16, 1	; 22
  58:	02 c0       	rjmp	.+4      	; 0x5e <setup+0x8>
    PORTB = PORTB | B00010000;
  5a:	c4 9a       	sbi	0x18, 4	; 24
  5c:	08 95       	ret
}
else {
    PORTB = PORTB & B11101111;
  5e:	c4 98       	cbi	0x18, 4	; 24
  60:	08 95       	ret

if (PINB & B00000010) is only true if PinB equals exactly B00000010. If any other pin is on it is false.

All these years I've been getting it wrong...

All these years I've been getting it wrong...

Well, don't forget to take the floating point conversion into account...

In the arduino core there is a binary.h file that defines those

I'd never seen that before. Talk about brute force programming!

N.B. :wink: