CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Jul 2002
    Posts
    69

    bit field question

    i am working with bit fields in order to compress a large amount of information into a smaller amount to better fit in memory for an embedded system. i have never worked with bit fields before, though. the main reason for doing this is all the data is binary, and i find it wasteful to use a full byte in memory for a boolean value (i doubt i'm the only one), though i see the use of it for general purposes (like addressing). my question is whether or not it is legal to make an array inside a struct of bitfields, to make them accessable from a loop, and if it is legal, how is this achieved. consider the following structure :

    typedef struct {
    bool value[39];
    } input;

    is there a way to make the above array but with each boolean value being a single bit? i am unsure, since i realize pointers aren't allowed, but perhaps there is some way to do it still that i don't know about. (i have tried putting a :1 after the bool definition; of course it reaffirms that pointers aren't allowed)

    --paul

  2. #2
    Join Date
    Jun 2002
    Posts
    1,417
    Here is one way:

    Code:
    	unsigned short bits = 6;
    	for(int i = -1; i < 8; i++)
    	{
    		// get a power of 2
    		int one_bit = (i < 0) ? 1 : 2 << i;
    		if(bits & one_bit)
    		{
    			// Found one!
    		}
    	}

  3. #3
    Join Date
    Jun 2002
    Location
    Letchworth, UK
    Posts
    1,020
    I'm not sure about this but I think vector<bool> will pack the bools on some compilers.
    Succinct is verbose for terse

  4. #4
    Join Date
    May 2002
    Location
    Texas
    Posts
    222
    I can't answer your question about arrays of bits.

    Have you thought of using bit masks (AND) or bit shifts (<<)? This is typically the method used in assembly language.

    It seems possible that the compiler could actually bloat your code more if you used arrays.

  5. #5
    Join Date
    Jun 2002
    Location
    Letchworth, UK
    Posts
    1,020
    Kdr Kane is right - I'd completely forgotten about this. Used to do it all the time in X windows.

    Say you want n bools

    unsigned int array[(n - 1) / _INTEGERAL_MAX_BITS + 1];

    _INTEGRAL_MAX_BITS is a MS thing but there will probably be something on your system.

    To set entry s

    array[s/_INTEGRAL_MAX_BITS] |= (1 << (s % _INTEGRAL_MAX_BITS));

    To clear entry s
    array[s/_INTEGRAL_MAX_BITS] &= ~(1 << (s % _INTEGRAL_MAX_BITS));

    To check entry s

    bool set = array[s/_INTEGRAL_MAX_BITS] & (1 << (s % _INTEGRAL_MAX_BITS)) != 0;
    Succinct is verbose for terse

  6. #6
    Join Date
    Jul 2002
    Posts
    69
    thanks, everyone for your comments, i will look at all the advice here and attempt to implement it over the next day or two (ok, that would start monday, not tomorrow ) in the meantime...i gotta debug. cutting the size of the executable in half has it's ups AND downs...heh

    --paul

  7. #7
    Join Date
    Jun 2002
    Location
    Letchworth, UK
    Posts
    1,020
    C++ also has bitset operations

    bitset<39> value; // declares 39 bits

    value.reset(i); // clears bit i

    value.set(i); // sets bit i

    value.test(i); // test bit i

    Another alternative is to use the assembler

    bts, btr and bt instructions
    Succinct is verbose for terse

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured