troubleshooting Question

improve padding addition code

Avatar of bachra04
bachra04Flag for Canada asked on
CC++
15 Comments2 Solutions373 ViewsLast Modified:
I need an improvement to the getMemberListSize methods using shift opration etc...
/// This structure is defined for a little endian platform.
/// 
///     0               1               2               3
///     0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7 0 1 2 3 4 5 6 7
///    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
///    |    RadioId    |   RS  |   DS  |T|        ACN          |  RRRR |
///    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
///    |                        MMI                    |   memberId    |
///    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
///    |   |memberId...        |    memberId...    |    memberId...    |  
///    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
///    ...
 
/// </summary>
struct RadioEvStruct
{ 
   /////////////////////////////////////////////////////////////////////
   //        First Line of packet
   /////////////////////////////////////////////////////////////////////
   unsigned __int16 RadioId:8; 	            //0-255 possible patch Id
   unsigned __int16 RadioState:4;            //(1)Keyed,(0)Not Keyed
   unsigned __int16 DimState:4;              //(1)Active,(0)Deactive
   unsigned __int16 MMI:1;                   //(0)group,(1)group (tetra)
   unsigned __int16 MemberNb:11;             // The number of members 
                                             // up to 1024
   unsigned __int16 Reserved:4;  	            // Reserved bits for future use.
 
   /////////////////////////////////////////////////////////////////////
   //        The rest of the packet
   /////////////////////////////////////////////////////////////////////
   unsigned char*   aMemberList;         // Contains 0 or 1 MMI (24 bits)
                                         // + 0 to 50 members (10 bits each)                                        
                                         // members + (optional) required 
                                         // padding to avoid alignment problem.
 
   /// <summary>
   /// Comparison operator.
   /// </summary>
   //////////////////////////////////////////////////////////////////////////
   // memberwise comparison is safer because of the potential padding problem 
   // Here we assume there is no padding or alignment problems as well as   
   // everything is little indian. 
   // The length of the MemberList + 4 is multiple of 4.
 
   bool operator == (RadioEvStruct RadioEvStatus)
   {
      return  memcmp (this, & RadioEvStatus, getMemberListSize() + 4);  
   }
 
   /// <summary>
   /// Inverse Comparaison operator.
   /// </summary>
   bool operator != (RadioEvStruct RadioEvStatus)
   {
      return !(*this == RadioEvStatus);
   }
   
   unsigned __int16 getMemberListSize()
   {
   	// calculates and returns the size of the memberList based
	// on the values of MMI and MemberNb fields.
 
	   unsigned __int16 uGroupSize = this.MMI ? 3:2;
      unsigned __int16 uTotalBitsSize = (uGroupSize + this.MemberNb)*32;
      return (uTotalBitsSize + (( 4 - uTotalBitsSize % 4)%4))/8;
 
   }
};
ASKER CERTIFIED SOLUTION
_phoenix_

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 2 Answers and 15 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 2 Answers and 15 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros