User | Kudos |
---|---|
6 | |
4 | |
4 | |
2 | |
2 |
In C when you create a struct, you can create bit fields to assign identifiers to specific bits of a attribute.
For example:
Struct
{
unsigned short toAddress : 5;
unsigned short wordCount : 8;
unsigned short msgType : 3;
unsigned short crc
} TCP_Header;
Once you have created the struct, you can cast the appropriate amount of data words into the struct and then access the structs attributes without having to do bit shifting & masking.
TCP_Header hdr = (TCP_Header) TCP_Stream.readWord();
if(hdr.toaddress == 1901 & hdr.msgType == 6)
{
process Message(hdr);
}
else
{
printf("\n Unexpected Address: %d\n",hdr.toAddress);
}
If we were able to create a cluster and specify which bits each control in the struct represents and then cast the data into said cluster, it would make working with bit packing and low level data manipulation much easier. This would save us from having to do a bunch of masking & shifting.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.