## concatenate array to bits

matlab extract bits

bitget matlab

bit array in c

matlab fi

bit array java

bit array c++

bit array c#

How can I achieve the following concatenation of 1s and 0s of low the density array `data`

into a smaller more densely filled array `c`

uint8_t data[16] = {1, 1, 0, 1, 1, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0, 1}; uint8_t c[2] = {0}; /* desired result c[0] = 11011011 c[1] = 10001101 */

I am a little bit struggeling here, up to now, I have got it like, but it does not seem to work like I expected:

static void compress(unsigned char *out, unsigned char *in, int length) { for(int i=0; i<length; i++) { if((i+1)%9==0) out++; *out |= in[i]<<((length-i)%9); } } int main(){ compress(c,data,16); printf("%d",c[0]); //should be 219 printf("%d",c[1]); //should be 177 (edit) }

thank you for helping me out!

Add one line to your code `printf ("%d %d\n", i,(length-i)%9 );`

and you see where the problem is.

Don't use modulo operation, add variable which helds the bit-shift number (initial value is 7) and reset it when it is negative:

static void compress2(unsigned char *out, unsigned char *in, int length) { int shift = 7; for(int i=0; i<length; i++) { *out |= in[i] << shift; if (--shift < 0) { ++out; shift = 7; } } }

**Concatenate bits of fi objects - MATLAB bitconcat,** This MATLAB function concatenates the bits of the elements of fixed-point fi input array, a. The output array has word length equal to the sum of the word lengths of the inputs and a fraction length of zero. The bit representation of the stored integer is in two's complement representation. Scaling does not affect the result type and value. If the inputs are all scalar, then bitconcat concatenates the bits of the inputs and returns a scalar.

static void compress(uint8_t *out, uint8_t *in, size_t length) { memset(out, 0, length >> 3 + !!(length & 7)); for(size_t i = 0; i < length; i++) { out[i >> 3] |= (in[i] << (7 - (i & 7))); //out[i >> 3] |= ((!!in[i]) << (7 - (i & 7))); - if array elements may be not only 0 or 1. } } int main() { uint8_t data[16] = {1, 1, 0, 1, 1, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0, 1}; uint8_t c[sizeof(data) >> 3 + !!(sizeof(data) & 7)]; compress(c,data,16); printf("%d\n",c[0]); //should be 219 printf("%d\n",c[1]); //should be 141 }

**Concatenate 3 bytes array of real time serial data into single ,** time serial Learn more about serial port, binary, concatenation, single precision. Now I want to convert this 3 byte array into one single precision number. The concat() method of Bytes Class in the Guava library is used to concatenate the values of many arrays into a single array. These byte arrays to be concatenated are specified as parameters to this method. For example: concat(new byte[] {1, 2}, new byte[] {3, 4, 5}, new byte[] {6, 7}.

Size of unit8_t is only 8 bits, not 9. So fundamentally, the error is in %9 instead of %8.

You can use separate counter (b) for bit shift amount from 7 down to 0. Once it reaches -1, advance out. Start with (length-1), because for length 1 you want it to be lowest bit, that is with shift amount 0.

static void compress(unsigned char *out, unsigned char *in, int length) { for(int* p=in, int b=(length-1)%8; p<in+length; p++, b--) { if(b < 0) { out++; b = 7; } *out |= *p << b; } }

Also you can use something like out[(length-i-1) / 8] |= in[i] << (length-i-1) % 8

P.S. Bit order is platform dependant...

**Merge two 8 bit arrays to one 16 bit array,** It's better to avoid dynamic memory allocation on an Arduino, with only 2 KB of SRAM memory. Note that your arrays cost 2 (bytes/int) * (8 + 8 + One way of concatenating multiple arrays is by using Build Array function. This function works in two modes: either appending elements to an n-dimensional array, which is the default mode, or concatenating multiple arrays. The Build Array function will work in concatenate mode when Concatenate Inputs has been selected from the shortcut menu. You can access the shortcut menu by right clicking on the VI.

**Bit array,** https://docs.scipy.org › doc › numpy › reference › generated › numpy.pac The Concatenate block operates in either vector or multidimensional array concatenation mode, depending on the setting of its Mode parameter. In either case, the block concatenates the inputs from the top to bottom, or left to right, input ports.

**PackBits - Numpy and Scipy,** I can store the three bytes into an array of bytes. channels comes in the form of 3 consecutive bytes (the first byte being the MSB and so on). The simplest and straight forward way to concatenate rows into a string value is to use FOR XML PATH in a select query. This FOR XML PATH method can be used in SQL Server version 2005 and higher. In the below sample query, I’ve concatenated multiple rows of the column “CountryName” to a single string and added a comma between the country

**Concatenate Bytes into single value,** In ES5 you can merge two Arrays using the .concat() method. var fruits = ['apples', 'peaches']; var vegetables = ['potatoes', 'tomatoes']; var all Here are the detailed steps: Select a cell where you want to enter the formula. Type =CONCATENATE( in that cell or in the formula bar. Press and hold Ctrl and click on each cell you want to concatenate. Release the Ctrl button, type the closing parenthesis in the formula bar and press Enter.

##### Comments

- thanks, worked! endianess turned out to be different, so i started with shift=0 and incremented it, until ++shift > 7 when i set shift to 0 again.
- thank you, like the other solutions, this worked fine as well
- thank you! also a valid solution, also thank you for the endianess hint