Hello!!
I got a code with mask
#define SCAN_PIN 9
uint32_t mask = ((uint32_t)0b1 << SCAN_PIN);
I’ve never seen the second line, specilly
((uint32_t)0b1 << SCAN_PIN);
What will be the value of mask?
What’s ‘0b1’ and ‘<<’ ?
Many thanks
Hello!!
I got a code with mask
#define SCAN_PIN 9
uint32_t mask = ((uint32_t)0b1 << SCAN_PIN);
I’ve never seen the second line, specilly
((uint32_t)0b1 << SCAN_PIN);
What will be the value of mask?
What’s ‘0b1’ and ‘<<’ ?
Many thanks
What will be the value of mask?
29
0b1 is the value 1 in binary.
(uint32_t) is a ‘cast’ operation that tells the compiler to treat the 1 as an unsigned long integer *32 bit number).
<< is the ‘shift bits left’ operator
SCAN_PIN is how far to shift.
A shorter version that means the same is: 1UL << SCAN_PIN
The ‘UL’ tells the compiler that the 1 is an unsigned long constant.
“uint32_t” is an unsigned 32 bit integer, “0b” means binary notation (like “0x” means hexadecimal), “<<” means left shift, so:
“(uint32_t)0b1” is binary 00000000000000000000000000000001
and left shifted 9 times is: 00000000000000000000001000000000
= decimal 512.