Reputation: 1949
I want to make a lookup-table/array.
The index values I get are of the form k = 2^n only, where n is an integer. So I want to reduce the arrays size to n and I therefore have to perform an operation on the index values as well.
I want the most efficient way to do this as I am programming on an embedded platform.
Example:
I got the values n = {1, 2, 4, 8, 16, 32}
I have an array defined as:
int myArray[6];
Now I want to convert the values n to m, where m = {1, 2, 3, 4, 5, 6} so I can access array elements:
myArray[m];
Upvotes: 0
Views: 84
Reputation: 53016
This is a solution
#include <stdio.h>
int main()
{
int array[6] = {1, 2, 4, 8, 16, 32};
int index[6];
int i;
for (i = 0 ; i < 6 ; i++)
{
int value;
value = array[i];
index[i] = 0;
while ((value >>= 1) != 0)
index[i] += 1;
printf("%d\n", index[i]);
}
return 0;
}
Upvotes: 1