Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Programmieren allgemein (https://www.delphipraxis.net/40-programmieren-allgemein/)
-   -   LowByte und Highbyte beschreiben (https://www.delphipraxis.net/106818-lowbyte-und-highbyte-beschreiben.html)

Alex_ITA01 17. Jan 2008 08:19


LowByte und Highbyte beschreiben
 
Hallo zusammen,
ich habe folgende Situation:
Auf einen SmallInt soll ich einmal ein Highbyte schreiben und einmal ein Lowbyte.

LowByte:
SmallInt := MeinWert and $0000FFFF

HighByte:
SmallInt := MeinWert shr 16

Das geht auch soweit aber wie ist es möglich, dass wenn ich das Highbye beschreiben will auf mein SmallInt, dass ich das vorhandene LowByte nicht mit überschreibe (es soll also vorhanden bleiben). Andersrum genauso. Wenn ich das LowByte beschreibe, soll das Highbyte nicht mit beschrieben werden.

Ich hoffe ihr könnt mir da helfen...
Danke
MFG Alex

sirius 17. Jan 2008 08:23

Re: LowByte und Highbyte beschreiben
 
am besten etwa so:
Delphi-Quellcode:
type TmySmallint= {packed} record
       case Boolean of
         True: (SI:smallint);
         False: (LoByte:byte; HiByte:Byte);
end;
Damit hast du einen neuen Typ, den du verwenden kannst.


Edit:
btw.: Was ist denn bei dir Smallint? Mit (MeinWert and $0000FFFF) besetzt du den gesamten Smallint und nicht nur das LoByte.

DeddyH 17. Jan 2008 08:51

Re: LowByte und Highbyte beschreiben
 
Mal aus dem Kopf:
Delphi-Quellcode:
procedure SetLowByte(var sInt: SmallInt; const b: Byte);
begin
  sInt := sInt or b;
end;

procedure SetHighByte(var sInt: SmallInt; const b: Byte);
begin
  sInt := sInt or (b shl 8);
end;
P.S.: Sirius hat übrigens Recht, bei $0000FFFF sind bereits alle Bits gesetzt.

uligerhardt 17. Jan 2008 08:52

Re: LowByte und Highbyte beschreiben
 
Oder
Delphi-Quellcode:
uses
  SysUtils;

  //...
  WordRec(YourSmallint).Lo := 1;
  WordRec(YourSmallint).Hi := 2;

Muetze1 17. Jan 2008 09:13

Re: LowByte und Highbyte beschreiben
 
Zitat:

Zitat von DeddyH
Mal aus dem Kopf:
Delphi-Quellcode:
procedure SetLowByte(var sInt: SmallInt; const b: Byte);
begin
  sInt := sInt or b;
end;

procedure SetHighByte(var sInt: SmallInt; const b: Byte);
begin
  sInt := sInt or (b shl 8);
end;

Deine Funktionen haben ein Problem: was ist, wenn schon ein Wert <> 0 in dem jeweiligen Byte Anteil steht? Du fügst deine Bits mit oder hinzu - aber die schon bestehenden bleiben stehen. Somit erreichst du damit nicht sicher das, was gefordert war bzw. du eigentlich erreichen wolltest.

DeddyH 17. Jan 2008 09:16

Re: LowByte und Highbyte beschreiben
 
Zitat:

Zitat von Alex_ITA01
Das geht auch soweit aber wie ist es möglich, dass wenn ich das Highbye beschreiben will auf mein SmallInt, dass ich das vorhandene LowByte nicht mit überschreibe (es soll also vorhanden bleiben). Andersrum genauso. Wenn ich das LowByte beschreibe, soll das Highbyte nicht mit beschrieben werden.


Muetze1 17. Jan 2008 09:18

Re: LowByte und Highbyte beschreiben
 
Zitat:

Zitat von DeddyH
Zitat:

Zitat von Alex_ITA01
Das geht auch soweit aber wie ist es möglich, dass wenn ich das Highbye beschreiben will auf mein SmallInt, dass ich das vorhandene LowByte nicht mit überschreibe (es soll also vorhanden bleiben). Andersrum genauso. Wenn ich das LowByte beschreibe, soll das Highbyte nicht mit beschrieben werden.


Ja und? Davon habe ich nicht geredet. Was ist wenn das LowByte schon einen Wert 3 hat und ich mit deiner Funktion das LowByte mit einer 4 belege. Dann habe ich nachher im LowByte eine 7 und nicht die angegebene 4. Nun klarer?

DeddyH 17. Jan 2008 09:19

Re: LowByte und Highbyte beschreiben
 
OK, da hast Du Recht.

Dax 17. Jan 2008 09:23

Re: LowByte und Highbyte beschreiben
 
Delphi-Quellcode:
procedure SetLowByte(var sInt: SmallInt; const b: Byte);
begin
  sInt := (sInt and $FF00) or b;
end;

procedure SetHighByte(var sInt: SmallInt; const b: Byte);
begin
  sInt := (sInt and $FF) or (b shl 8);
end;


Alle Zeitangaben in WEZ +1. Es ist jetzt 21:49 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