Bitfield

This is a discussion on Bitfield within the C Programming forums, part of the General Programming Boards category; hi, I am now reading something about bitfields. I just wanna know what is the importance of bitfield? I don't ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    115

    Bitfield

    hi, I am now reading something about bitfields. I just wanna know what is the importance of bitfield? I don't know if the book that I read is not enough to make me understand the use of bitfield. I have searched over the net for an hour but never found any good tuts about it. Is there any good tutorial that you can provide with some good sample codes? thanks

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    This site has a very good tutorial based around the idea of car parks -> http://www.cprogramming.com/tutorial...operators.html

  3. #3
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Are you asking about real world, practical applications of bitfields??

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    115
    Quote Originally Posted by itCbitC View Post
    Are you asking about real world, practical applications of bitfields??
    yes.. I have read a lot about bitfield but they just provide very small explanation and a very few sample code, enough for them to provide a sample not thinking if others would comprehend the importance of bitfield with their very few sample and explanation.

    I have read something about unions as well, and they are all saying the same thing. That all the variables depend on the variable that has the biggest size. They are all stored in the same memory address of that variable. But they were never able to let the reader appreciate and understand the importance and use of unions and bitfields.

    honestly.. somebody advised me that I could use bitfield to access a printer. I was reading something on how to access a printer port via parallel port, I was advised that it's an old school way of accessing a printer it's now USB port.
    Last edited by $l4xklynx; 12-22-2008 at 11:36 PM.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,506
    > honestly.. somebody advised me that I could use bitfield to access a printer
    Downgrade their advice then (or ignore them).

    Given something like
    Code:
    struct bits {
      unsigned int status:1;
      unsigned int enable:2;
      unsigned int data:8;
    };
    You've no idea from one compiler to the next (or even compiler options for the same compiler) about say
    - the size of bits
    - whether bits would be accessed as a single storage unit (assuming it fitted in a single SU)
    - whether status was the MSB or the LSB
    - whether data was in the same SU as enable, or the next SU.

    All of which make bit-fields all but useless for picking apart any kind of external data (say the status register of the parallel port, or the header of a file).

    Also, you can't
    - point to members of a bit-field
    - use pointers or floats in a bit-field

    Practical applications?
    On a desktop, probably none.

    You need to tick yes to all of these before bit-fields become useful:
    - you have a small amount of memory (say an embedded system)
    - all the members of the struct are integers of one sort or another
    - the numeric ranges of the integers are small compared to INT_MAX
    - you have many instances of the struct (say a large array) (see memory)
    - the data will not be communicated to the outside world) (see port/file above)

    It was very useful 40 years back when C first started to appear, and machines had like 64K of memory, and there was only ever one C compiler to worry about, then it was exceedingly useful to squeeze every last bit.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Oct 2008
    Posts
    115
    Quote Originally Posted by Salem View Post
    It was very useful 40 years back when C first started to appear, and machines had like 64K of memory, and there was only ever one C compiler to worry about, then it was exceedingly useful to squeeze every last bit.
    is it still useful nowadays?

    so a bitfield is only used to pack all the variables and make it fit into a smaller amount of byte is my comperehension correct?

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by $l4xklynx View Post
    is it still useful nowadays?

    so a bitfield is only used to pack all the variables and make it fit into a smaller amount of byte is my comperehension correct?
    I have used bitfields for register defintions when I worked on drivers.

    Drivers are nearly always not-very-portable anyways, as the driver is specifically written for a particular OS, and whilst you possibly COULD use gcc-mingw for Windows driver development, the officially supported version of MS Visual C/C++ is included with the (free) MS Windows driver developers kit, so there is really no reason to make such a driver portable across multiple compilers).

    But very often, it is much more portable to achieve the same thing with various combinations of shift/and/or operations. And it's nearly always better to use independent variables for each field - even if it takes up a lot more space, it will not require the same amount of "messing about" that is needed when doing bitfield manipulations.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Oct 2008
    Posts
    115
    Quote Originally Posted by matsp View Post
    I have used bitfields for register defintions when I worked on drivers.

    Drivers are nearly always not-very-portable anyways, as the driver is specifically written for a particular OS, and whilst you possibly COULD use gcc-mingw for Windows driver development, the officially supported version of MS Visual C/C++ is included with the (free) MS Windows driver developers kit, so there is really no reason to make such a driver portable across multiple compilers).

    But very often, it is much more portable to achieve the same thing with various combinations of shift/and/or operations. And it's nearly always better to use independent variables for each field - even if it takes up a lot more space, it will not require the same amount of "messing about" that is needed when doing bitfield manipulations.

    --
    Mats
    I see, I guess i'm gonna take a pretty long time to learn how to use bitfield and include it in any application that I may create.

    Since bitfield was useful 40 years ago as Salem mentioned, that means that bitfield is not widely used now? if so, how come? what supersedes bitfield to make it not widely used nowadays.

  9. #9
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    > what supersedes bitfield to make it not widely used nowadays.
    Salem already mentioned that,

    Quote Originally Posted by Salem extract
    It was very useful 40 years back when C first started to appear, and machines had like 64K of memory, and there was only ever one C compiler to worry about, then it was exceedingly useful to squeeze every last bit.
    There are many compilers now, and memory is no longer an issue (well, less of an issue).

  10. #10
    Registered User
    Join Date
    Oct 2008
    Posts
    115
    Quote Originally Posted by zacs7 View Post
    > what supersedes bitfield to make it not widely used nowadays.
    Salem already mentioned that,

    There are many compilers now, and memory is no longer an issue (well, less of an issue).
    now I understand, since memory is not an issue right now that means that bitfield manipulation is no longer needed because it's only used for application that will be installed to a machine that have an issue with memory, am I correct?

  11. #11
    Registered User
    Join Date
    Dec 2008
    Posts
    9
    Quote Originally Posted by $l4xklynx View Post
    now I understand, since memory is not an issue right now that means that bitfield manipulation is no longer needed because it's only used for application that will be installed to a machine that have an issue with memory, am I correct?
    Hi,
    This is my first post on the site.

    In fact when memory matters some use an unsigned value to act as a bit field. You can work with individual bits using bit-wise operations via macros or inline functions. If implemented well that should be both more memory efficient and faster.

    Regards.

  12. #12
    Registered User
    Join Date
    Oct 2008
    Posts
    115
    Quote Originally Posted by Visitor View Post
    Hi,
    This is my first post on the site.

    In fact when memory matters some use an unsigned value to act as a bit field. You can work with individual bits using bit-wise operations via macros or inline functions. If implemented well that should be both more memory efficient and faster.

    Regards.
    I see, that's more clearer.. since there is an issue with memory before bitfields are used to make process quickly.

  13. #13
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Welcome to the forum, Visitor.

    If anyone is really interested in what bitfields can do, Google up "Crafty" and follow the links. Crafty is a famous chess program, with open source, and uses bitboards and logical operations on those bitboards, extensively.

    The author is a professor at the University of Alabama, and a former world champion chess programmer, who is still very active. His name is Robert Hyatt, and he is very active on the chess programmers forum:

    http:www.talkchess.com/

    He has remarked that it took him more than a year to get thinking in terms of bitboards and chess, really effectively, after he changed Crafty over to bitboards.

    When DOS was 16 bits / word, and systems had a narrow data path, Crafty was frequently lunch meat to so-so chess opponents, because Crafty relied on speed, and now, it just didn't have that speed.

    Well, the "lunch meat" days are over, and Crafty is again in the top tier of chess programs, enjoying the 64 bit OS and larger data paths of today.

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Visitor View Post
    Hi,
    This is my first post on the site.

    In fact when memory matters some use an unsigned value to act as a bit field. You can work with individual bits using bit-wise operations via macros or inline functions. If implemented well that should be both more memory efficient and faster.

    Regards.
    Bitfields decompose into bitwise and/or/shift operations. Just like any manual code (with or without inlines or macros). It is, however, more portable between compilers and processor architectures to use unsigned values and shift/and/or operations to solve the problem - it's still not 100% portable if you want data-file compatibility between different machines on binary data - but that's regardless of whether the data is bitfields, bits managed with shift/and/or or plain integers, since endianess makes this a troublesome situation. Storing data as text instead of binary will solve that problem.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  15. #15
    Registered User
    Join Date
    Dec 2008
    Posts
    9
    Quote Originally Posted by matsp View Post
    Bitfields decompose into bitwise and/or/shift operations. Just like any manual code (with or without inlines or macros). It is, however, more portable between compilers and processor architectures to use unsigned values and shift/and/or operations to solve the problem - it's still not 100% portable if you want data-file compatibility between different machines on binary data - but that's regardless of whether the data is bitfields, bits managed with shift/and/or or plain integers, since endianess makes this a troublesome situation. Storing data as text instead of binary will solve that problem.

    --
    Mats
    Well see wikipedia article on bitfields specially this :

    However, bit members in structs have practical drawbacks. First, the ordering of bits in memory is architecture dependent and memory padding rules varies from compiler to compiler. In addition, many popular compilers generate inefficient code for reading and writing bit members, and there are potentially severe thread safety issues relating to bit fields (especially on multiprocessor systems) due to the fact that most machines cannot manipulate arbitrary sets of bits in memory, but must instead load and store whole words.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bit manipulation
    By Neo1 in forum C++ Programming
    Replies: 8
    Last Post: 03-24-2008, 11:53 AM
  2. bitfield "array"
    By bibsik in forum C++ Programming
    Replies: 6
    Last Post: 04-13-2006, 12:34 AM
  3. bits & bytes
    By pelom in forum C Programming
    Replies: 8
    Last Post: 10-11-2005, 09:45 AM
  4. bitfield memory question
    By sufthingol in forum C++ Programming
    Replies: 19
    Last Post: 03-26-2005, 03:43 PM
  5. Structure definition
    By Unregistered in forum C Programming
    Replies: 18
    Last Post: 07-31-2002, 09:52 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21