Autor Beitrag
Delphililein
Hält's aus hier
Beiträge: 3



BeitragVerfasst: Sa 29.03.03 10:36 
anfänger sucht hilfe für folgendes Problem.

Ich möchte die Bits in einem Byte einzelnen Variablen zuordnen. Ich muss die Bits einzeln abfragen können und auch einzeln ändern und dann wieder in das Byte zurückgeben. Ich möchte über eine Hardwarekarte die einzelnen Bits = die 8 Ports ansteuern. Vielen dank für eure Hilfe.

Gruss

Moderiert von user profile iconTino: Moderiert.
tommie-lie
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 4373

Ubuntu 7.10 "Gutsy Gibbon"

BeitragVerfasst: Sa 29.03.03 11:26 
Hallo und willkommen im Delphi-Forum

Wenn du die Datenleitungen der Hardware bereits als Integerzahl hast, kannst du mit den logischen Operanden (or, and, shl usw - weiteres siehe Delphi-Onlinehilfe) und Zahlen (so steht zum Beispiel 1 für das erste Bit, 2 für das zweite, 4 für das dritte usw) rausfinden, welche Datenleitung benutzt wird.
Alternativ kannst du einzelne Boolean-Variablen für jede Datenleitung erstellen und die dann auf True bzw False setzen, aber erstere Methode (and dürfte der richtige Kandidat sein) dürfte wohl ein paar Umwege weglassen und schneller gehen.

_________________
Your computer is designed to become slower and more unreliable over time, so you have to upgrade. But if you'd like some false hope, I can tell you how to defragment your disk. - Dilbert
AndyB
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 1173
Erhaltene Danke: 14


RAD Studio XE2
BeitragVerfasst: Sa 29.03.03 11:30 
Unter Delphi geht das nicht ganz so einfach wie unter C/C++. Man muss sich eben etwas einfallen lassen.

Hier mal etwas, das ein Array of Boolean einsetzt:
ausblenden Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
type
  TBitArray = array[0..7] of Boolean;

var
  b: Byte;
  Bits: TBitArray;
begin
  GetBits(Bits, b);
  Bits[0] := False;
  Bits[1] := True;
  b := SetBits(Bits);
end;

function SetBits(const Bits: TBitArray): Byte;
var i: Integer;
begin
  Result := 0;
  for i := 0 to High(Bits) do
    if Bits[i] then
      Result := Result or 1 shl i;
end;

procedure GetBits(var Bits: TBitArray; Value: Byte);
var i: Integer;
begin
  for i := 0 to High(Bits) do
    Bits[i] := Value and (1 shl i) <> 0;
end;

_________________
Ist Zeit wirklich Geld?
Delphililein Threadstarter
Hält's aus hier
Beiträge: 3



BeitragVerfasst: Sa 29.03.03 22:25 
Titel: Nochmals eine Frage
Vielen Dank für eure Antworten,

aber als neuling raff ichs noch nicht ganz. Ich möchte am schluß 8 Variablen haben z.b. x0 bis x7 die je nach Zustand der Bits des Bytes das ich als Hex Zahl habe den zustan True oder False hat.

Gruß Delphililein

Moderiert von user profile iconTino: Absätze entfernt.
MSCH
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 1448
Erhaltene Danke: 3

W7 64
XE2, SQL, DevExpress, DevArt, Oracle, SQLServer
BeitragVerfasst: Sa 29.03.03 22:55 
Ist doch alles schon erklärt. Es gibt in Delphi keine Variable X des Typs bit.
Nimm ein Byte (sind ja 8 Bits) und werte diese einzeln aus.
grez.
msch
maximus
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 896

Win XP, Suse 8.1
Delphi 4/7/8 alles prof
BeitragVerfasst: Sa 29.03.03 23:30 
Hallo.

Es gibt da einer simplen, aber sehr effektiven trick:

ausblenden Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
// einen set-typ erstllen, da sets nicht weiter als'n paar bits sind!
type TBitSet = set of (bit0, bit1, bit2, bit3, bit4, bit5, bit6, bit7);

var b:byte;
...

  TBitSet(b) := [bit1,bit4,bit5]; // ein byte aus bits 'formen'
  //...
  ShowMessage('byte: '+IntToStr(b));
  //...
  if bit4 in TBitSet(b) then ShowMessage('Juhuu! bit4'); // auf bits testen!

  if TBitSet(b) = [bit0,bit7] then ShowMessage('lol'); // exakt prüfen
  // mehrere bits hinzufügen -> es gelten alle set operatoren(+-*<=>= etc.)
  TBitSet(b) := TBitSet(b) + [bit0, bit3];
  //..
  ShowMessage('byte jetzt: '+IntToStr(b));

Dies ermöglicht einen sehr komfortablen umgang mit bits :)

mfg maximus.
maximus
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 896

Win XP, Suse 8.1
Delphi 4/7/8 alles prof
BeitragVerfasst: So 30.03.03 15:23 
Nachtrag: Um ein set als string anzuzeigen kann man folgende func. benutzen (D6,D7):
ausblenden Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
uses typInfo;
...
TIntSet = set of 0..SizeOf(Integer) * 8 - 1;
...
function SetToStr(SetTypeInfo:PTypeInfo;value:integer;Brackets:boolean=true):string;
var
  S: TIntSet;
  TypeInfo: PTypeInfo;
  compTypeDate: PTypeData;
  I: Integer;
begin
  Result := '';
  Integer(S) := Value;
  TypeInfo := GetTypeData(SetTypeInfo)^.CompType^;
  compTypeDate := GetTypeData(TypeInfo);
  for I := compTypeDate.MinValue to compTypeDate.MaxValue do
    if I in s then
    begin
      if Result <> '' then
        Result := Result + ',';
      Result := Result + GetEnumName(TypeInfo, I);
    end;
  if Brackets then Result := '[' + Result + ']';
end;


Modifizierte function aus der unit typInfo, um unabhängig von properties zusein!

Aufruf:ShowMessage(SetToStr(typInfo(TBitSet), b));

_________________
mfg.
mâximôv
TheD
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 27



BeitragVerfasst: So 30.03.03 19:37 
ausblenden Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
var
  Bit0, Bit1, Bit2, Bit3, Bit4, Bit5, Bit6, Bit7: Boolean;
  bByte: Byte;
begin
  bByte := //Hier ist also Dein Byte...
  if (bByte and 1) <> 0 then Bit0 := True;
  if (bByte and 2) <> 0 then Bit1 := True;
  if (bByte and 4) <> 0 then Bit2 := True;
  if (bByte and 8) <> 0 then Bit3 := True;
  if (bByte and 16) <> 0 then Bit4 := True;
  if (bByte and 32) <> 0 then Bit5 := True;
  if (bByte and 64) <> 0 then Bit6 := True;
  if (bByte and 128) <> 0 then Bit7 := True;
end;


So, jetzt hast Du 8 Variablen vom Typ Boolean.

Moderiert von user profile iconTino: Moderiert.

_________________
Bis dannn... TheD!
Wer im Schlachthaus sitzt, sollte nicht mit Schweinen werfen! ;)
tommie-lie
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 4373

Ubuntu 7.10 "Gutsy Gibbon"

BeitragVerfasst: So 30.03.03 19:45 
Das ist exakt das, was ich oben vorgeschlagen hab', nur in Codeform und somit lernt man nix :roll:

_________________
Your computer is designed to become slower and more unreliable over time, so you have to upgrade. But if you'd like some false hope, I can tell you how to defragment your disk. - Dilbert
TheD
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 27



BeitragVerfasst: So 30.03.03 20:08 
tommie-lie hat folgendes geschrieben:
Das ist exakt das, was ich oben vorgeschlagen hab', nur in Codeform und somit lernt man nix :roll:

Du siehst ja was alle anderen für sachen schreiben und das der UrPoster überhaupt nicht damit klar kommt. Und mit der Hilfe scheint der auch nciht vertraut zu sein.

_________________
Bis dannn... TheD!
Wer im Schlachthaus sitzt, sollte nicht mit Schweinen werfen! ;)
tommie-lie
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 4373

Ubuntu 7.10 "Gutsy Gibbon"

BeitragVerfasst: So 30.03.03 20:13 
naja, wenn jemand eine Hardwarekarte programmieren will, dann denke ich mal schon, daß er weiß, wie man in der Hilfe sucht.

_________________
Your computer is designed to become slower and more unreliable over time, so you have to upgrade. But if you'd like some false hope, I can tell you how to defragment your disk. - Dilbert