Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revision | Next revisionBoth sides next revision | ||
user:embedded_c_code_doesn_t_have_to_be_ugly [2020/05/11 17:15] – [4. Bit manipulations vs. structured data] Igor Yefmov | user:embedded_c_code_doesn_t_have_to_be_ugly [2020/05/11 17:18] – [4. Bit manipulations vs. structured data] Igor Yefmov | ||
---|---|---|---|
Line 101: | Line 101: | ||
Here's a more concrete example: working with 16/32 bit integers, where depending on the context it is either a CPU-addressable word, or a stream of bytes on the wire (be it USB or I²C). Traditionally that would result in the 1970's style code like this: | Here's a more concrete example: working with 16/32 bit integers, where depending on the context it is either a CPU-addressable word, or a stream of bytes on the wire (be it USB or I²C). Traditionally that would result in the 1970's style code like this: | ||
- | <code C> | + | <code C> |
uint8_t location[4]; | uint8_t location[4]; | ||
location[1] = (byteAddress >> 16) & 0xFF; /* MS byte */ | location[1] = (byteAddress >> 16) & 0xFF; /* MS byte */ | ||
Line 129: | Line 129: | ||
/////////////////////////////////// | /////////////////////////////////// | ||
- | PackedUint16 pageAddress | + | PackedUint32 byteAddress |
uint8_t location[4] = {}; /* uninitialized memory is evil */ | uint8_t location[4] = {}; /* uninitialized memory is evil */ | ||
location[1] = byteAddress.h.l; | location[1] = byteAddress.h.l; | ||
Line 135: | Line 135: | ||
location[3] = byteAddress.l.l; | location[3] = byteAddress.l.l; | ||
- | /* alternatively the code above can be written | + | /* alternatively the code above can be written |
- | const uint8_t * const location | + | const uint8_t * const location_arr |
- | */ | + | |
- | </ | + | |