It probably wouldn't make such a big difference, as bit-shiting by
multiples of 8 bits is likely very efficient. The union trick may,
however, make your code easier to read and write:
union Packet {
struct {
uint8_t address;
uint8_t command;
int32_t data;
uint8_t checksum;
};
uint8_t bytes[7];
};
void send_a_packet() {
Packet packet;
packet.address = 0x10;
packet.command = 0x32;
packet.data = 0xba987654;
packet.checksum = 0xdc;
// now send packet.bytes through the link.
}
Warnings:
This works on AVR-based arduinos, because they are 8-bit micros that
have no alignment constrains. On other architectures you may need to
add __attribute__((packed)) to the struct.
This assumes that both endpoints of the communication are
little-endian, and agree that the data is to be transmitted in
little-endian order. If you work with mixed endianness, you may want
to stick with bit-shifting, or use something like htonl().