Again, suspecting a small Arduino board based on the wording because we haven't heard otherwise…
The low-cost official Arduino devices use cheap ATmega processors and clones. Most are 8-bit microcontrollers that run at 16 MHz or less. A little more processing power than the original NES, but with 1% of the storage space and no video/sound chips.
Since the platform is open to extension on any microprocessor, many other devices have much better processors. I mentioned the ESP32 chips, a popular dual core 32-bit 240MHz processor, basically a SoC device similar in power to ~1998's desktop computers, yet still compatible with the Arduino libraries and tools.
a light breeze said:
That said, var & (1 << pos) is likely to be faster than (var >> pos) & 1, especially in cases where pos is known at compile-time.
The AVR instruction set has LSR and LSL, shifting right or left by a single bit at a time. It also operates on 8 bits at a time. So either way you go, they've still got to loop through the shift. If they're working on a 16-bit number that's two 8-bit LSL or LSR instructions plus carrying the value. A 32-bit number is about 10 instructions. A 64-bit number gives a notable hit on performance. Shifting by 8 at a time as a constant can get compiler optimizations, but not as a variable.
For anybody who does anything significant with the chips, it is well known that the bit shift operations if (data & (1 << n))
(either direction, right or left) slows down as n increases, and that performance drops tremendously as the variable jumps from 16, 32, or 64-bit values. Unless the device has better CPU instructions for the shift, it's going to be a loop with performance linear to n.
If you're looping across them all, it's far better to do a loop like this:
mask = 1; // whatever size you have, int8, int16, int32, int64, doesn't matter.
// inside the loop
{
if(var & mask) { ... }
mask<<1;
}
With that you only have one per loop, giving much better performance.
The second element was this:
DividedByZero said:
If it's a 0 a GPIO pin gets fired 0, if it's a 1 a GPIO pin gets fired a 1.
Setting pins is somewhat slow. It is slower to write a value than to drop power to zero. It does a lot of tests and checks on pin mode, and translation between logical pin number to physical device output method. Then it has to merge with other pins.
Many people who write for these devices will instead write directly to the port. It's easier to write PORTC=value
or PORTD=value
and set 8 pins at once. You need to know details of how ports are mapped.
On the official Arduino Uno device, digitalWrite for a value takes about 3 microseconds, clearing takes about 1 microsecond, but setting the port directly takes about 0.2 microseconds and can do a bank of them at once.