Delphi-PRAXiS
Seite 3 von 3     123   

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Programmieren allgemein (https://www.delphipraxis.net/40-programmieren-allgemein/)
-   -   Index vom x-tem gesetztem Bit (https://www.delphipraxis.net/204489-index-vom-x-tem-gesetztem-bit.html)

Uwe Raabe 3. Jun 2020 11:50

AW: Index vom x-tem gesetztem Bit
 
Zitat:

Zitat von Andreas13 (Beitrag 1466188)
Sind die Variablen-Deklarationen innerhalb der For-Schleife unter XE8 schon zulässig oder sind die Zeilen nur beim Hineinkopieren verrutscht?

Inline-Variablen gibt es erst seit Delphi 10.3, also nein und nein.

einbeliebigername 3. Jun 2020 12:00

AW: Index vom x-tem gesetztem Bit
 
Zitat:

Zitat von Andreas13 (Beitrag 1466188)
Sorry, aber Deine Lösung läßt sich (unter XE5) (noch?) nicht kompilieren.

Sorry, wollte die Inline-Variablen auch mal ausprobieren. Dann aber vergessen die wieder rauszunehmen:

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;

Andreas13 3. Jun 2020 12:03

AW: Index vom x-tem gesetztem Bit
 
Danke Uwe & einbeliebigername!
Andreas

js747a 16. Jun 2020 15:44

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)

himitsu 16. Jun 2020 16:13

AW: Index vom x-tem gesetztem Bit
 
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


Alle Zeitangaben in WEZ +1. Es ist jetzt 18:16 Uhr.
Seite 3 von 3     123   

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz