473,796 Members | 2,426 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

regarding bit fields

Hi Everyone,

We all know that bit fields are to be used within a structure, like

struct sample
{
int i:4;
}

However, is there any reason as to why it can only be specified only
within a structure or a union and why not standalone as,

int i:4;

Dec 13 '06 #1
3 1449
In article <11************ **********@16g2 000cwy.googlegr oups.com>
<sa*****@yahoo. co.inwrote:
We all know that bit fields are to be used within a structure, like

struct sample
{
int i:4;
}

However, is there any reason as to why it can only be specified only
within a structure or a union and why not standalone as,

int i:4;
"Because Dennis said so", more or less.

(In other words, there is no fundamental reason bitfields could not
be applied to ordinary variables. Note, however, that there is no
"pointer to bitfield" type in C, so there *is* a fundamental reason
they could not be applied to arrays, at least not without first
adding "pointer to bitfield" types.)

Dennis Ritchie also once said that C's bitfields are "a botch and
a blemish". I have to agree: they are not supported "correctly"
at a high level (no arrays of bitfields, no pointers to bitfields,
and so on), and at the same time, they do not map usefully to
hardware or protocol needs (no control over underlying machine
representations , cannot span machine words, and so on). In other
words, their sole function is to specify representation -- in
this case, "number of bits in machine storage" -- yet they fail
to let you specify the representation *enough*.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Dec 13 '06 #2
On 13 Dec 2006 19:21:19 GMT, Chris Torek <no****@torek.n etwrote in
comp.lang.c:
In article <11************ **********@16g2 000cwy.googlegr oups.com>
<sa*****@yahoo. co.inwrote:
We all know that bit fields are to be used within a structure, like

struct sample
{
int i:4;
}

However, is there any reason as to why it can only be specified only
within a structure or a union and why not standalone as,

int i:4;

"Because Dennis said so", more or less.

(In other words, there is no fundamental reason bitfields could not
be applied to ordinary variables. Note, however, that there is no
"pointer to bitfield" type in C, so there *is* a fundamental reason
they could not be applied to arrays, at least not without first
adding "pointer to bitfield" types.)

Dennis Ritchie also once said that C's bitfields are "a botch and
a blemish". I have to agree: they are not supported "correctly"
at a high level (no arrays of bitfields, no pointers to bitfields,
and so on), and at the same time,
Up to here, I agree with you...
they do not map usefully to
hardware or protocol needs (no control over underlying machine
representations , cannot span machine words, and so on). In other
words, their sole function is to specify representation -- in
this case, "number of bits in machine storage" -- yet they fail
to let you specify the representation *enough*.
....but I disagree with this last point. It may be true enough in
general, but there are specific implementations where it is done well
and extremely useful.

This is the case in many embedded microcontroller/DSP implementations
that I have used. It is quite common for the silicon vendor to supply
extensive header files defining all of the on-chip memory mapped
peripherals with unions overlaying bit fields and unsigned integer
types.

Of course, they make sure the bit ordering and other features of their
definitions compile properly with their compiler. They would not
necessarily be portable to any other compiler, but then neither would
the hardware peripherals they address.

So this may be a portability issue, especially on desktops and work
stations, but can be and is used well, despite its limitations, on
many embedded platforms.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Dec 14 '06 #3
"Jack Klein" <ja*******@spam cop.netwrote in message
news:d6******** *************** *********@4ax.c om...
On 13 Dec 2006 19:21:19 GMT, Chris Torek <no****@torek.n etwrote in
comp.lang.c:
>In article <11************ **********@16g2 000cwy.googlegr oups.com>
<sa*****@yahoo. co.inwrote:
We all know that bit fields are to be used within a structure, like

struct sample
{
int i:4;
}

However, is there any reason as to why it can only be specified only
within a structure or a union and why not standalone as,

int i:4;

"Because Dennis said so", more or less.

(In other words, there is no fundamental reason bitfields could not
be applied to ordinary variables. Note, however, that there is no
"pointer to bitfield" type in C, so there *is* a fundamental reason
they could not be applied to arrays, at least not without first
adding "pointer to bitfield" types.)

Dennis Ritchie also once said that C's bitfields are "a botch and
a blemish". I have to agree: they are not supported "correctly"
at a high level (no arrays of bitfields, no pointers to bitfields,
and so on), and at the same time,

Up to here, I agree with you...
>they do not map usefully to
hardware or protocol needs (no control over underlying machine
representation s, cannot span machine words, and so on). In other
words, their sole function is to specify representation -- in
this case, "number of bits in machine storage" -- yet they fail
to let you specify the representation *enough*.

...but I disagree with this last point. It may be true enough in
general, but there are specific implementations where it is done well
and extremely useful.

This is the case in many embedded microcontroller/DSP implementations
that I have used. It is quite common for the silicon vendor to supply
extensive header files defining all of the on-chip memory mapped
peripherals with unions overlaying bit fields and unsigned integer
types.

Of course, they make sure the bit ordering and other features of their
definitions compile properly with their compiler. They would not
necessarily be portable to any other compiler, but then neither would
the hardware peripherals they address.
Actually, it seems in your last paragraph that you are agreeing with Chris.
Of course, they make sure the bit ordering and other features of their
definitions compile properly with their compiler. They would not
necessarily be portable to any other compiler ...
But they _would_ be portable to any other compiler, if they:
>let you specify the representation *enough*.
There is definite room for improvement with 'C'. Not only is there the
portability issue, but there is a learning and training cost. I've worked
with compilers that pack bitfields MSB first and LSB first, and when I look
at memory dumps and so on I need to refamiliarize myself with how the
compiler packs each time ... it would be easier if they all packed the same
way.

And I also wish there were a way to give the compiler _MORE_ freedom in what
it did in some cases. When one has bitfields that are being used for
compact storage only (and not for ill-advised type conversions and so on),
one should be able to tell the compiler to rearrange fields as it pleases.
Two examples come to mind:

a)A processor I've used in the past had a neat instruction for swapping
nibbles of a byte. The compiler did a really good job if one just happened
to place a bitfield of size 4 as the upper nibble ... the compiler should
have the freedom in some cases for force this alignment by rearranging
bitfields.

b)In general, shifting and masking to extract bitfields is an expensive
operation (for embedded systems). The compiler should have the freedom in
some cases to align frequently-used bitfields against the LSB to eliminate
the shifting.

The *enough* is a problem. One should be able to give the compiler less
freedom in some cases and more freedom in others.

Dec 14 '06 #4

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

Similar topics

5
1530
by: chris | last post by:
What is a class ?? is it like a function ?? this has allways confused me as i am a newby to programming (since Basic in the 80's) thanks for any insight you can give
8
5266
by: Mike | last post by:
Hello, I have a few rather urgent questions that I hope someone can help with (I need to figure this out prior to a meeting tomorrow.) First, a bit of background: The company I work for is developing a web-based application, one part of which involves allowing the user the ability to page through transaction "history" information. The _summary_ history table will have the following fields: ServiceName, Date, User-Ref1, User-Ref2,...
1
1382
by: Reno Raines | last post by:
I'm working with MS Access 2003. I've created a table and the fields I want. For some of these fields I've created a drop-down menu (list box). The data type of the fields are "Text". How do I tell Access to only accept the options listed in the drop-down menu (entered by keyboard or chosen by mouse) and not anything else? I've tried several options in the "Input mask" but it's still not working. Regards
0
1114
by: NWx | last post by:
Hi, I discovered a strange (?!?!) issue related objects saved to cache. Let's explain my situation. I have a webform for user registration. Users enter username, password and other info Username must be unique, obviously, and this is enforced at database level, with a unique index on username field. I use a BO object to save / load data from database. This BO uses a DataRow
4
1848
by: Bill Moran | last post by:
I hadn't really looked at this until I started having problems with it. For those who haven't been following along, I'm converting an application originally written in MSSQL to Postgres. I'm a little startled by how BIT fields are handled differently. Apparently, MSSQL converts freely between BIT and INT. Those who know, already know that Postgres doesn't do this. On one hand, I'm curious as to why. It would seem easy enough to...
0
1427
by: Zlatko Matić | last post by:
Hello. I use MS Graphs nested in reports, extensively. They show subset of report's recordset by linking master/child fields. While reports are based on pass-through queries (PostgreSQL), my graphs are based on local tables which content is refreshed from pass-through queries. The reason for that is because I determined that it is 20 times faster than to base graphs directly on pass-through queries. In order to avoid known problem with...
2
1295
by: alagan | last post by:
Hai,, I am mathiyalagan. Working in a small s/w in my home town.. We are doing some database related windows applications.. Actually we had problem with the datagridview that is we had master detail for Sales Entry (Salesmaster, SalesDetail).. The fields in Sales master.. Invoiceno, customername, address ,date,invoice amt, The fields in Sales Detail
3
2225
by: mahaveer | last post by:
Hi , I am facing a strange problem with STL maps ,I am using gdb to debug my debug my code. Upto one point the number of entries in the map is shown as follows , $79 = {_M_t = { _M_impl = {<std::allocator<std::_Rb_tree_node<std::pair<const long unsigned int, SessionContainer*> > >> = {<__gnu_cxx::new_allocator<std::_Rb_tree_node<std::pair<const long unsigned int, SessionContainer*> > >> = {<No data fields>}, <No data fields>},...
4
2029
omerbutt
by: omerbutt | last post by:
hi there i am amking an inventory application with php,ajax,javascript,html,dhtml,sql the problem is that i have hidden the input fields regarding the replace no's of the parts with this line of code document.getElementById('bgrp1').style.visibility='hidden'; document.getElementById('lable_rp_no1').style.display='none'; document.getElementById('rp_no1').style.display='hidden'; the code for the html part is here
0
9680
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9528
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10456
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10230
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10012
tracyyun
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9052
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
4118
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 we have to send another system
2
3731
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2926
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.