Einzelnen Beitrag anzeigen

Benutzerbild von himitsu
himitsu

Registriert seit: 11. Okt 2003
Ort: Elbflorenz
43.114 Beiträge
 
Delphi 12 Athens
 
#25

AW: Index vom x-tem gesetztem Bit

  Alt 16. Jun 2020, 16:13
Delphi-Quellcode:
if byte1 and 1 = 1 then
if byte1 and 2 = 2 then
if byte1 and 4 = 4 then
if byte1 and 8 = 8 then
...

// keine Redundanzen: erstmal die unnötig doppelten Zahlen entfernt

if byte1 and 1 <> 0 then
if byte1 and 2 <> 0 then
if byte1 and 4 <> 0 then
if byte1 and 8 <> 0 then
...

// und dann gibt es noch viele andere Wege, um das letzte/rechte Bit zu prüfen

if Odd(byte1 {shr 0}) then // if (byte1 {shr 0}) and 1 <> 0 then
if Odd(byte1 shr 1) then // if (byte1 shr 1) and 1 <> 0 then
if Odd(byte1 shr 2) then // if (byte1 shr 2) and 1 <> 0 then
if Odd(byte1 shr 3) then // if (byte1 shr 3) and 1 <> 0 then
...

// oder man nutzt direkt vorhandene Bit-Operationen

//type TByteSet = set of 0..7;
if 1 in TByteSet(byte1) then
if 2 in TByteSet(byte1) then
if 3 in TByteSet(byte1) then
...

// bzw. direkt die Variable "byte1" als diesen Typ definieren, ohne ständige Konvertierung
Gerade bei Bitmasken arbeite ich gern Hexadezimal, anstatt Dezimal. (binäre Zahlen kann Delphi leider nicht)
$01 = 1
$02 = 2
$04 = 4
$08 = 8
$10 = 16
$20 = 32
$40 = 64
$80 = 128
Garbage Collector ... Delphianer erzeugen keinen Müll, also brauchen sie auch keinen Müllsucher.
my Delphi wish list : BugReports/FeatureRequests

Geändert von himitsu (16. Jun 2020 um 17:34 Uhr)
  Mit Zitat antworten Zitat