countingBits

countingBits

LED Counter

A hobbyist project involves using a Arduino board to turn on and off several LEDs, let’s assume there are 8 LEDs. The current status of the 8 LEDs that are turned on or off is captured in one single integer variable led_status. If a particular bit is set to value 0 (zero), the corresponding LED is turned off.

Write a function which accepts this integer variable led_status as input and returns the count of the number of LEDs that are currently switched on.

NB: There might be more bits in the integer variable than there are LEDs. In that case, it can be assumed that those bits will also be set to 0 (zero).

Bonus

Is it possible to write a one-line code for this in Python?

Last updated