I want to automatically determine length in bytes of a field (addr
) that is uint32
, based on it's contents. Compiler is GCC. I use this:
uint8 len;
if(addr < 256) len = 1;
else if (addr < 65536) len = 2;
else if (addr < 16777216) len = 3;
else len = 4;
Is there a more efficient way?
This is inside a SPI function for a embedded device. I'm interested in the fastest way except macros, since addr
can be a variable.