473,396 Members | 1,683 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

bit Fields in c# structs ??

does c# provide bitfields in structs ??

can't find any hint in msdn

Johan
Nov 15 '05 #1
5 20737
Seems i will have to fall back to cpp for that

"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:uI**************@TK2MSFTNGP12.phx.gbl...
Johan,
does c# provide bitfields in structs ??


No, C# doesn't support bit fields.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.

Nov 15 '05 #2
No, and i'd advise against using them even if they were there, but you can
create a structure\class that works like one internally with a little work.
This would be of use only if you need to produce in memory values perhaps to
send to legacy platform code or writing to files that expect bit fields. In
any other condition, just use a normal class and don't worry about space.

Basically, you would encapsulate a BitVector32 or BitArray and provide
boolean & int properties that set bits. You will have to worry about any
possible endian issues, etc, but that shouldn't be much of a problem. This
should allow you to create structures that have the memory equivilent of a
bit vector (marshalling should deal with just the internal BitVector & not
the various properties), or classes that expose a BitVector32 that can be
written to a file containing the proper data format.

Some caevets however, are that bitvector based structures that require ints
will either require you to figure out what bits need be set yourself, or
possibly a custom marshaller that will ignore the Section objects and any
other data you might need to keep track of location.
You will also have to explicitly lay out the BitVector32 fields location if
more than one exists.
I am assuming BitVector32 marshals properly however, it might not, and i
don't really have anything to test with, ;).
"Sagaert Johan" <RE*******************@hotmail.com> wrote in message
news:ec**************@TK2MSFTNGP09.phx.gbl...
does c# provide bitfields in structs ??

can't find any hint in msdn

Johan

Nov 15 '05 #3
Sagaert,
You can define a new Enum with the FlagsAttribute to come close to
'bitfields'.

Or as Daniel stated, a BitArray or BitVector32 might do it.

Otherwise as Mattias stated: bitfields in the C++ sense of the word are not
supported.

Hope this helps
Jay

"Sagaert Johan" <RE*******************@hotmail.com> wrote in message
news:ec**************@TK2MSFTNGP09.phx.gbl...
does c# provide bitfields in structs ??

can't find any hint in msdn

Johan

Nov 15 '05 #4
I came to the conclusion to use a struct in my C# and then convert it to
what my embedded system would expect in memory.

"Chad Myers" <cm****@N0.SP.AM.austin.rr.com> wrote in message
news:es**************@TK2MSFTNGP09.phx.gbl...

"Sagaert Johan" <RE*******************@hotmail.com> wrote in message
news:ec**************@TK2MSFTNGP09.phx.gbl...
does c# provide bitfields in structs ??

can't find any hint in msdn


Use the [Flags] attribute on an an enumerator.

[Flags]
public enum PizzaToppings
{
ExtraCheese = 1,
Pepperoni = 2,
ItalianSausage = 4,
AmericanSausage = 8,
Peppers = 16,
Mushrooms = 32,
Anchovis = 64,
Pineapples = 128
}

...

public class Pizza
{
private PizzaToppings toppings;

public PizzaToppings Toppings
{
get{ return toppings; }
set{ toppings = value; }
}

... blah blah
}

...

Pizza p = new Pizza();

p.Toppings =
PizzaToppings.ExtraCheese |
PizzaToppings.ItalianSausage |
PizzaToppings.Pepperoni;
-c

Nov 15 '05 #5
I came to the conclusion to use a struct in my C# and then convert it to
what my embedded system would expect in memory.

"Chad Myers" <cm****@N0.SP.AM.austin.rr.com> wrote in message
news:es**************@TK2MSFTNGP09.phx.gbl...

"Sagaert Johan" <RE*******************@hotmail.com> wrote in message
news:ec**************@TK2MSFTNGP09.phx.gbl...
does c# provide bitfields in structs ??

can't find any hint in msdn


Use the [Flags] attribute on an an enumerator.

[Flags]
public enum PizzaToppings
{
ExtraCheese = 1,
Pepperoni = 2,
ItalianSausage = 4,
AmericanSausage = 8,
Peppers = 16,
Mushrooms = 32,
Anchovis = 64,
Pineapples = 128
}

...

public class Pizza
{
private PizzaToppings toppings;

public PizzaToppings Toppings
{
get{ return toppings; }
set{ toppings = value; }
}

... blah blah
}

...

Pizza p = new Pizza();

p.Toppings =
PizzaToppings.ExtraCheese |
PizzaToppings.ItalianSausage |
PizzaToppings.Pepperoni;
-c

Nov 15 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Davide Bruzzone | last post by:
Greetings all... I need to create a number of bitfield structs whose contents are smaller than the size of an int. For example: typedef struct foo FOO; struct foo { unsigned char fieldOne:...
19
by: Jasper Kent | last post by:
Can anyone explain the logic behind structs being allowed neither memeber initialisers or default constructors. Doesn't this just encourage developers to create constructors with dummy...
5
by: Bilgehan.Balban | last post by:
Hi, I am currently brushing up my c++ knowledge and I would like to ask you about the differences between classes and C structs, in the function/method perspective. 1) Is it correct to say...
20
by: pinkfloydhomer | last post by:
Is it well-defined and portable to do something like: typedef struct { int type; char c; } S1; typedef struct {
61
by: Marty | last post by:
I am new to C# and to structs so this could be easy or just not possible. I have a struct defined called Branch If I use Branch myBranch = new Branch(i); // everything works If I use Branch...
2
by: S. Lorétan | last post by:
Hello. I have some structs in different namespaces/classes/other structs and I sometime have to check if it contains something or not. myStruct == null doesn't work. I've currently done it...
11
by: Cliff Martin | last post by:
Hi, I am reading a fairly large file a line at a time, doing some processing, and filtering out bits of the line. I am storing the interesting information in a struct and then printing it out....
29
by: Dom | last post by:
I'm really confused by the difference between a Struct and a Class? Sometimes, I want just a group of fields to go together. A Class without methods seems wrong, in that it carries too much...
50
by: titan nyquist | last post by:
I wish to compare two structs via == but it does not compile. I can overload and create my own == but am I missing something that c# already has implemented? ~titan
6
by: =?Utf-8?B?QWxleGFuZGVyZmU=?= | last post by:
Hi, I have a C# program that uses an unmanaged dll that has a function similar to the signature below : void f(out MyStruct arr, out int num); // num = actual array length returned The array...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
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,...
0
jinu1996
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...
0
agi2029
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 using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.