Bitmanipulation für Anfänger
Jump to navigation
Jump to search
Von Bits und Bytes
Daten-Typen (Variablen-Typen)
- 1 Bit ist die kleinste Einheit
- 1 Bit kann nur 2 Werte annehmen: 0 oder 1 (false oder true)
- Beispiel: 0
- Beispiel: 1
- 1 Byte...
- ...hat 8 Bit
- ein Byte kann 256 (2 hoch 7) Werte annehmen: 0 bis 255
- Beispiel: 0 0 0 0 0 0 0 0 ist der Wert 0
- Beispiel: 1 0 0 0 0 0 0 0 ist der Wert 128
- Beispiel: 1 0 0 0 0 0 0 1 ist der Wert 129
- Beispiel: 1 1 1 1 1 1 1 1 ist der Wert 255
- 1 Integer[1]
- hat 4 Byte (32 Bit)
- kann 4.294.967.296 Werte annehmen: −2.147.483.648 bis 2.147.483.647
- Beispiele
- 0 0 0 0 0 0 0 0 | 0 0 0 0 0 0 0 0 | 0 0 0 0 0 0 0 0 | 0 0 0 0 0 0 0 0 ist der Wert 0
- 0 0 0 0 0 0 0 0 | 0 0 0 0 0 0 0 0 | 0 0 0 0 0 0 0 0 | 0 0 0 0 0 0 0 1 ist der Wert 1
- 0 0 0 0 0 0 0 0 | 0 0 0 0 0 0 0 0 | 0 0 0 0 0 0 0 1 | 0 0 0 0 0 0 0 1 ist der Wert 256
- 0 0 0 0 0 0 0 0 | 0 0 0 0 0 0 0 1 | 0 0 0 0 0 0 0 0 | 0 0 0 0 0 0 0 0 ist der Wert 65.536
- 0 0 0 0 0 0 0 1 | 0 0 0 0 0 0 0 0 | 0 0 0 0 0 0 0 0 | 0 0 0 0 0 0 0 0 ist der Wert 16.777.216
- 0 1 1 1 1 1 1 1 | 1 1 1 1 1 1 1 1 | 1 1 1 1 1 1 1 1 | 1 1 1 1 1 1 1 1 ist der Wert 2.147.483.647
Über die Bits eines Bytes oder Integers
- Das rechteste Bit eines Bytes oder Integer nennt man das LeastSignificantBit[2].
- Das rechteste Bit eines Bytes oder Integer nennt man das MostSignificantBit[3].
Bit-Shifts
- SHL verschiebt die Bits eines Bytes oder Integers nach links
- Beispiele:
var a, b: integer;
a:=1; // a ist: 0 0 0 0 0 0 0 0 | 0 0 0 0 0 0 0 0 | 0 0 0 0 0 0 0 0 | 0 0 0 0 0 0 0 1 b:=a shl 8; // b ist: 0 0 0 0 0 0 0 0 | 0 0 0 0 0 0 0 0 | 0 0 0 0 0 0 0 1 | 0 0 0 0 0 0 0 0 // (Alle Bits wurden um 8 Stellen nach links verschoben)
a:=65.536; // a ist: 0 0 0 0 0 0 0 0 | 0 0 0 0 0 0 0 1 | 0 0 0 0 0 0 0 0 | 0 0 0 0 0 0 0 1 b:=a shl 16; // b ist: 0 0 0 0 0 0 0 0 | 0 0 0 0 0 0 0 1 | 0 0 0 0 0 0 0 0 | 0 0 0 0 0 0 0 0 // (Alle Bits wurden um 16 Stellen nach links verschoben, das rote wurde ins Nirvana verschoben, // das grüne landete tats. 16 Stellen weiter links)
Boolsche Algebra
- Links:
- Integer und andere ordinale Typen in FreePascal (=Lazarus=Delphi): http://www.freepascal.org/docs-html/ref/refsu5.html
- Integer in Wikipedia: http://de.wikipedia.org/wiki/Integer_(Datentyp)
- Fußnoten: