PORTC = 21; //set bits 0,2,4 to a 1, the rest to 0.
can you read or write 8 bits at once?
PORTC = B00010101; //set bits 0,2,4 to a 1, the rest to 0.
I read that all 8 bit binary numbers had been explicitly defined in Arduino and was surprised that they were not "natively" supported in C/C++. But I have a question. I just tried this: int x = B01001100; and the compiler didn't complain. Then I tried adding 8 more bits and it squawked. I guess that makes sense if only the first 256 binary numbers are defined, but doesn't it take 16 bits to be an integer type? Does this mean you can access only the low byte in an integer using binary?
#define BIT(x) (1 <<x)#define RED BIT(0)#define GREEN BIT(1)#define BLUE BIT(2)#define YELLOW (RED | GREEN)#define LED_ON BIT(15)const int mask = 0b1000000000000011;const int mask = BIT(15) | BIT(1) | BIT(0);const int mask = LED_ON | GREEN | RED;const int mask = LED_ON | YELLOW;
Code: [Select]PORTC = B00010101; //set bits 0,2,4 to a 1, the rest to 0.For some unfathomable reason this is not possible with some C compilers but as far as I know it works ok with the Arduino. Don
All C compilers support binary constants.
Quote from: bperrybap on Dec 31, 2012, 08:40 pmAll C compilers support binary constants.ANSI C has no support for binary literals.The 0bxxxxx format is something GCC adds as an extension to the standard. I think a couple of other compilers have similar extensions as well, but not all by any means.
Kind of odd that there is no support for binary literals in ANSI C.
Please enter a valid email to subscribe
We need to confirm your email address.
To complete the subscription, please click the link in the
email we just sent you.
Thank you for subscribing!
via Egeo 16