473,407 Members | 2,314 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,407 software developers and data experts.

Bit Fields

I have the program illustrating use of Bit-Fields:

#include<stdio.h>

struct DISK_REGISTER {
unsigned ready : 1;
unsigned error_occured : 1;
unsigned disk_spinning : 1;
unsigned write_protect : 1;
unsigned head_loaded : 1;
unsigned error_code : 8;
unsigned track : 9;
unsigned sector : 5;
unsigned command : 5;
};

int main(void)
{
struct DISK_REGISTER dr;

/*...no more code here ; just trying out...*/

return 0;
}

When i run it through a debugger( Visual C++ 08)
I get the following results :
Autos:
Name Value Type

ready 0 unsigned int
error_occured 0 unsigned int
disk_spinning 1 unsigned int
write_protect 1 unsigned int
head_loaded 0 unsigned int
error_code 102 unsigned int
track 102 unsigned int
sector 19 unsigned int
command 25 unsigned int

Can anyone please explain ,what the values mean?
I know that Bit-Fields are not-portable and probably the values
generated are compiler/system specific.
Further can anyone post pointers to any archive containing some more
examples?
Jan 18 '08 #1
6 2477
In article <fm**********@aioe.org>, Tarique <pe*****@yahoo.comwrote:
>I have the program illustrating use of Bit-Fields:
>#include<stdio.h>
>struct DISK_REGISTER {
unsigned ready : 1;
unsigned error_occured : 1;
unsigned disk_spinning : 1;
unsigned write_protect : 1;
unsigned head_loaded : 1;
unsigned error_code : 8;
unsigned track : 9;
unsigned sector : 5;
unsigned command : 5;
};
>int main(void)
{
struct DISK_REGISTER dr;

/*...no more code here ; just trying out...*/

return 0;
}
>When i run it through a debugger( Visual C++ 08)
I get the following results :
Autos:
Name Value Type
>ready 0 unsigned int
error_occured 0 unsigned int
disk_spinning 1 unsigned int
write_protect 1 unsigned int
head_loaded 0 unsigned int
error_code 102 unsigned int
track 102 unsigned int
sector 19 unsigned int
command 25 unsigned int
>Can anyone please explain ,what the values mean?
The values do not mean anything. You left dr uninitialized, so it
contains a random value. Glancing at the values, I -speculate- that
the memory location had previously been occupied by a pointer
with 'command' overlaying what used to be the most significant bits
of the pointer -- but that's pure speculation, and the value
could have been anything.
--
"There are some ideas so wrong that only a very intelligent person
could believe in them." -- George Orwell
Jan 18 '08 #2
Tarique wrote:
I have the program illustrating use of Bit-Fields:

#include<stdio.h>

struct DISK_REGISTER {
unsigned ready : 1;
unsigned error_occured : 1;
unsigned disk_spinning : 1;
unsigned write_protect : 1;
unsigned head_loaded : 1;
unsigned error_code : 8;
unsigned track : 9;
unsigned sector : 5;
unsigned command : 5;
};

int main(void)
{
struct DISK_REGISTER dr;

/*...no more code here ; just trying out...*/

return 0;
}

When i run it through a debugger( Visual C++ 08)
I get the following results :
Autos:
Name Value Type

ready 0 unsigned int
error_occured 0 unsigned int
disk_spinning 1 unsigned int
write_protect 1 unsigned int
head_loaded 0 unsigned int
error_code 102 unsigned int
track 102 unsigned int
sector 19 unsigned int
command 25 unsigned int

Can anyone please explain ,what the values mean?
Absolutely nothing.

You've created the structure with automatic storage.

It is not guaranteed to be initialised to anything meaningful.
Jan 18 '08 #3
On Jan 18, 6:25 pm, Tarique <peo_...@yahoo.comwrote:
I have the program illustrating use of Bit-Fields:
<snip>
>
When i run it through a debugger( Visual C++ 08)
I get the following results :
Autos:
Name Value Type

ready 0 unsigned int
error_occured 0 unsigned int
disk_spinning 1 unsigned int
write_protect 1 unsigned int
head_loaded 0 unsigned int
error_code 102 unsigned int
track 102 unsigned int
sector 19 unsigned int
command 25 unsigned int
A debugs programs for a certain architecture and implementation.
C is beyond these. What you are inspecting is the values of
uninitialiazed objects.
I know that Bit-Fields are not-portable and probably the values
Bit-fields as a concept are portable. C89 has them.
Further can anyone post pointers to any archive containing some more
examples?
Regarding bit-fields?
look question 2.26 at the comp.lang.c FAQ
<http://c-faq.com/>
Jan 18 '08 #4
Thank You.
I got the point.
Jan 18 '08 #5
Tarique wrote:
I have the program illustrating use of Bit-Fields:
[The bit fields are irrelevant to your question]
[...]
int main(void)
{
struct DISK_REGISTER dr;
^^^^^^^^^^^^^^^^^^^^^^^
This is an uninitialized local variable. Its contents are indeterminate.
return 0;
}
[Tarique's question is about the values of dr as seen by his debugger]
Can anyone please explain ,what the values mean?
They mean nothing.
'dr' is an uninitialized local variable. Its contents are indeterminate.
I know that Bit-Fields are not-portable and probably the values
generated are compiler/system specific.
Bit-fields are portable. The manner in which they are stored becomes
important only when you write and read them, but that's true of _any_
variable written in one environment and read in another. Again,
bit-fields have *nothing* to do with your problem.
Jan 18 '08 #6
In article <5v*************@mid.individual.net>,
Martin Ambuhl <ma*****@earthlink.netwrote:
>Bit-fields are portable. The manner in which they are stored becomes
important only when you write and read them, but that's true of _any_
variable written in one environment and read in another.
By "write and read them" I take it you mean "write and read them
to a stream (or file)". Whether that is what you meant is not
clear, but you might have perhaps not taken into account their
use in system interfaces. I don't -think- you mean the
vacuous statement that "if you just leave them sitting in memory
and do not use them in any way, then the manner in which they
are stored is not important."
The maximum bit-field size is not entirely specified. It
is a constraint violation if the number of bits specified exceeds
the number of bits in an "ordinary object" of the specified type,
int, signed int, unsigned int, or (C99) _Bool . However, the
underlying sizes of those objects is not fixed, so a bit-field
size of (say) 30 is valid on an implementation where int is 30 or more
bits, but not valid on an implementation where an int is only 16 bits.
For portability, one should thus restrict one's bitfields to 16 bits.
Unfortunately it is not uncommon for real interfaces to
be specified in terms of 32 bit words, and it is a nuisance to
portably synthesize a bit-field that crosses a 16 bit boundary.
The implementation is permitted to allocate any memory unit big
enough to hold the bitfield, so the implementation might choose
(for example) to pack a structure of bitfields inside a long
(say 32 bits) while not providing the tools to get the fields
where you want. For example, what does the following mean?

struct atest { unsigned int a:3; unsigned int b:16; unsigned int c:13 };

If the maximum memory unit size the implementation uses is 16 bits
(assuming the int is normally 16 bits), then this would be
(a, padding, b, c, padding) or (padding, a, b, padding, c) for
a total of 3 ints storage. But if the maximum memory unit size the
implementation uses is 32 bits (even through the int is normally 16),
or if int is 32 bits, then it would all get packed into 32 bits.
Stranger variations are possible for memory allocation unit
sizes from 20 to 32...
As you correctly point out (or imply), the padding differences are
A Problem for any kind of I/O stream interpreted by a different system,
but the variations in the maximum bit-field sizes make a code portability
difference, and the variations in the memory allocation unit together
with the variations in the maximum bit-field sizes can result in
having to piece together a structure literally bit-by-bit when
trying to do interoperability.
--
"History is a pile of debris" -- Laurie Anderson
Jan 18 '08 #7

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

Similar topics

2
by: Tim Cowan | last post by:
Hi I am trying to create a CSV file with some fields in double quotes ("). I am not having any luck. This is what I want; "8684","COSTING","000010001","REG",X4,15.00, ,14,0,14,9,6 I am...
0
by: bill mahoney | last post by:
I have access 2k and I have 2 tables. One is a 7X6 table representing a month with zeroes padding the fields where no day exist. For example February 2003 0 0 0 0 0 0 1 2 3 4 5 6 7 ...
4
by: Melissa | last post by:
I have a frontend file named CustomerApp and backend file named CustomerData. CustomerApp is at C:\Customer Database and CustomerData is at S:\Customer Database. Could someone help me with the code...
5
by: Megan | last post by:
Hi everybody- I'm helping a friend with a music database. She has an old one and is creating a new one. She wants to compare records and fields in the old database with records and fields in the...
2
by: Darryl Kerkeslager | last post by:
As the subject above hopefully makes clear, I want to do several reports, "with lots of fields not otherwise in database". These reports also have variable-length text. I have defined the...
0
by: Proteus | last post by:
Hi, I'm using access 97 on windows 98. I have table with 162 fields in it. I need to withdraw this information in a (totals) query with 164 fields (162 "avg" fields and 2 "count" fields). When I...
5
by: Genboy | last post by:
My "VIS" Website, which is a C# site created in VS.NET, Framework 1.1, is no longer compiling for me via the command line. As I have done 600 times in the last year and a half, I can compile to...
4
by: Vigneshwar Pilli via DotNetMonster.com | last post by:
Hey, Well, I have a Problem. I have designed a table which has few fields which are being declared in the database of type .... char of length 10. and other fields with varchar 50 and...
9
by: sean.scanlon | last post by:
can someone help understand how i can could access a struct field dymanically like: foo->fields ? when i try to compile this i get the following error: 'struct pwd' has no member named 'fields'...
482
by: bonneylake | last post by:
Hey Everyone, Well i am not sure if this is more of a coldfusion problem or a javscript problem. So if i asked my question in the wrong section let me know an all move it to the correct place. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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:
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...
0
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...
0
isladogs
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 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.