Thread: Bit-Fields in C.

  1. #1
    Registered User
    Join Date
    Sep 2011
    Location
    Athens , Greece
    Posts
    357

    Bit-Fields in C.

    Hello. I have some queries about bit fields :

    Let's look at how the MS-DOS operating system stores the date at which a file created or modified?!

    Code:
    struct file_date {
    
        unsigned int day : 5;   // 5 is the length of this member in bits
        unsigned int month: 4;
        unsigned int year : 7;
    };
    The book that I am reading said that storing these numbers as normal integers would waste space of memory. My queries has to do with the memory layout. I have read that the size of a structure is not the sum of the size of its members because of the CPU allignment, is that true? and if it is why we are talking about memory economy? if we create bit fields like here? how we know that we save some memory space?

    What is the usage of unnamed bit field? What is the "padding" ?

    Assuming that we have the following union :

    Code:
    union int_date {
      unsigned short i;
      struct file_date fd;
    };
    What is the size of this union? It is the biggest member in size but can we know this in advance due to allignment on the structure?

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Quote Originally Posted by MSDN
    The int and unsigned int types have the size of the system word. This is two bytes (the same as short and unsigned short) in MS-DOS and 16-bit versions of Windows, and 4 bytes in 32-bit operating systems. However, portable code should not depend on the size of int.
    Therefore, the size of and the alignment of the bit-field will most likely be 2 in MS-DOS. For the same reason, the union will most likely be 2 bytes in size.

    Bit-fields have little to do with memory alignment. The compiler, when you try to access a bit-field value, does the shifting and masking of the requested bits, in order to seem like you're operating using discrete values.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Does anyone but me think using Bit-Fields is often a case of premature optimization?

    Edit: In the MS-DOS days it was valid optimization because of the 640K memory limit.

    Tim S.
    Last edited by stahta01; 06-27-2014 at 12:19 PM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  4. #4
    Registered User
    Join Date
    Jun 2014
    Posts
    9
    Plus the way the FAT was builded up, You did not have much room for file name, attributes, date and so on.

    LNX, to save memory, let the int contain the date as on disk instead of in the struct

    unsigned int date;

    date = day * 0xf800 + month * 0x0780 + year;

    No struct, no this not that only one int.

  5. #5
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Quote Originally Posted by Romme View Post
    Plus the way the FAT was builded up, You did not have much room for file name, attributes, date and so on.

    LNX, to save memory, let the int contain the date as on disk instead of in the struct

    unsigned int date;

    date = day * 0xf800 + month * 0x0780 + year;

    No struct, no this not that only one int.
    Did you mean to use bitwise "and" instead of multiplication?

    i.e.
    Code:
    date = (day & 0xf800) + (month & 0x0780) + year;
    [edit]
    And maybe some bit shifting?
    [/edit]
    Last edited by Click_here; 06-28-2014 at 04:52 AM.
    Fact - Beethoven wrote his first symphony in C

  6. #6
    Registered User
    Join Date
    Jun 2014
    Posts
    9
    No actually multiplications.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. bit fields
    By Saurabh Mehta in forum C Programming
    Replies: 8
    Last Post: 03-21-2013, 01:28 AM
  2. Bit fields
    By juice in forum C Programming
    Replies: 7
    Last Post: 12-13-2011, 08:38 AM
  3. Bit fields
    By Edelweiss in forum C Programming
    Replies: 5
    Last Post: 08-23-2011, 10:01 AM
  4. bit fields before non-bit fields . . .
    By dwks in forum C Programming
    Replies: 10
    Last Post: 10-13-2005, 02:36 AM
  5. Bit fields
    By GaPe in forum C Programming
    Replies: 8
    Last Post: 01-22-2002, 02:01 PM