473,385 Members | 1,958 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,385 software developers and data experts.

Question about bit-fields and portability

Hello everyone,

I read here and there that bit-field usage should be avoided because
it's not portable. My question is why it's not portable?
For example let's say I have a struct in my code with bit-fields like
the following:

struct bitfield_t {
unsigned int val1 : 1;
unsigned int val2 : 3;
unsigned int val3 : 2;
};

How can such a bit-field struct be used in a non portable way? (Can
you give me an example if possible)

In my understanding non-portable means that if you save a struct like
this in a binary form (in a file for example or send it over the
network) and then try to restore it on a platform with different
endianness or compiler it probably will be interpreted differently. Is
this the only case in which the usage of bit-fields is not portable?

Will source code which contains bit-fields like bitfield_t work
(execute) the same on different platforms (Without considering how the
bits are saved on each platform)?

Thanks for your time
Jul 13 '08 #1
2 5922
DiAvOl wrote:
I read here and there that bit-field usage should be avoided
because it's not portable. My question is why it's not portable?
They are perfectly portable, they just can't be pe used portably
to read binary files produced on a different implementation.
For example let's say I have a struct in my code with bit-fields like
the following:

struct bitfield_t {
unsigned int val1 : 1;
unsigned int val2 : 3;
unsigned int val3 : 2;
};

How can such a bit-field struct be used in a non portable way? (Can
you give me an example if possible)
Unless you make non portable assumptions about the representation of
int, and the size of the word in which the bitfields are allocated,
and the
ordering of the bitfield allocation, then you run into problems
reading
binary files containing a struct bitfield_t produced by other
implementations.

Note that 'other implementations' can mean the _same_ compiler with
different settings.
In my understanding non-portable means that if you save a struct like
this in a binary form (in a file for example or send it over the
network) and then try to restore it on a platform with different
endianness or compiler it probably will be interpreted differently. Is
this the only case in which the usage of bit-fields is not portable?
No there are others, such as assuming unsigned short bit fields
are supported, or that a plain int bitfield of 1 bit can have two
values, 0 and -1.
Will source code which contains bit-fields like bitfield_t work
(execute) the same on different platforms (Without considering
how the bits are saved on each platform)?
Generally, yes, unless you go out of your way to make them work
differently. Unfortunately, going of the way seems to be the status
quo. ;-)

--
Peter
Jul 13 '08 #2
On Sun, 13 Jul 2008 13:58:18 -0700 (PDT), DiAvOl <di****@freemail.gr>
wrote in comp.lang.c:
Hello everyone,

I read here and there that bit-field usage should be avoided because
it's not portable. My question is why it's not portable?
There are several reasons why it is not portable.
For example let's say I have a struct in my code with bit-fields like
the following:

struct bitfield_t {
unsigned int val1 : 1;
unsigned int val2 : 3;
unsigned int val3 : 2;
};
The first portability issue with this code is how large the structure
type will be. Even assuming 8-bit bytes (CHAR_BIT == 8), it may be a
single byte on some platforms, 2 or 4 bytes on others.
How can such a bit-field struct be used in a non portable way? (Can
you give me an example if possible)
One of the uses that bit-fields are put to is to map named bit fields
onto hardware devices.

On a platform with 8-bit memory access, this structure _might_ map
well onto the line control register of a memory-mapped 8250 UART, or
one if its enhanced descendents:

struct lcr
{
unsigned int word_size: 2;
unsigned int stop_bits: 1;
unsigned int parity_enable: 1;
unsigned int even_parity: 1;
unsigned int stick_parity: 1;
unsigned int break_ctrl: 1;
unsigned int div_latch_access: 1;
};

On another compiler for the same platform, this structure might
completely mis-match the logical bit fields with the hardware in the
device.
In my understanding non-portable means that if you save a struct like
this in a binary form (in a file for example or send it over the
network) and then try to restore it on a platform with different
endianness or compiler it probably will be interpreted differently. Is
this the only case in which the usage of bit-fields is not portable?
There is actually a case from long ago where a certain vendor of PC
compilers changed the way bit-fields were laid out from one version of
their compiler to the next. Even though the operating system and
processor were the same.
Will source code which contains bit-fields like bitfield_t work
(execute) the same on different platforms (Without considering how the
bits are saved on each platform)?
If you generate and use bit-field structures with an application,
there are not much in the way of portability issues.

If you attempt to communicate them across any sort of communication
interface, or store them to a medium like a disk drive, flash memory,
etc., to be read back later, there can be problems. Not just on
different platforms but even on the same platform with different
compilers or even different versions of the same compiler.

--
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.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
Jul 13 '08 #3

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

Similar topics

3
by: aaj | last post by:
This is a simple question compared to some of the stuff that gets asked (and answered) here, but sometimes its easy to over look the simpler things. I've been working with databases for a few...
1
by: Kurt Krueckeberg | last post by:
In the book C++ Gothcas, Gotcha #7 is an example of using boolean logic to simply code. My question follows this snippet from the book. "Do you have to count to eight when presented with the...
13
by: Amy DBA | last post by:
I've been asked to administer a DB2 V 8 (32-bit install) on a Solaris 64-bit platform. It seems like whomever installed DB2 on the server, goofed for not installing DB2 v8 64 bit. Do I understand...
9
by: Bob | last post by:
Is ASP.NET 1.1 available on the 64 bit extended version of Windows 2003 Server? When I install VS.NET 2003 I then get Service Unavailable from IIS when navigating to the main under construction...
3
by: Ken H | last post by:
Hi I have a question about architecting solutions.. I have a part of a project which requires me to track person details (name, addresses, etc... Should I be creating Person objects, Address...
2
by: Frank Rizzo | last post by:
Hello, I have a pretty nice box in production (2 dual core Opterons) and it is running the 32-bit version of Windows Server 2003 and 32-bit version of SQL Server 2000. The application that's...
17
by: Christian Wittrock | last post by:
Hi, What does ANSI C say about casting an 8 bit pointer to a 16 bit one, when the byte pointer is pointing to an odd address? I have detected a problem in the Samsung CalmShine 16 compiler. This...
168
by: broeisi | last post by:
Hello, Is there a way in C to get information at runtime if a processor is 32 or 64 bit? Cheers, Broeisi
28
by: =?Utf-8?B?R2Vvcmdl?= | last post by:
Hello everyone, I am developing C++ COM native code (unmanaged C++) using Visual Studio 2005. I do not take any new features of 64-bit platform, and currently my code runs fine on 32-bit...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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...

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.