Tutorial :Assign unsigned char to unsigned short with bit operators in ansi C


I know it is possible to assign an unsigned char to an unsigned short, but I would like to have more control how the bits are actually assigned to the unsigned short.

unsigned char UC_8;  unsigned short US_16;    UC_8 = 0xff;  US_16 = (unsigned char) UC_8;  

The bits from UC_8 are now placed in the lower bits of US_16. I need more control of the conversion since the application I'm currently working on are safety related. Is it possible to control the conversion with bit operators? So I can specify where the 8 bits from the unsigned char should be placed in the bigger 16 bit unsigned short variable.

My guess is that it would be possible with masking combined with some other bit-operator, maybe left/right shifting.

UC_8 = 0xff;  US_16 = (US_16 & 0x00ff) ?? UC_8; // Maybe masking?  

I have tried different combinations but have not come up with a smart solution. I'm using ansi C and as said earlier, need more control how the bits actually are set in the larger variable.

EDIT: My problem or concern comes from a CRC generating function. It will and should always return an unsigned short, since it will sometimes calculate an 16 bit CRC. But sometimes it should calculate a 8 bit CRC instead, and place the 8 bit on the eight LSB in the 16 bit return variable. And on the eight MSB should then contain only zeros.

I would like to say something like:

US_16(7 downto 0) = UC_8;   US_16(15 downto 8) = 0x00;  

If I just typecast it, can I guarantee that the bits always will be placed on the lower bits in the larger variable? (On all different architectures)


What do you mean, "control"?

The C standard unambiguously defines the unsigned binary format in terms of bit positions and significance. Certain bits of a 16-bit variable are "low", by numerical definition, and they will hold the pattern from the 8-bit variable, the other bits being set to zero. There is no ambiguity, no wiggle room, and nothing else to control.


Maybe rotation of bits will help you:

US_16 = (US_16 & 0x00ff) | ( UC_8 << 8 );  

Result in bits will be:
C - UC_8 bits
S - US_16 bits
CCCC CCCC SSSS SSSS, resp.: SSSS SSSS are last 8 bits of US_16

But if UC_8 was 1 and US_16 was 0, then US_16 will be 512. Are you mean this?

US_16 = (US_16 & 0xff00) | ( UC_8 & 0x00ff );  



Is this what you want?


If it is important to use ansi C, and not be restricted to a particular implementation, then you should not assume sizeof(short) == 2. And why bother to cast an unsigned char to an unsigned char (the same thing)? Although probably safe to assume char is 8 bits nowadays, even though that's not guaranteed.

uint8_t UC_8;  uint16_t US_16;  int nbits = ...# of bits to shift...;  US_16 = UC_8 << nbits;  

Obviously, if you shift more than 15 bits, it may not be what you want. If you need to actually rearrange the bits, rather than just shift them to some position, you'll have to set them individually

int sourcebit = ...0 to 7...;  int destinationbit = ...0 to 15...;  // set  US_16 |= (US_8 & (1<<sourcebit)) << (destinationbit - sourcebit);  // clear  US_16 &= ~((US_8 & (1<<sourcebit)) << (destinationbit - sourcebit));  

note: just wrote, didn't test. probably not optimal. blah blah blah. but something like that will work.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »