[Anmerkungen zum Algorithmus] Binäre Operationen in C++

Behalte das niedrigste Bit 1----x & (-x)

int x  = 0b00001010;
//-x = 0b11110110

int lsb= ( x == INT_MIN ? x: x& (-x) );
// lsb(lowestSignificantBit)

>>> lsb = 0b0000 0010

Entfernen Sie das niedrigste Bit 1----x&(x-1)

int x = 0b00000110;
//x-1 = 0b00000101

int non_lsb = x & (x-1);

>>> non_lsb = 0b00000100

Integrierte binäre C++-Funktionen

Ermitteln Sie die Anzahl von 1

Zählen Sie, wie viele Bits einer 32-Bit-Ganzzahl ohne Vorzeichen 1 sind

int num = 9;
int cnt_one = __builtin_popcount(num);
//cnt_bit == 2;

Guess you like

Origin blog.csdn.net/LittleSeedling/article/details/121058205