Trying to use a Delphi for .NET generated enum, which is actually a
Pascal set (a bitset where each bit corresponds to an item in an enum,
with special operators for union, section and difference), I noticed that
there is not really good way to iterate over such an enum consisting of
bits. Or is there?
Part of my test code:
namespace BitSetsTest
{
class Test
{
public enum Bits
{
a = 1,
b = 2,
c = 4,
d = 8,
e = 16,
f = 32,
g = 64,
h = 128
}
public static void WriteBits(Bits b)
{
for (int i = (int)Bits.a; i <= (int)Bits.h; i <<= 1)
if ((i & (int)b) != 0)
Console.Write("{0} ", (Bits)i);
else
Console.Write(". ");
Console.WriteLine();
}
// snip
My question is: is there a better way to iterate over the values of such
a bitset -- like I'm doing in WriteBits() -- than casting back and forth
between enum and int?
In Delphi I would do:
type
Bit = (a, b, c, d, e, f, g, h); // base enum
Bits = set of Bit; // set
procedure WriteBits(b: Bits);
var
i: Bit;
begin
for i := Low(Bit) to High(Bit) do
if i in b then
Write(i.ToString, ' ')
else
Write('. ');
In C# such a type turns out to be coded in IL as the enum described in
the C# code above. Is there a way to iterate over the individual bits?
--
Rudy Velthuis
"The full use of your powers along lines of excellence."
-- definition of "happiness" by John F. Kennedy (1917-1963) 2 3413
"Rudy Velthuis" <rv*******@gmx.de> wrote in
news:xn***************@news.t-online.de: Trying to use a Delphi for .NET generated enum, which is actually a Pascal set (a bitset where each bit corresponds to an item in an enum, with special operators for union, section and difference), I noticed that there is not really good way to iterate over such an enum consisting of bits. Or is there?
Part of my test code:
namespace BitSetsTest { class Test { public enum Bits { a = 1, b = 2, c = 4, d = 8, e = 16, f = 32, g = 64, h = 128 }
public static void WriteBits(Bits b) { for (int i = (int)Bits.a; i <= (int)Bits.h; i <<= 1) if ((i & (int)b) != 0) Console.Write("{0} ", (Bits)i); else Console.Write(". ");
Console.WriteLine(); }
// snip
My question is: is there a better way to iterate over the values of such a bitset -- like I'm doing in WriteBits() -- than casting back and forth between enum and int?
In Delphi I would do:
type Bit = (a, b, c, d, e, f, g, h); // base enum
Bits = set of Bit; // set
procedure WriteBits(b: Bits); var i: Bit; begin for i := Low(Bit) to High(Bit) do if i in b then Write(i.ToString, ' ') else Write('. ');
In C# such a type turns out to be coded in IL as the enum described in the C# code above. Is there a way to iterate over the individual bits?
Rudy,
Here's another way to iterate over a bit enumeration:
public static void WriteBits2(Bits b)
{
foreach (int i in System.Enum.GetValues(typeof(Bits)))
if ((i & (int) b) != 0)
Console.Write("{0} ", System.Enum.GetName(typeof(Bits), i));
else
Console.Write(". ");
}
Here's some good code that adds "set" operations to C#: http://www.codeproject.com/csharp/Sets.asp
Hope this helps.
Chris.
-------------
C.R. Timmons Consulting, Inc. http://www.crtimmonsinc.com/
At 09:03:42, 19.04.2004, Chris R. Timmons wrote: Rudy,
Here's another way to iterate over a bit enumeration:
public static void WriteBits2(Bits b) { foreach (int i in System.Enum.GetValues(typeof(Bits))) if ((i & (int) b) != 0) Console.Write("{0} ", System.Enum.GetName(typeof(Bits), i)); else Console.Write(". "); }
Thanks, that was exactly the kind of code I was looking for. Someone had
already pointed me to the set emulation code on codeproject, but my
question was for existing Pascal sets, which are emulated in Delphi via
bit enums.
--
Rudy Velthuis
"Give me a museum and I'll fill it."
-- Pablo Picasso (1881-1973) This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Niall Smart |
last post by:
Hi
I'm trying to emulate a "following-sibling-or-self" XPath axis without
using the union operator. The XML looks like this:
<deeply-nested>
<select>
<option value="1">One</option>
<option...
|
by: Bart |
last post by:
Hi there,
Since you've all told me that frames ar evil, I'm planning to disguard
frames in favour of CSS "pseudo-frames" for my personal website.
While trying to "emulate frames" (that is I...
|
by: Julian Maisano |
last post by:
Can anybody help me to translate the following lines to pascal(delphi)?
Let me explain what I'm looking for:
int i = 1; {In this line, an integer variable which is called "i" is
declared and...
|
by: Chris Gordon-Smith |
last post by:
I am tying to call a Pascal function from C++, and vice versa.
Does anyone know how to do this, or where detailed information on this topic
can be found?
For the C++ to Pascal call I have...
|
by: Rich Herrick |
last post by:
The latest version of my "Pascal-like" set class is available here:
http://www.richherrick.com/software/herrick-1.0.zip
Those from the old YAHOO BOOST forum might remember it from several...
| |
by: Stephan Schaem |
last post by:
How does one write an unmanaged function that perform this functionality?
In short I want to turn off/on visual style in my app...
Thanks,
Stephan
PS: two people have been looking for...
|
by: dhruba.bandopadhyay |
last post by:
Am using Borland C++ 4.5 for the old dos.h APIs. It appears that newer
versions of compilers stop support for the oldskool DOS routines. Am
trying to convert/port an oldskool Pascal program that...
|
by: SMALLp |
last post by:
Hy! I desperately need help!
I need to make application that would accept Pascal code and check if it
returns good results.
My idea is (from a beginner point of view) to make application in...
|
by: Ruud |
last post by:
Hallo allemaal,
During the conversion of my program from Pascal to C, I was more or
less able to find the C equivalent of most Pascal functions so far.
Only four gave me some real trouble. I...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |