combining bits to make byte array

Hi All,

I need a way to create a byte array from various size "bit" arrays?

Let me try explain in an example...

X is represented by 7bits... I have 10 X's (70bits)
Y is represented by 3bits... I have 3 Y's (9bits)
Z is represented by 5bits... I have 1 Z (5bits)

I need to be able to individually set all X,Y,Z. then I need to represent this as a stream of 84bits meaning I need to combine XYZ into a single 11byte array.

Any advice?

Apologies if the question in vague.

Regards,

Use an array of 10 integers and shift X,Y and Z into each integer.

(High byte) 0ZZZZZYY (Low byte)YXXXXXXX
Then when you want to use the bits, just mask them off.

Another solution is to use a struct and maybe a Union.

StudentSA:
Apologies if the question in vague.

Don't apologize. Just make it less vague.

Do you mean that X is represented by 01111111, Y by 00000111 and Z by 00011111

If not, what do you mean ?

...R

Thanks, @Hazardsminds how do I shift it through?
@Robin2, yip exactly. But I need to concatenate xyz into a byte array.

Lets say I have 1 of each... then the resultant byte array must be

XXXXXXXY YYZZZZZ0

You have the regular shift left and right (<< and >>) operators, which you can shift in yourself or you can use the already made function, bitWrite().

https://www.arduino.cc/en/Reference/BitWrite

Or if everything is already in bytes, you can just shift X_byte to the left 9 times followed by Y_byte 6 times and Z_byte 1 time.

HBYTE |= (XBYTE << 1) | (YBYTE >> 2);
LBYTE |= (YBYTE << 6) | (ZBYTE << 1);

? I think. Assuming your data is in the form Robin2 suggested.

You may have to be careful of sign-extension problems.
Make sure you're working on unsigned data-types.

I am still confused.

You say the data is as I postulated in Reply #2 which (to my mind) means that you have 14 bytes 10 of which have 1 leading 0, 1 has 3 leading zeros and 3 have 5 leading zeros.

Then you seem to want to compact them into a collection of 11 bytes - presumably the first one has 4 leading zeros and the other 10 are all 1s.

If that is the output you want there is no need to worry about the input. Just create an array of 11 bytes all having a value 255 except the first which has the value 15.

However ... how on earth could you extract the original data - assuming that is what is required.

What are you not telling us ?

...R

Lets see if I can clarify this...

I have an led driver that is controlled via a serial interface tlc5955. U need to write a 769bit "common" shift register.

***Side gripe... who makes registers not a multiple of 8bits???

Anyhow... this register is split up on a bit level... so sets of bits do different things. I am trying to write the control data. It is made up of 48x7bits dot correction, 3x3bit maximum current, 3x7bit global brightness and 1x5bit function control.

I need to write this out the spi line as bytes. For example the 48x7bit dot correction is an array of byte[48] in the code.this allows me to set each one. But to write this out I need only the 7bits from each byte to be joined into a array of 42bytes.

StudentSA:
Lets see if I can clarify this...

I need to write this out the spi line as bytes. For example the 48x7bit dot correction is an array of byte[48] in the code.this allows me to set each one. But to write this out I need only the 7bits from each byte to be joined into a array of 42bytes.

Ahh - I think the fog is clearing ...

When I suggested in Reply #2 that (for example) X is represented by 01111111 I meant that literally and you did not correct me to explain that the 7 bits can vary depending on the value involved. I had thought that you were using that bit pattern to represent the character 'X' rather than using the standard ascii code.

I can see what needs to be done now, but I don't have a quick answer.

For example the first byte just needs to be shifted 1 bit to the left to lose the leading zero and leave the low bit "empty"
Then the next byte also needs to be shifted 1 but to the left but then its high bit must be placed into the low bit position of the first byte and the remainder of the second byte must be shifted a further place left so as to leave the 2 low bits "empty".
The process for the 3rd byte is similar except that the 2 high bits must go as the low bits in the second byte
and repeat ad nauseum ...

It could be done as a series of steps like that.

...R

Hint: Looking at the spec for this device (TLC5955) leads me to the conclusion that it is easier to let the compiler do the work of packing the data structure.

Cheers!

Mr kowalski, can u elaborate further?

I coded a solution where i loop through all the bits of the final register and insert the data from the individual bytes. Messy but should work.

Here is a starting point Bit field - Wikipedia

Cheers!

kowalski:
Here is a starting point Bit field - Wikipedia

I don't see anything in that link to suggest a simple way of "packing" the bits in the way the OP wants.

...R