I have a particular byte where each bit in the byte depends on some other value or information. In particular, one byte is formatted as follows:
Bits 1-3 = 011
Bits 4-7 = binary value of char at that position
Bit 8 = 1 or 0 depending on a 2nd parameter
Thus, I thought I might replace code like:
if (last == TRUE) {
callsign[j] = 0b01100001;
} else {
callsign[j] = 0b01100000;
}
with the simple two-liner:
char mask[];
sprintf("%s%i", 0b1111111, last);
callsign[j] = 0b01100001 & mask;
Unfortunately, that didn't work, generating a ton of errors, among them an Attempt to create a pointer to a constant
, which I can't decipher.
Essentially, either way, I need to create a byte composed of individual bits or groups of bits in a specific order. Inevitably, they will be variables, and somehow I need to concatenate them into a byte.
I was thinking masks would be the way to go, but even if I opt for a mask, I somehow need to concatenate a parameter into the mask.
What's the best way to go about this? Using masks seems convenient, but how can I create a mask by combining variables with binary?