I'm tring to turn an array on it's ear...bitwise. What I want to do
is the following...
suppose I have the following array (each element represents a single
bit)
array1 = {1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
I view it as follows:
array[0] = 1111
array[1] = 0000
array[2] = 0000
array[3] = 0000
I want to transform it to array2 so that I get
array2[0] = 1000
array2[1] = 1000
array2[2] = 1000
array2[3] = 1000
I've written the following code that does what I want:
(code follows)
_____
#include <iostream>
using namespace std;
int main(){
unsigned int arraysize = 32;
unsigned long array[32] = {};
unsigned int seedsize = 32;
unsigned long seed[32] = {};
/*
for(int i = 0; i < seedsize ; i++)
seed[i] = 1;
*/
seed[0] = 0xffffffff;
int bits_per_word = 8 * sizeof(unsigned long);
unsigned int modgroup = arraysize / bits_per_word;
unsigned long ifactor;
for(int i = 0; i < seedsize; i++){
ifactor = (bits_per_word * (i % modgroup));
for(unsigned long j = 0; j < bits_per_word; j++){
array[ifactor+j] |= (((seed[i] & (1<<j))>>j)<<(i/modgroup));
}
}
for(int i = 0; i < arraysize; i++)
cout << seed[i] << " " << array[i] << endl;
cout << endl;
}
____
end code
This works fine. However I'm wondering if there is a beter way to do
this line:
array[ifactor + j] |= (((seed[i] & (1 << j)) >> j) << (i/modgroup));
?
Any suggestions?
It seems like there's got to be an easier way than doing 3 shift
operations...
Thanks in advance for any help you can offer.