Relative Content

Tag Archive for binaryserial-port

How to pack 32 numeric values into one 32 bit variable?

Our application consists of a hardware device and an application run on a computer.
Between these we have a communication, where the binary (on/off) state of different channels is interchanged. For this we have so far used a 32 bit variable, using one bit for each channel.
Now we need to expand the granularity of the information exchanged, so we get more than just a binary (on/off) information for each channel. We need to exchange a value of 0-50 for each channel.
Of course we could just transfer an array of bytes with 32 fields instead of only one 32 bit variable, but due to backwards compatibility, and also communication speed, we would like to stick with only one variable.
Considering that one 32 bit unsigned variable can hold 4,294,967,296 different numbers, we think that it must be possible to communicate our 32 channels with each a number between 0 and 20 somehow. I just can’t wrap my head around a suitable way of packing this.
Can anyone point me in the direction of how to pack 32 values of 0-50 into one 32 bit variable?
Any help is much appreciated.