union and bits

Discussion in 'C Programming' started by chandra sekhar nunna, Aug 14, 2003.

  1. Hello All,

    typedef union
    {
    int m_4ByteData;
    char m_8bitData[4];
    }data;

    data conv;
    conv.m_4ByteData = 1000;

    I am using above union i am getting all the individual 8 bits ( 8 * 4 =
    32 bits for int ). How to get the int value ( m_4ByteData) using
    m_8bitData[4].

    Thanks in advance
    Chandra-
     
    chandra sekhar nunna, Aug 14, 2003
    #1
    1. Advertising

  2. chandra sekhar nunna

    olaf Guest

    Just access the structure

    typedef union
    {
    int m_4ByteData;
    char m_8bitData[4];
    }data;

    data conv;
    conv.m_4ByteData = 1000;
    printf("m_8bitData = %d %d %d %d\n",
    conv.m_8bitData[0],
    conv.m_8bitData[1],
    conv.m_8bitData[2],
    conv.m_8bitData[3]);

    It has the same memmory attached to it

    so &(conv.m_4ByteData) == &(conv.m_8bitData[0]) is always TRUE.

    Greetings Olaf

    chandra sekhar nunna <> wrote in message news:<>...
    > Hello All,
    >
    > typedef union
    > {
    > int m_4ByteData;
    > char m_8bitData[4];
    > }data;
    >
    > data conv;
    > conv.m_4ByteData = 1000;
    >
    > I am using above union i am getting all the individual 8 bits ( 8 * 4 =
    > 32 bits for int ). How to get the int value ( m_4ByteData) using
    > m_8bitData[4].
    >
    > Thanks in advance
    > Chandra-
     
    olaf, Aug 14, 2003
    #2
    1. Advertising

  3. Hi,

    Any idea how round double values using the same concept.

    Bye
    Chandra-

    olaf wrote:

    > Just access the structure
    >
    > typedef union
    > {
    > int m_4ByteData;
    > char m_8bitData[4];
    > }data;
    >
    > data conv;
    > conv.m_4ByteData = 1000;
    > printf("m_8bitData = %d %d %d %d\n",
    > conv.m_8bitData[0],
    > conv.m_8bitData[1],
    > conv.m_8bitData[2],
    > conv.m_8bitData[3]);
    >
    > It has the same memmory attached to it
    >
    > so &(conv.m_4ByteData) == &(conv.m_8bitData[0]) is always TRUE.
    >
    > Greetings Olaf
    >
    > chandra sekhar nunna <> wrote in message news:<>...
    > > Hello All,
    > >
    > > typedef union
    > > {
    > > int m_4ByteData;
    > > char m_8bitData[4];
    > > }data;
    > >
    > > data conv;
    > > conv.m_4ByteData = 1000;
    > >
    > > I am using above union i am getting all the individual 8 bits ( 8 * 4 =
    > > 32 bits for int ). How to get the int value ( m_4ByteData) using
    > > m_8bitData[4].
    > >
    > > Thanks in advance
    > > Chandra-
     
    chandra sekhar nunna, Aug 14, 2003
    #3
  4. chandra sekhar nunna

    Dan Pop Guest

    In <> chandra sekhar nunna <> writes:

    >Hello All,
    >
    >typedef union
    >{
    > int m_4ByteData;
    > char m_8bitData[4];
    >}data;
    >
    >data conv;
    >conv.m_4ByteData = 1000;
    >
    >I am using above union i am getting all the individual 8 bits ( 8 * 4 =
    >32 bits for int ). How to get the int value ( m_4ByteData) using
    >m_8bitData[4].


    Put the stuff into m_8bitData[4] and read the value of m_4ByteData.
    In theory, this invokes undefined behaviour, in practice, it works.

    You may want to replace the magical constant 4 by sizeof(int) and to
    use the type unsigned char for m_8bitData[4].

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
     
    Dan Pop, Aug 14, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matt Garman
    Replies:
    1
    Views:
    677
    Matt Garman
    Apr 25, 2004
  2. GGG
    Replies:
    10
    Views:
    12,635
    Donar
    Jul 6, 2006
  3. Peter Dunker

    union in struct without union name

    Peter Dunker, Apr 26, 2004, in forum: C Programming
    Replies:
    2
    Views:
    889
    Chris Torek
    Apr 26, 2004
  4. sarmin kho
    Replies:
    2
    Views:
    840
    A. Lloyd Flanagan
    Jun 15, 2004
  5. Miki Tebeka
    Replies:
    1
    Views:
    452
    Marcin 'Qrczak' Kowalczyk
    Jun 14, 2004
Loading...

Share This Page