AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren

Index vom x-tem gesetztem Bit

Ein Thema von calibra301 · begonnen am 1. Jun 2020 · letzter Beitrag vom 16. Jun 2020
Antwort Antwort
Seite 3 von 3     123
Benutzerbild von Uwe Raabe
Uwe Raabe

Registriert seit: 20. Jan 2006
Ort: Lübbecke
10.934 Beiträge
 
Delphi 12 Athens
 
#21

AW: Index vom x-tem gesetztem Bit

  Alt 3. Jun 2020, 12:50
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.
Uwe Raabe
Certified Delphi Master Developer
Embarcadero MVP
Blog: The Art of Delphi Programming
  Mit Zitat antworten Zitat
einbeliebigername

Registriert seit: 24. Aug 2004
140 Beiträge
 
Delphi XE8 Professional
 
#22

AW: Index vom x-tem gesetztem Bit

  Alt 3. Jun 2020, 13:00
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;
Mit freundlichen Grüßen, einbeliebigername.
  Mit Zitat antworten Zitat
Andreas13

Registriert seit: 14. Okt 2006
Ort: Nürnberg
709 Beiträge
 
Delphi XE5 Professional
 
#23

AW: Index vom x-tem gesetztem Bit

  Alt 3. Jun 2020, 13:03
Danke Uwe & einbeliebigername!
Andreas
Grüße, Andreas
Wenn man seinem Nächsten einen steilen Berg hinaufhilft, kommt man selbst dem Gipfel näher. (John C. Cornelius)
  Mit Zitat antworten Zitat
js747a

Registriert seit: 11. Aug 2008
Ort: Nord hessen
43 Beiträge
 
Delphi 7 Enterprise
 
#24

AW: Index vom x-tem gesetztem Bit

  Alt 16. Jun 2020, 16:44
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)
  Mit Zitat antworten Zitat
Benutzerbild von himitsu
himitsu
Online

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

AW: Index vom x-tem gesetztem Bit

  Alt 16. Jun 2020, 17: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 18:34 Uhr)
  Mit Zitat antworten Zitat
Themen-Optionen Thema durchsuchen
Thema durchsuchen:

Erweiterte Suche
Ansicht

Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 10:48 Uhr.
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