Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Sonstige Fragen zu Delphi (https://www.delphipraxis.net/19-sonstige-fragen-zu-delphi/)
-   -   Delphi [C->Delphi] struct { ULONG a:2; } (https://www.delphipraxis.net/122231-%5Bc-delphi%5D-struct-%7B-ulong-2%3B-%7D.html)

Win32.API 11. Okt 2008 22:09


[C->Delphi] struct { ULONG a:2; }
 
Hallo,

ist es möglich, soetwas

Code:
struct {
ULONG a:2;
ULONG b:1;
ULONG c:6;
ULONG d:2;
}test;
In Delphi zu deklarieren?

Gruß,
Win32.API

SirThornberry 11. Okt 2008 22:21

Re: [C->Delphi] struct { ULONG a:2; }
 
ja. eine Struct ist ein record und ein ULONG wird ein unsigned Long sein also ein Cardinal
Edit: ups, ich seh grad das es sich um ein Bitfeld handelt - ich hatte die Doppelpunkte übersehen. Soweit mir bekannt ist das in Delphi nicht möglich. Das bleibt den Systemnahen Programmiersprachen zumeist vorbehalten.

SirTwist 11. Okt 2008 23:20

Re: [C->Delphi] struct { ULONG a:2; }
 
Ich weiß nicht, was Delphi daraus machen würde, abe ich meine mich an meinen lang zurückliegenden Info-LK erinnern zu können, da gab es sowas wie "packed array of Boolean". Gibts das noch? Wenn ja, könnte das das gewüsnchte sein.

Gruß,
SirTwist

himitsu 11. Okt 2008 23:43

Re: [C->Delphi] struct { ULONG a:2; }
 
Zitat:

Zitat von SirTwist
da gab es sowas wie "packed array of Boolean". Gibts das noch?

Boolean ist ein Byte (auch wenn es einen Bitwert darstellen soll)

Am Nähesten kommt dann wohl (da hier mehrere Bist in einem Feld vorkommen) eine Variante mit Masken (den Konstanten im Beispiel).
Delphi-Quellcode:
type TTest = Cardinal;

const aMask = $0003; // b0000000000000011
      bMask = $0004; // b0000000000000100
      cMask = $01F8; // b0000000111111000
      dMask = $0600; // b0000011000000000

var  x: TTest;
      a, b, c, d: Cardinal;

a := x and aMask;
b := (x and bMask) shr 2;
c := (x and cMask) shr 3;
d := (x and dMask) shr 9;
Wenn alle Felder je nur ein Bit groß gewesen wären, dann hätte man auch ein SET (Bitfeld) nehmen können :angel:

alzaimar 12. Okt 2008 08:43

Re: [C->Delphi] struct { ULONG a:2; }
 
NB:
Mit einem "Packed Record" ging das in Pascal früher:
Delphi-Quellcode:
test = packed Record
  a : 0..3;
  b : 0..1;
  c : 0..64;
  d : 0..3;
  end;
Wie gesagt: Früher.


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