Bit-Manipulationen: Die Welt der Bits
Erstellt von
Daniel Schuhmann
Jan
24
Zu Beginn des Computerzeitalters waren Programmierer ganz automatisch »bitfest«: Frühe Digitalrechner hatten noch keine QWERTZ-Tastatur, sondern erlaubten nur die bitweise Eingabe. Bei heutigen Computersystemen sind die Bits deutlich in den Hintergrund gedrängt worden, obgleich auch noch heute alles auf ihnen basiert.
Die folgenden Codeschnipsel erlauben auch für ungeübte auf einfache Weise das Setzen, Löschen und Abfragen von Bits.
1
2
3
4
| function GetBit(const Value: DWord; const Bit: Byte): Boolean;
begin
Result := (Value and (1 shl Bit)) <> 0;
end; |
1
2
3
4
| function ClearBit(const Value: DWord; const Bit: Byte): DWord;
begin
Result := Value and not (1 shl Bit);
end; |
1
2
3
4
| function SetBit(const Value: DWord; const Bit: Byte): DWord;
begin
Result := Value or (1 shl Bit);
end; |
1
2
3
4
5
| function EnableBit(const Value: DWord; const Bit: Byte;
const TurnOn: Boolean): DWord;
begin
Result := (Value or (1 shl Bit)) xor (DWord(not TurnOn) shl Bit);
end; |
Abgelegt unter:
Code-Schnipsel
Keine Kommentare
Sie müssen angemeldet sein, um einen Kommentar zu schreiben.