I am a newbie for solving these kind of problems. I need to extract variable no of bits from a single short value.
Like If I have read something from an array and need to fill another array first reading the 10 bits from earlier read value , and then again 6 bits to another short.
Like:
int pixelNo = 0;
short pixelValue_part = pixels[pixelNo];
// but here i need only 10 bits , in the second
// iteration i might need 4 bits and then so on so forth.
After reading these shorts in parts, I will have to put these parts into
second array sequentially inorder to arrange all pixels in the sequence.
Note :
The problem is of arranging all input pixel sequence in ascending ordered way.I have to arrange the pixels of each having size of 10 bits. For this reason I would need read first 10 bits of short.
Edit:
| | 0,1 512,513 1024,1025 1536,1537 1,2,3 513,514,515 1025,1206,1027 1357,1538,1539
|_|____|___|___|_|___|______|______
I have above array as input and I want to produce output like the following array.
| | 0,1 2,3 4,5 6,7 …… 513,514,515,1024,1025,1536,1537…
|_|____|___|___|_|___|______|______
Values of arrays all are Pixels of some image. So in actual the image was unarranged in pixels in input array, and then the second array is the array of arranged (sorted ) pixels.
Assuming your original data is in
src, and you want a span ofnbits (startingn+posbits from the low end), this will extract those bits:Breaking it down:
(1<<n)-1is a mask ofn1’s (binary)src >> posslides the bits you want down to the “bottom” of the variableYou can do this for each piece you need. To put the pieces together, you’d use
<<to shift pieces where you need them to be and then|(bitwise-or) the pieces together.