uint8_t tx_data[64];
....
tx_data[43] = utime>>24;
tx_data[44] = utime>16;
tx_data[45] = utime>>8;
tx_data[46] = utime; uint8_t tx_data[64];
....
tx_data[43] = utime>>24;
tx_data[44] = utime>16;
tx_data[45] = utime>>8;
tx_data[46] = utime; tx_data[43] = utime >> 24;
tx_data[44] = utime >> 16;
tx_data[45] = utime >> 8;
tx_data[46] = utime >> 0;
Or even: tx_data[43] = utime >> (3 * 8);
tx_data[44] = utime >> (2 * 8);
tx_data[45] = utime >> (1 * 8);
tx_data[46] = utime >> (0 * 8);Having spent a lot of time digging into C code and doing microcontroller programming professionally, I have learned a certain disdain for the practice of using minimal whitespace in expressions, and the related practice of minimising LOC using C's very dense expressions syntax(unary dec/inc operators, the fact that assignment is itself an expression, etc).
The dense expressions syntax is pretty much a holdover from the time C was invented by Dennis Ritchie for the purpose of porting Unix; when it had to be typed on a painfully slow electromechanical teletype. This is also why most Unix commands are 2-4 characters long.
But it serves very little meaningful purpose today. It's important to remember that you might be able to turn 3 lines of code into 1, but it'll still be the same amount of machine code. And it will probably be harder to read, harder to change, and harder to reason about. And it can be a lot easier to miss some edgecase or even a typo like in this case. I have seen such a silly amount of off-by-one errors in C code due to some subtle misuse of unary increment/decrement that I stopped using those operators altogether. They don't improve your software in any meaningful way.
x << (3 * 8);
vs. x << 030; tx_data[44] = utime>16;
Good catch!I wonder if this would have been flagged with -Wall?
Interesting thought now you say that (no warning) - only found it because of seeing the pattern (apropos the article) and from that having a good idea what was causing it (knowing the int was assembled a byte at a time).
If I had a criticism of modern compilers, it's the blizzard of uninteresting warnings ("strncmp takes const char star, did you really mean to pass it unsigned char star") that make people want to not use -Wall.
> ("strncmp takes const char star, did you really mean to pass it unsigned char star")
This warning (with a different function) actually saved my bacon once, pointing me to a very obscure bug in the code.
My practice is to use -Wall and make sure that the code compiles without any warnings at all. Then I don't have a deluge of warnings to wade through.
I recommend asan too, where possible.
Why C's implicit conversions are trash. Why big endian is trash. Why you should do a reverse copy instead of stuff like this.