Delphi-PRAXiS
Seite 4 von 4   « Erste     234   

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Programmieren allgemein (https://www.delphipraxis.net/40-programmieren-allgemein/)
-   -   Bitoperationen (https://www.delphipraxis.net/158544-bitoperationen.html)

Dunkelbunt27 25. Feb 2011 16:54

AW: Bitoperationen
 
Hallo,

ich verstehe, was ihr meint.
Auch wenn es vielleicht unsinnig und sinnfrei ist, mach ich erstmal weiter.

Eigentlich setzt man doch ein Bit mit:
Code:
attribut:= attribut or $01;
Und entfernt es wieder mit:
Code:
attribut := attribut and not $01;
Oder?

Ich mache das wie folgt:
Button1:
Code:
attribut:= attribut or $01;
attribut := attribut and not $02;
Byte = 1

Button2:
Code:
attribut:= attribut or $02;
attribut := attribut and not $01;
Byte = 00000011

Es müsste doch 00000010 rauskommen!?

Fg Dunkelbunt

DeddyH 25. Feb 2011 16:57

AW: Bitoperationen
 
Du meinst 10 binär? Ja, das sollte eigentlich so sein. Lässt Du Dir auch zum richtigen Zeitpunkt den aktuellen Wert ausgeben?

Dunkelbunt27 25. Feb 2011 17:09

AW: Bitoperationen
 
Ja ich meine binär.
Ich denke der Zeitpunkt stimmt. Immer wenn ich mit den Pfeiltasten die PositionSpalte bzw PositionZeile bewege wird der Ihalt des Bytes angezeigt, sprich Zeichen und dazugehöriger binärer Wert ...

Edit:
Ich kenne jetzt den Fehler. Es lag daran, dass zwar die eigentlich Berechnung stimmte, sie aber für das falsche Feld ausgeführt wird.

DeddyH 25. Feb 2011 17:24

AW: Bitoperationen
 
Ich hab mal schnell ein Beispielprojekt erstellt, da stimmt alles.
Delphi-Quellcode:
procedure TForm5.Button1Click(Sender: TObject);

  function ByteToString(b: Byte): string;
  const
    BitChars: array [Boolean] of Char = ('0', '1');
  var
    Bit, Posi: Byte;
  begin
    SetLength(Result, SizeOf(b) shl 3);
    Bit := 1;
    Posi := Length(Result);
    while Posi > 0 do
      begin
        Result[Posi] := BitChars[b and Bit = Bit];
        Bit := Bit shl 1;
        dec(Posi);
      end;
  end;

var
  attribut: Byte;
begin
  attribut := 0;
  attribut := attribut or $01;
  attribut := attribut and not $02;
  ShowMessage(ByteToString(attribut));
  attribut := attribut or $02;
  attribut := attribut and not $01;
  ShowMessage(ByteToString(attribut));
end;

Dunkelbunt27 25. Feb 2011 17:52

AW: Bitoperationen
 
Danke für das Beispiel.
Dein ByteToStr ist echt gut, ich hatte es ein wenig anderst gemacht, aber das ist nicht so wichtig.
An dieser Stelle funktioniert alles was funktionieren soll, denke ich.
Ich muss jetzt nur noch interne Probleme lösen...

Danke auch an die Andere hilfsbereiten DP-Member, Dunkelbunt

Aphton 25. Feb 2011 20:02

AW: Bitoperationen
 
Zitat:

Zitat von Dunkelbunt27 (Beitrag 1084407)
Eigentlich setzt man doch ein Bit mit:
Code:
attribut:= attribut or $01;
Und entfernt es wieder mit:
Code:
attribut := attribut and not $02;
Oder?

Ich mache das wie folgt:
Button1:
Code:
attribut:= attribut or $01;
attribut := attribut and not $02;
Byte = 1

Button2:
Code:
attribut:= attribut or $02;
attribut := attribut and not $01;
Byte = 00000011

Ich verstehe nicht, warum du immernoch solche Fehler machst:
Code:
// "Eigentlich setzt man doch ein Bit mit:"
attribut:= attribut or $01;
// "Und entfernt es wieder mit:"
attribut := attribut and not $02;
Byte = 1
Im Grund hast du ja schon Recht mit dem Entfernen aber du entfernst nicht das gesetzte Bit, sondern das zweite Bit.
Sagen wir einmal, Attribut habe den Wert 0.

attribut:= attribut or $01 :
Code:
   00000000
or 00000001
-----------
   00000001
attribut := attribut and not $02;
Code:
not 2:
2     = 00000010
not 2 = 11111101

    00000001
and 11111101
------------
    00000001

Du erhälts also 1 binär, was 2^0 Dezimals entspricht,

Dunkelbunt27 26. Feb 2011 14:20

AW: Bitoperationen
 
Hallo,
Zitat:

Im Grund hast du ja schon Recht mit dem Entfernen aber du entfernst nicht das gesetzte Bit, sondern das zweite Bit.
Mein Fehler, sorry, im Programm habe ich immer das richtige Bit "entfernt" weil ich ja wenn ich das erste Bit setze das 2. Ausschalte und nicht gleich wieder das erste Bit ausschalte. Der Fehler ist durch das kopieren aus dem Quelltext entstande, soweit hab ich das Prinzip schon verstanden. Trotzdem hast du recht, das es in diesem Kontext nicht richtig geschrieben ist.

FG Dunkelbunt


Alle Zeitangaben in WEZ +1. Es ist jetzt 12:31 Uhr.
Seite 4 von 4   « Erste     234   

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