Autor Beitrag
yndaso
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 108



BeitragVerfasst: Di 27.01.09 16:09 
Hallo zusammen!

Ich habe in meiner Anwendung 8 Kästchen (1Kasten entspricht ein Bit).
Die Kästchen sollen je nach dem Status des Bits die Farbe verändern. Als
"Eingangswert" habe ich eine Variable vom Typ Byte.
Ich will also schauen ob in dem Byte, z.b. das Bit0 gesetzt ist oder nicht!
Wie macht man das? Also in VB6 ging das immer so:
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
if ((EKS_SafetyOperation && 2 ^ 0) == 2 ^ 0)
{
   Panel1_Bit0.BackColor = Color.Green;
}

...usw


Der Compiler hier sagt aber das der Operator ^ nicht auf bool und int angewendet werden kann UND
das der operator && nicht auf byte und int angewendet werden kann...

Kurz gesagt, ALLES IST FALSCH

Kann mir jemand sagen wie die if Anweisung in C# aussehen muss?


Vielen Dank
Th69
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Moderator
Beiträge: 4808
Erhaltene Danke: 1061

Win10
C#, C++ (VS 2017/19/22)
BeitragVerfasst: Di 27.01.09 16:26 
s. z.B. www.mycsharp.de/wbb2...d.php?threadid=48671

Also nicht && sondern & verwenden und ^ stellt in C# den XOR-Operator dar...
Also einfach:
ausblenden C#-Quelltext
1:
2:
3:
if ((EKS_SafetyOperation & 1) == 1)
{
}
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19346
Erhaltene Danke: 1754

W11 x64 (Chrome, Edge)
Delphi 12 Pro, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Di 27.01.09 18:26 
Wobei eine hexadezimale Schreibweise sinnvoller wäre. Bei einem Byte ist das noch nicht so wichtig, aber bei mehreren Bytes (z.B. einem Integerwert mit 4 Byte) ist das Umrechnen in dezimale Schreibweise nicht mehr so schnell im Kopf:
ausblenden C#-Quelltext
1:
2:
3:
if ((EKS_SafetyOperation & 0x01) == 1)
{
}
So hast du nämlich nur immer 4 Bit, die du in eine Hexadezimalziffer umrechnen musst. ;-)