Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 989215
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T05:45:22+00:00 2026-05-16T05:45:22+00:00

I have the following struct: struct SysData { // Topic (32 bits) UTL_UINT16_Tdef SystemID:11;

  • 0

I have the following struct:

struct SysData
{ 
        // Topic (32 bits)
        UTL_UINT16_Tdef SystemID:11;    // set to decimal 25
        UTL_UINT16_Tdef TypeID:5;       // set to 2 (telemetry type)
        UTL_UINT16_Tdef ContentID;      // set to decimal 1234
}

SysData MsgHdr;
MsgHdr.SystemID   = 25;
MsgHdr.TypeID     = 2;
MsgHdr.ContentID  = 0;

If I do something like this:

    unsigned int a;
    memcpy(&a, &MsgHdr, sizeof(MsgHdr));
    headerInfo[0] = a & 0x7FF;
    headerInfo[1] = (a >> 16) & 31;
    headerInfo[2] = (a >> 21) & 0xFFFF;

headerInfo[0] should have the value 25, but it has 36. What am I doing wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T05:45:23+00:00Added an answer on May 16, 2026 at 5:45 am

    You shouldn’t guess on the internal representation of SysData.Compiler may choose to pack bit fields together or not, to align them left or right, etc. It can even choose to map them to 32 bits integer for performances issue. You just can’t know. The unused part of the bitfield may contain garbage and it’s probably where you got your 36.

    It’s strange, but not really hard to check. Change your 25 to other values and see what you get.

    However, memcopying your structure to some unsigned int is probably not a good idea. Why don’t you access bitfield directly ? Like in headerInfo[0] = MsgHdr.SystemID; that’s what bitfields are for. In your example the memcopy is just a loss of time (and also dangerous as you can see).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 490k
  • Answers 490k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer window.open opens a new independent window. It works in most… May 16, 2026 at 9:50 am
  • Editorial Team
    Editorial Team added an answer I think that there is some bug in vim: collections… May 16, 2026 at 9:50 am
  • Editorial Team
    Editorial Team added an answer Try the following public IEnumerable<Item> DoStuff( IEnumerable<Item> someItems, Func<IGrouping<DateTime,decimal>,Item> reduce)… May 16, 2026 at 9:50 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I have the following struct in C++: #define MAXCHARS 15 typedef struct { char
I have the following struct in C++: struct routing_entry { unsigned long destSeq; //
I have the following struct: struct cell { int nmbr; struct cell *p; };
I have the following struct: typedef struct{ int vin; char* make; char* model; int
I have the following struct: struct Datastore_T { Partition_Datastores_T cmtDatastores; // bytes 0 to
I have the following struct declaration and typedef in my code: struct blockHeaderStruct {
I have the following struct: [StructLayout(LayoutKind.Sequential, Pack = 1)] struct cAuthLogonChallenge { byte cmd;
I have the following struct in C# class public struct Employee { public const
Suppose I have the following struct and function returning a pointer: typedef struct {
I have defined the following struct to represent an IPv4 header (up until the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.