Ich habe ein Array von neun Elementen und einer ganzen Zahl. Mit der Ganzzahl und Bitmaskierung möchte ich Informationen darüber speichern, ob ein Element des Arrays gültig ist.Einfache Möglichkeit, das n-te Array-Element zu bekommen, wenn alles, was ich habe, ist n^2
Also meine Frage ist: Gibt es einen einfacheren Weg als mit log2() von math.h von meiner Bitmaske zu meinem Array-Element Nummer zu bekommen? Zum Beispiel speichere ich foo2,
foo4
und foo5
in mValue
mit Bitmaskierung. Dann möchte ich das Array-Element an der Position foo2 = 2
, das ist das zweite Bit, also das zweite Array-Element, das 34
ist. Aber der einzige Weg, an den ich denken kann, ist die log2(n)
Funktion. Gibt es einen einfacheren mit Bitverschiebung?
Einfach gesagt, ich möchte, dies zu tun:
Ist das n-te Bit einer ganzen Zahl mValue
auf 1 gesetzt? dann hol mir das n-te Element eines Arrays bar
.
#include <math.h>
const int foo1 = 1;
const int foo2 = 2;
const int foo3 = 4;
const int foo4 = 8;
const int foo5 = 16;
int bar[9] = {23,34,82,8,7,0,23,19,20};
int mValue;
void SetValue(int nVal)
{
mValue = nVal;
}
bool IsElementValid(int nVal)
{
return mValue & nVal;
}
int main()
{
SetValue(foo2 | foo4 | foo5);
IsElementValid(foo4); //true
IsElementValid(foo5); //true
IsElementValid(foo1); //false
//access array element 1 with value foo2 (2)
if(IsElementValid(foo2))
printf("%d\n", bar[log2(foo2)]); // output: '34'
}
[diese Frage und Antworten] (http://stackoverflow.com/questions/757059/position-of-least-significant-bit-that-is-set) könnte für Sie nützlich sein – mvidelgauz