There's a function "bit()"; computing me the value of a specified bit, for example bit(6) = 64.
How can I do this the other way round?
In my case I have a byte telling me which one of 8 inputs caused an interrupt, so each time only one bit is set.
I'd like to get a number from 0 to 7 telling me which input has caused the interrupt I can loop through all bits, but is there a faster and simpler solution?
Use a mask on your byte, examine bit 1 to see if it is set.
Using the index in a 'for' loop to see if the bit is set.
If not, shift the byte to the right, repeat.
How are you getting this data? Presumably you're reading off the PINx register of the corresponding port; you need to handle the case where multiple pins are asserted, and the case where you get an interrupt because the value on the pin has returned to it's idle value... Your assumption that only one bit would be set is invalid unless the hardware somehow restricts which pins can be asserted simultaneously.
C has a built-in function for this: __builtin_clz() (count leading zeros ); it should use special instructions if the particular CPU happens to have them (ARM CM3 has a "clz" instruction (by CM0 doesn't!)), or do the loop (or perhaps some other "better" algorithm) if not. There are similar built-in functions for finding the first one from the LSB.