![]() |
AW: Index vom x-tem gesetztem Bit
Zitat:
|
AW: Index vom x-tem gesetztem Bit
Zitat:
Delphi-Quellcode:
function ValueOfNthSetBitV2(const aValue: UInt64; const aValueBitWidth: Byte; const aN: UInt64): Byte;
var vTmp: Array[0..63] of UInt64; vBitCount: Byte; vI: Integer; vBit: UInt64; begin if aValueBitWidth= 0 then raise Exception.Create('Fehlermeldung'); vBitCount:= 0; for vI:= 0 to aValueBitWidth- 1 do begin vBit:= 1 shl vI; if (aValue and vBit)<> 0 then begin vTmp[vBitCount]:= vBit; Inc(vBitCount); end; end; if vBitCount= 0 then raise Exception.Create('Fehlermeldung'); Result:= vTmp[(aN- 1) mod vBitCount]; end; |
AW: Index vom x-tem gesetztem Bit
Danke Uwe & einbeliebigername!
Andreas |
AW: Index vom x-tem gesetztem Bit
Var byte1 : Byte;
If (byte1 AND 1) = 1 then ..... Bit 1 gesetzt (=1) If (byte1 AND 2) = 2 then ..... Bit 2 gesetzt (=1) If (byte1 AND 4) = 4 then ..... Bit 3 gesetzt (=1) If (byte1 AND 8) = 8 then ..... Bit 4 gesetzt (=1) If (byte1 AND 16) = 16 then ..... Bit 5 gesetzt (=1) If (byte1 AND 32) = 32 then ..... Bit 6 gesetzt (=1) If (byte1 AND 64) = 64 then ..... Bit 7 gesetzt (=1) If (byte1 AND 128) = 128 then ..... Bit 8 gesetzt(=1) |
AW: Index vom x-tem gesetztem Bit
Delphi-Quellcode:
Gerade bei Bitmasken arbeite ich gern Hexadezimal, anstatt Dezimal. (binäre Zahlen kann Delphi leider nicht)
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 $01 = 1 $02 = 2 $04 = 4 $08 = 8 $10 = 16 $20 = 32 $40 = 64 $80 = 128 |
Alle Zeitangaben in WEZ +1. Es ist jetzt 14:33 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz