Writing Data to Bit Field

Z

Zero

Hi everybody!

Is it possible to write a value into a bit field which initializes all
bits?

example:

struct Field
{
unsigned int Bit1 : 1;
unsigned int Bit2 : 2;
} MyFied;

Now, I would like to write the value 3 to the variable MyField,
so that Bit1 and Bit2 become TRUE ( == 1).

Any suggestion?

Thanks!
 
V

Vladimir S. Oka

Zero said:
Hi everybody!

Is it possible to write a value into a bit field which initializes all
bits?

example:

struct Field
{
unsigned int Bit1 : 1;
unsigned int Bit2 : 2;

I guess you mean `: 1` here as well (judging by the rest of the post).
As it is, Bit2 has width 2, not 1.
} MyFied;

Now, I would like to write the value 3 to the variable MyField,
so that Bit1 and Bit2 become TRUE ( == 1).

Any suggestion?

AFAIK, not in a portable way.

The closest you can portably get is to have a constant struct with Bit1
and Bit2 set to 1, and then assign that to variables of the same type.
E.g.:

struct Field all_bits_one = { 1, 1};

MyFied = all_bits_one;

If you don't want to be portable, you can find out how your
implementation represents integers (or similar handy objects) and try
constructing them appropriatelly and casting to your struct.
 
V

Vladimir S. Oka

Vladimir said:
I guess you mean `: 1` here as well (judging by the rest of the post).
As it is, Bit2 has width 2, not 1.


AFAIK, not in a portable way.

The closest you can portably get is to have a constant struct with Bit1
and Bit2 set to 1, and then assign that to variables of the same type.
E.g.:

struct Field all_bits_one = { 1, 1};

Obviously:

const struct Field all_bits_one = { 1, 1};

if you wanted it constant as I suggested.
 
V

Vladimir S. Oka

Zero said:
Can you give me an example for the cast-operation?

Please quote what, and who you're replying to. If you're using Google,
you need to click on Show Options, and then Reply that appears beneath
the header (and while you're at it, don't top-post either in case you
feel tempted).

I's also move to retract my suggestion that you can cast to a struct.
Casts only work on scalar types, and structs are not scalar types. I
blame insuficient coffee intake. Sorry.

Why would you want to do such a thing anyway?
 
Z

Zero

Vladimir said:
Please quote what, and who you're replying to. If you're using Google,
you need to click on Show Options, and then Reply that appears beneath
the header (and while you're at it, don't top-post either in case you
feel tempted).

Ok, I never have quoted before...
I's also move to retract my suggestion that you can cast to a struct.
Casts only work on scalar types, and structs are not scalar types. I
blame insuficient coffee intake. Sorry.

Yes, that is also what my compiler says...
Why would you want to do such a thing anyway?

It would be the fastest way to initialize the bits, wouldn't it?

Zero
 
V

Vladimir S. Oka

Zero said:
Ok, I never have quoted before...


Yes, that is also what my compiler says...


It would be the fastest way to initialize the bits, wouldn't it?

It shouldn't be less efficient than what I suggested earlier (a good
compiler will probably behind the scenes effectively do what you were
trying to). It's also much more readable.

You should also know the first rule of optimisation:

1. Don't do it.

Compilers are generally better at it. Second rule is useful as well:

2. Don't do it yet.
 
R

Richard G. Riley

Doesn't worry me too much since if I'm interested in a thread then it
can easily be tagged or reconstructed at will despite being an online
newsreader. Personally I hate people constantly including the entire
previous post : the title and thread ID is usually enough for me to
know where I am. Still, horses for courses : and *some* context is
often nice especially in a "to and fro" type thread.

Yes, that is also what my compiler says...


It would be the fastest way to initialize the bits, wouldn't it?

Zero

Maybe. What About using a Union? BTW its horrible : address the fields
seperately and let the compiler take care of optimizations unless
there really is a need to write data to a word at exactly the same time.
 
P

pemo

Zero said:
Hi everybody!

Is it possible to write a value into a bit field which initializes all
bits?

example:

struct Field
{
unsigned int Bit1 : 1;
unsigned int Bit2 : 2;
} MyFied;

Now, I would like to write the value 3 to the variable MyField,
so that Bit1 and Bit2 become TRUE ( == 1).

Any suggestion?

Thanks!


What do you mean by 'write a value', e.g., would this do, or are you looking
to use '='?

memset(&MyFied, ~(unsigned int)0, sizeof(MyFied));
 
V

Vladimir S. Oka

Zero said:
What do you mean with your hint?

Create a `union` with one member of the type that you deem convenient
for quick initialisation, and the other being your actual bit-field
structure. Unions are constructs that store all their members in the
same memory block which you can then access in different ways (look it
up in a C book). So if you had an `int` in your union, you could force
a certain, implementation defined, bit-pattern into your bit-field as
well.

As I said earlier, this wouldn't be portable, and it's also horribly
difficult to figure out what's happening when you review the code
later. Just don't do it.
 
V

Vladimir S. Oka

pemo said:
What do you mean by 'write a value', e.g., would this do, or are you looking
to use '='?

memset(&MyFied, ~(unsigned int)0, sizeof(MyFied));

Ah, a beautiful example of how it can be done in yet another, this time
even more incomprehensible way. ;-)

Zero, please don't do any of these things. Just spell out exactly what
you're doing.
 
V

Vladimir S. Oka

Zero said:
Yeah, I want to write MyField = 3;

This will not write 3 into MyField. It will just guarantee that you'll
find 1s in your bit-fields.
but it seems as the function memset seems to be ideal!

You cannot be serious!
 
?

=?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=

Zero said:
Ok, I never have quoted before...


Yes, that is also what my compiler says...


It would be the fastest way to initialize the bits, wouldn't it?

Hopefully your compiler is smart enough to assemble
an initialization such as struct Foo bar = { 0x1,0x4,0x7}; in the
nest/fastest way.
 
R

Richard G. Riley

What do you mean by 'write a value', e.g., would this do, or are you looking
to use '='?

memset(&MyFied, ~(unsigned int)0, sizeof(MyFied));

That is hardly optimal for setting two bit fields with 3 bits :)
 
R

Richard G. Riley

What do you mean with your hint?

Use google. Google up "C union". I have no idea if it will work with a
bitfield. Find out.

The bottom line is that it might legally allow you to write 0xFFFF to
the memory address of the bitfield. And a big might but nice
experiment to find out.
 
T

tmp123

Zero said:
Yeah, I want to write MyField = 3;



but it seems as the function memset seems to be ideal!

Thank a lot!

memset will be not inlined. That means, by example, that a simple
becames a call to procedure + stack allocation + loop + stack
deallocation + return.
 
R

Robin Haigh

Zero said:
Yeah, I want to write MyField = 3;



but it seems as the function memset seems to be ideal!


Do you need bitfields? The struct is padded to a 4-byte object on my
platform. There must be any number of ways to do the job more efficiently
(if that matters) in no more space using a char or 2 chars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,196
Latest member
ScottChare

Latest Threads

Top