Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
You are viewing an older revision! See the latest version
Hilfestellung
Der übliche Ausdruck, mit dem man Probleme zu haben scheint, ist "(1 << position)". Alles was dies bewirkt ist, eine Maske mit einem einzelnen Bit zu erstellen, die mit jedem Integer-Typ funktioniert. Das Argument "position" gibt die Position an, an der das Bit sein soll. Wenn position = 0 ist, ergibt dieser Ausdruck:
0000 0000 0000 0000 0000 0000 0000 0001b
Wenn position = 8, wird ausgewertet:
0000 0000 0000 0000 0000 0001 0000 0000b
Mit anderen Worten, es wird einfach ein Nullenfeld mit einer 1 an der angegebenen Position erstellt.
Sobald die Maske erstellt ist, wird sie den Operatoren bitweise und (&) oder (|) und xor (^) auf das Argument angewendet.
Achtung!
auf die Datentypen: uint8_t, char, short, int oder long.
Weitere Anwendungen (wobei in C/C++ häufig Kombinierte Zuweisungen verwendet werden:
lösche das 3. Bit:
x &= ~(1<<3) // x = x anded with the inverse of (0x01 shifted left 3 times)
setze das 5. und 7. Bit
// x = x ored with (0x01 shifted left 5 times) ored with (0x01 shifted left 7 times) x |= (1 << 5) | (1 << 7);
überprüfe das 2. Bit og es 1 ist
if (x & (1<<2)) { ... }
Das obige nutzt die C-Eigenschaft aus, dass 0 false ist, alles andere ist true. Wenn es schwer fällt, dem zu folgen, kann immer folgendes verwendet werden:
if ( (x & (1<<2)) == (1<<2)) // if x anded with 0x02 == 0x02 { ... }