Bit Shifting Question

What would be the maximum number of places I could shift a bit on an Arduino Nano or Nano Every?

Thank You!

To the end of the data word. Then, it falls off the end.

It depends on the type of variable. 8 for a char/byte, 16 for an 'int', 32 for a 'long int' and 64 for a 'long long int'.

Note: Because of "Integer Promotion" you can shift a char/byte by 16 bits since it gets promoted to 'int'.

Bit shifting is really only useful for unsigned integer types. In fact, I'm not sure it's well-defined for signed types.

Thank you John,

I'm using the code below to turn on the LEDS on an Adafruit 24 LED bar graph. All the code i'snt here but what i'm doing is using the function defined below to display a decimal counter number as binary on the 24 position bar graph. it works for bits 0-15 and then bits 16-23 are shown high on the bar graph, is it a limitation of the Arduino i'm using?

int tmDelay = 1000; // Default time delay if not using a potentiometer
int counter = 0;

void setup() {

void updateLED(int LedNumber, int BitNumber, uint32_t counter) {

bar.setBar(LedNumber, (counter & (1 << BitNumber)) == (1 << BitNumber) ? LED_GREEN : LED_OFF);
}

void loop() {

updateLED(0, 0, counter);
updateLED(1, 1, counter);
updateLED(2, 2, counter);
updateLED(3, 3, counter);
updateLED(4, 4, counter);
updateLED(5, 5, counter);
updateLED(6, 6, counter);
updateLED(7, 7, counter);
updateLED(8, 8, counter);
updateLED(9, 9, counter);
updateLED(10, 10, counter);
updateLED(11, 11, counter);
updateLED(12, 12, counter);
updateLED(13, 13, counter);
updateLED(14, 14, counter);
updateLED(15, 15, counter);

bar.writeDisplay();

delay(tmDelay);

counter = (counter + 1);

}

Wrong datatype for your mask...

bar.setBar(LedNumber, (counter & (1UL << BitNumber)) == (1UL << BitNumber) ? LED_GREEN : LED_OFF);

That constant '1' is a 16-bit integer constant. If you want it to be an 'unsigned long' constant, say '1ul'.

Not to mention that 'counter' is a signed integer.

Ok thank you! I'll give this a try and do some reading to understand my mistake.

Thank you

Thank you gfvalvo

Thank you again for the quick response!

Yes, that was my mistake! You were correct. Thank you its working now!

Thanks John. You were correct. Its working now! Greatly appreciated.

Thank you again. You were correct. I made 'counter an unsigned long'

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.