LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-17-2011, 05:58 AM   #1
Santoshkb
Member
 
Registered: Sep 2007
Posts: 56

Rep: Reputation: 15
Question Bitfields query.. after typecasting.


Hi all,

Have a usage of the following structures(with bit fields), after typecasting to the similar datasize structure and accessing 2 8bits as single 16bit datamember is resulting incorrect(swapping of nibbles).

What is the cause,
I think endianess should not be issue, as data assignment and accessing is on same machine, please do know how to fix this,


Host machine: Linux 64 Bit.
2.6.32-131.0.15.el6.x86_64 #1 SMP Tue May 10 15:42:40 EDT 2011 x86_64 x86_64 x86_64 GNU/Linux

Code:
#include<stdio.h>
#include <stdlib.h>
#include <string.h>


typedef unsigned int UINT32;
typedef struct
{
        UINT32       SigName:8;    /* member of MG_EVT_NAME_ENUM */
        UINT32       SigCnxn:8;    /* number of Connection target (IX_NULL if none) */
        UINT32       SigParm:16;   /* signal parameter value  */
} MG_SIGNAL_VECTOR_T32;

typedef struct
{
        UINT32       RqEvtName:8;    /* member of MG_EVT_NAME_ENUM */
        UINT32       RqEvtCnxn:8;    /* number of Connection target (IX_NULL if none) */
        UINT32       RqEvtActn:8;    /* action(s) OR of MG_EVT_ACTION_ENUMs */
        UINT32       RqEvtSetn:8;    /* set # of embedded EVENT SET (IX_NULL if none) */
} MG_RQEVT_VECTOR_T32;

main()
{
        MG_RQEVT_VECTOR_T32 * pEvt = malloc(sizeof(MG_RQEVT_VECTOR_T32));

        memset (pEvt, 0, sizeof(MG_RQEVT_VECTOR_T32));
        pEvt->RqEvtActn = 0;
        pEvt->RqEvtSetn |= 0xff;


        printf("RqEvtName =%d, RqEvtCnxn =%d, RqEvtActn=%d, RqEvtSetn=%d\n", pEvt->RqEvtName, pEvt->RqEvtCnxn, pEvt->RqEvtActn, pEvt->RqEvtSetn);


        MG_SIGNAL_VECTOR_T32 * pEvt1 = (MG_SIGNAL_VECTOR_T32 *)pEvt;


        printf("SigName =%d, SigCnxn=%d, SigParm=%d\n",pEvt1->SigName, pEvt1->SigCnxn, pEvt1->SigParm);

}

./a.out
RqEvtName =0, RqEvtCnxn =0, RqEvtActn=0, RqEvtSetn=255
SigName =0, SigCnxn=0, SigParm=65280



Expecting the sigparam to have 255...


Thanks much
Santosh
 
Old 11-17-2011, 06:28 AM   #2
jthill
Member
 
Registered: Mar 2010
Distribution: Arch
Posts: 211

Rep: Reputation: 67
Quote:
Originally Posted by Santoshkb View Post
./a.out
RqEvtName =0, RqEvtCnxn =0, RqEvtActn=0, RqEvtSetn=255
SigName =0, SigCnxn=0, SigParm=65280

Expecting the sigparam to have 255...
Code:
$ dc -e '16o65280p'
FF00
Your machine is little-endian, a holdover from the days of soda-straw data paths. Google.
 
Old 01-15-2012, 12:55 AM   #3
Santoshkb
Member
 
Registered: Sep 2007
Posts: 56

Original Poster
Rep: Reputation: 15
Issue was with endianess,
converting the bigendian to little endian format before accessing solved.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
typecasting z99 Programming 4 07-03-2011 11:54 AM
Typecasting void pointer saurabhchokshi Programming 5 04-14-2010 01:00 PM
interesting MySQL query/view query :s mjh Programming 3 03-25-2008 07:30 AM
mysql use output of one query in another query secretlydead Programming 2 11-19-2007 01:25 AM
help with mysql query: return nth rows in query hawarden Programming 2 07-31-2006 06:36 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:55 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration