Hi,
[edit] Ich formuliere mal um. Ich schreibe einen Drucker an mit dem Hexdezimal Wert 0x1D (Dez. 29). Dabei gibt er mir eine Statusmeldung mit 2 bytes (dezimal) heraus.
z.b. 29 67
Das erste byte steht für die Abfrage.
Das zweite gibt mir Aufschluss darüber welchen Status er hat.
67 = 0100 0011. das 4. Bit von rechts zeigt mir das Papierende an. 1 = true, 0 = false.
Nun will ich wissen wenn mit der Drucker eine Dezimal Zahl wiedergibt welche Bits 1 und welche 0 sind. So dass ich auf jeden Status reagieren kann.
Bei deiner Quelle müsste er mir ja bei dem Quelltext:
class Program
{
private static byte BIT1 = 0x01;
private static byte BIT2 = 0x02;
private static byte BIT3 = 0x03;
private static byte BIT4 = 0x04;
private static byte BIT5 = 0x05;
private static byte BIT6 = 0x06;
private static byte BIT7 = 0x07;
private static byte BIT8 = 0x08;
static void Main(string[] args)
{
//ReadOptions bitfield = ReadOptions.eins | ReadOptions.zwei | ReadOptions.vier;
byte bitfield = 0x43; // 0x05 = 0101, 0x43 = 01000011
if ((bitfield & BIT1) == BIT1)
Console.WriteLine(" Bit 1 ist gesetzt ");
if ((bitfield & BIT2) == BIT2)
Console.WriteLine(" Bit 2 ist gesetzt ");
if ((bitfield & BIT3) == BIT3)
Console.WriteLine(" Bit 3 ist gesetzt ");
if ((bitfield & BIT4) == BIT4)
Console.WriteLine(" Bit 4 ist gesetzt ");
if ((bitfield & BIT5) == BIT5)
Console.WriteLine(" Bit 5 ist gesetzt ");
if ((bitfield & BIT6) == BIT6)
Console.WriteLine(" Bit 6 ist gesetzt ");
if ((bitfield & BIT7) == BIT7)
Console.WriteLine(" Bit 7 ist gesetzt ");
if ((bitfield & BIT8) == BIT8)
Console.WriteLine(" Bit 8 ist gesetzt ");
// Key
Console.ReadKey();
}
}
folgendes herausgeben:
Bit 1 ist gesetzt
Bit 2 ist gesetzt
Bit 7 ist gesetzt
Jedoch gibt er mir:
Bit 1 ist gesetzt
Bit 2 ist gesetzt
Bit 3 ist gesetzt
gruß,
saka
Hallo sakanoue,
es müßte heißen :
private static byte BIT1 = 0x01;
private static byte BIT2 = 0x02;
private static byte BIT3 = 0x04;
private static byte BIT4 = 0x08;
private static byte BIT5 = 0x10;
private static byte BIT6 = 0x20;
private static byte BIT7 = 0x40;
private static byte BIT8 = 0x80;
dann kommts richtig raus.
Bei deinem ursprünglich geposteten Code würde es so gehen :
using System;
namespace ConsoleApplication1
{
[Flags]
public enum ReadOptions : uint
{
eins = 0x01,
zwei = 0x02,
vier = 0x04
}
class Program
{
static void Main(string[] args)
{
for (int bit = 0; bit <= 7; bit++)
{
int value = (int)Math.Pow(2, bit);
if (Enum.IsDefined(typeof(ReadOptions), (ReadOptions)value))
{
Console.WriteLine("{0} - {1}", bit, ((ReadOptions)value).ToString());
}
else
{
Console.WriteLine("{0} - not defined", bit);
}
}
Console.ReadKey();
}
}
}
Gruß
Michael Schmitz
Hallo,
schau dir doch auch mal die BitVector32-Struktur an: http://msdn.microsoft.com/de-de/library/system.collections.specialized.bitvector32%28VS.80%29.aspx
Damit kann man meiner Meinung nach wesentlich einfacher auf solche Strukturen zugreifen.
Ich hab mir die für ein anderes Projekt auch schon mal auf 64-Bit erweitert. Bei Bedarf kann ich das ganze hier auch mal posten.
Grüße
Falls es darum geht, die Bits in Symbole (Wörter) zu übersetzen, dann bieten sich Enums statt Konstanten an. Dank Enum.GetValues() kann man dann auch schöne Bit-Schleifen produzieren, die ein wenig "sprechender" sind als Schleifen mit Shifts.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace EnumTest
{
class Program
{
[Flags]
public enum Bit : byte
{
Eins = 0x1,
Zwei = 0x2,
Vier = 0x4,
Acht = 0x8
// usw.
}
static void Main(string[] args)
{
Output1();
Output2();
Console.ReadLine();
}
private static void Output1()
{
Console.WriteLine("Folgende Bits sind gesetzt: " + ((Bit)5).ToString());
}
private static void Output2()
{
CheckAllBits(5);
}
public static bool CheckBit(byte val, Bit b)
{
return ((Bit)val & b) != 0;
}
public static void CheckAllBits(byte val)
{
foreach (Bit b in Enum.GetValues(typeof(Bit)))
{
if (CheckBit(val, b))
{
Console.WriteLine(string.Format("Bit {0} gesetzt", b));
}
}
}
}
}