Thread: Passing a byte and its bits to a function

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477
    Quote Originally Posted by slingerland3g View Post
    With a union you can only store one or the other but not both as they share the same memory space. So are you needing to set(store) the specific bits of a passed in byte to a function within your struct definition? What the purpose of the char then? There are other ways of doing this. As I see it you will need to set your bits like so:


    variable.bits.b0 = 1;

    Or something like that.
    It's rather that he can store them as both, not one or the other. If you had a union with a double and an int, and you put a double such as 1.5 in it, and then retrieved the int, you would get the int whose bits and bytes are the direct equivalent to the bits and bytes of the double(At least, as much of the double as the int can store, eg. 4 bytes). If you were to cast it to an int, you'd get 1.0.

    This effectively means he can assign a whole byte to the unsigned char part of his union, and then retrieve its individual bits, as well as change them.
    "What's up, Doc?"
    "'Up' is a relative concept. It has no intrinsic value."

  2. #2
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    I believe the best way to do this is not with bit-fields. It is with a mask and functions. Do this:
    Code:
    char getBit(unsigned char byte, char pos) //pos {1,8}
    {
        switch(pos)
        {
             case 1:
                 return byte & 1; // 00000001
             case 2:
                 return byte & 2; // 00000010
             ...
             case 8:
                 return byte & 128; // 10000000
        }
    }
    
    void setBit(unsigned char* byte, char bit, char pos) //bit = 0 or 1, pos {1,8}
    {
        if (bit == 0)
            switch(pos)
            {
             case 1:
                 *byte &= 254; // 11111110
             ...
             case 8:
             ...     
            }
        else if (bit == 1)
            switch(pos)
            {
             case 1:
                 *byte |= 1; // 00000001
             ...
             case 8:
             ...     
            } 
    }
    or sth like that. So you use an unsigned char as a byte and just access each bit with bit operators. What you do is also possible, but again, you would need to copy each time the value of byte to bits and vice versa if you want to use a struct.

    edit: I would guess that you cannot actually access 1 bit, so each bit will take 1 byte int the memory. So there is no reason to use bit fields if you also want to use the byte as whole. If the struct is actually 2 bytes in total (1 for byte, 1 for bits) then you MIGHT be able to have only the bits and cast the struct to an unsigned char, though don't think something like this will work.
    Last edited by C_ntua; 12-03-2008 at 09:54 PM.

  3. #3
    Registered User
    Join Date
    Aug 2006
    Posts
    100
    Interesting read about "optimizing" by using bit fields.

    http://blogs.msdn.com/oldnewthing/ar...6/9143050.aspx

    Not really worth the effort.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by rdrast View Post
    Interesting read about "optimizing" by using bit fields.

    http://blogs.msdn.com/oldnewthing/ar...6/9143050.aspx

    Not really worth the effort.
    But that wasn't what the original poster was talking about. (S)He wants to read and write bits out of a byte - so we have 8 tightly packed bits in a unsigned char, and we want to overlay those with the same bit pattern defined as one bit each. This requires some sort of bitwise operations to be performed by the code - there is no other way we can take 8 tightly packed bits and "deal with them".

    The linked article talks about trying to store unrelated data items AS TIGHTLY PACKED BITS, in an effort to save space. Which the article argues is a waste of effort, and I would agre (as a general rule at least - if there are thousands of booleans that can be packed tightly together, then it may well be worth it).

    --
    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.

Popular pages Recent additions subscribe to a feed