arrays - C# How convert short[] to bool[]? -


short[] sbuf = new short[2]; sbuf[0] = 1; sbuf[1] = 2;  bool[] bbuf = new bool[sbuf.length * 16]; buffer.blockcopy(sbuf, 0, bbuf, 0, sbuf.length * 2);  desired result value   sbuf[0] = 1 bbuf[0] = true, bbuf[1] = false, bbuf[2] = false, bbuf[3] = false... sbuf[0] = 2 bbuf[16] = false, bbuf[17] = true, bbuf[18] = false, bbuf[19] = false... 

but can not converted correctly.
want convert short [] bool [], don't know how.

assuming each bool represents bit corresponding short (which presumably why multiplied size 16) can conversion follows:

bbuf = sbuf     .selectmany(s => enumerable.range(0, 16).select(i => (s & (1<<i)) != 0))     .toarray(); 

the idea construct 16 booleans each short calling enumerable.range, masking number (1 << i), , comparing result zero.


Comments

Popular posts from this blog

inversion of control - Autofac named registration constructor injection -

verilog - Systemverilog dynamic casting issues -

ios - Change Storyboard View using Seague -