6

The field part of bit-fields seems to suggest that they can only be fields inside a structure or union.

Can a bit-field be a typical "stand-alone" variable, outside any aggregate data-type like union or structure as follows:

 int sum:6; //Can this work as a declaration statement? sum=4; 

If not, why so? If bit-fields are basically intended to use less memory, why can't we declare any variable as a bit-field if we know it won't exceed that size?

2
  • 1
    There are no "stand-lone" bitfields. It really makes no sense. You already have integral types of all conceivable sizes (e.g. uint8_t), and there's nothing gainable from "using fewer bits". Addressing is quite possibly slower when using anything that's not machine word sized anyway. Commented May 4, 2013 at 11:40
  • @KerrekSB LOL, I got my answer through a comment!! Commented May 4, 2013 at 11:44

3 Answers 3

5

Bit-fields are only part of structs or unions because that's what the C standard allows. It would have been possible to decide differently. Why the committee decided to write the standard the way it is, you would have to ask them.

To find the information in the C99 standard:

From the table of contents:

6.7 Declarations

6.7.2 Type specifiers (okay, this one is a little bit obscure)

6.7.2.1 Structure and union specifiers.

One bit-field is the declarator_opt : constant-expression part of the syntax. This syntax is only allowed here, and a consequence is that one cannot declare bit-fields elsewhere.

The syntax tells you that the name of a bit-field can optionally be omitted, if you were curious for this sort of information. Clause 6.7.2.1:3 goes further and prohibits names for 0-length bit-fields.

Sign up to request clarification or add additional context in comments.

1 Comment

Why the committee decided to write the standard the way it is, you would have to ask them. He was asking us!
0

Bit fields can only be defined in structs and unions where they can be referred to individually by name. You cannot address memory by bits, a minimum of a byte size is required (8 bits). In order to address a variable bit by bit, you may use a mask like so:

int num = 9; int fourthBit = (num >> 4) & 1; 

A struct can have a bigger size, for example an int (of 4 bytes) and then be divided, by bits, to different pieces. Of course, the assignment will be compiled using masks.

Refer to this for more info: http://msdn.microsoft.com/en-us/library/yszfawxh(v=vs.80).aspx

15 Comments

A multiple of a byte is used even when you use bitfields in a structure.It's never like "2 bytes and 5 bits".But bit fields enable us to use 1,2,3 byte for an integer.So why can't they be used to do the same as "stand-alone" variables?
Your link is no help.Just carries the known stuff about bitfields,not the answer to my question.
My answer refers to your first question: "Can this work as a declaration statement". As for your second question - why, I think its related to the the language syntax and syntax rules. In order for you to refer to a bit in a variable, there would have been some kind of syntax to support it, something like: num$bit4 or something like that. But there is already a syntax for doing something similar, using a dot like so: num.bit4 so I guess they used this and required that referring to bits in a variable be via a struct. I will try to find a reference for this.
I never said my statement int sum:6 expects bits!! It's obvious that I expect 1 byte as I would have had it been in a structure like struct test{int sum:6};.
And where in my question I said I intend to access a single individual bit?I am talking about the variable stored in the bitfield.
|
0

If not, why so? If bit-fields are basically intended to use less memory, why can't we >declare any variable as a bit-field if we know it won't exceed that size?

Because the memory is fixed at 16 or 32 bits wide. It would be too slow to pack a users 14 bit ints into 16 bits and use the other 2 bits . Its better to waste the 2 bits and don't let you define in efficient storage. Thats why if you really want to you can use a union. You wouldn't be gaining anything by making the compiler work harder for nothing.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.