Thread: Structure definition

  1. #16
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    He cannot have it both ways. You cannot just keep track of values spread over random number of bits, then expect to get it back in the same manner.

    Actually, with my example, you can. If you want the original input, you display using:

    union x somevar;

    somevar.b[0] ... b[3]

    If you want the data as it is spread out over the bit fields, you do it:

    somevar.a.a ... somevar.a.d

    You cannot, I repeat cannot simply have both worlds with a single variable, unless you use a union.

    You cannot mash your data acroass a bitfield (which has different bit alignments than your original data) and expect to just fetch it back into it's original form with simple printing operators.

    The only, and best, way to do this, is a union as I have shown.

    I'm not harping at you, I'm letting him know that you cannot simply mash your stuff into a bitfield and then wonder why "it doesn't print right!".

    And actually, to quote the original post:
    How do I write a structure that will retreive information from this array in the following manner -->

    the first three bits contain information a
    the next 5 bits contain information b
    the next 12 bits contain information c
    the next 12 bits contain information d
    It does sound like he just wants it hammered into a bit field, so he can then just display the bitfield (not the original data--he says nowhere that he wants to print its original form!).

    As such, memcopy would be fine. If he doesn't need to preserve the original data, and just wants to stick it into a bitfield, the union works, memcopy works, etc.

    However, if he decides he needs to see the original data, the union is the best bet. (Plus it's easy.)

    Quzah.
    Hope is the first step on the road to disappointment.

  2. #17
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    Agreed

  3. #18
    Registered User
    Join Date
    Jul 2002
    Posts
    15
    Thanks for this discussion folks. Let me restate my problem so that there is clarity.
    I want to define a structure that will retreive the meaning of the char array into member variables. The meaning is
    first 3 bits --> a
    next 5 bits --> b
    next 12 bits --> c
    next 12 bits --> d

    I need to define this structure, (it may be a union also. I totally control this structure) so that I can say struct.a and get the first 3 bits from that byte stream. Conversely-->

    if I say struct.a=0x3
    struct.b=0x14

    and then try to access the char array (in the union), it should get me the byte string--> 0111 0100 ...

    as you can see here the first 3 bits are 0x3 and the next 5 bits are 0x14 ...

    Now with the union solution, this is the union I used

    typedef struct
    {
    unsigned long a : 3;
    unsigned long b : 5;
    unsinged long c : 12;
    unsigned long d : 12;
    } Bits;

    typedef union
    {
    Bits b;
    char str[4];
    } Union;

    Union ob;
    ob.str[0] = 0x1;
    ob.str[1] = 0x7b;
    ob.str[2] = 0xc0;
    ob.str[3] = 0x08;

    now ob.b.a should be 0
    ob.b.b should be 1
    ob.b.c should be 0x7bc
    ob.b.d should be 0x008

    it is not so ... How do I rearrange the order of members in the Bits structure to achieve this.

    Also
    Union ob;
    ob.b.a=0;ob.b.b=0x1;ob.b.c=0x7bc;ob.b.d=0x008;

    now if I check ob.str it should have the following string
    0x1 0x7b 0xc0 0x08

    Thanks for your interest in this post.

  4. #19
    Registered User
    Join Date
    Jul 2002
    Posts
    15
    Folks,
    I think I got it ...

    I needed to place the union defenition like this -->

    struct bits
    {
    unsigned long d : 12
    unsigned long c : 12
    unsinged long b : 5
    unsinged long a : 3
    }


    Now the byte stream that comes in has to be reversed for every four bytes. i.e x1 x2 x3 x4 x5 x6 x7 x8 should become

    x4 x3 x2 x1 x8 x7 x6 x5 ...

    now cast the new array to this structure. YOu will get what the bit values ... Now populate this struct and then memcpy into a four byte array and then reverse again, this will get you the original string.

    thanks for your guidance

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. DLL compiling question
    By Noose in forum Windows Programming
    Replies: 2
    Last Post: 12-16-2004, 07:16 AM
  4. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM