Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Set whole Data to a struct with BitFields

Set whole Data to a struct with BitFields

Scheduled Pinned Locked Moved C / C++ / MFC
help
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    FrankStar89
    wrote on last edited by
    #1

    Hi all, I need to define a structure (its some spec frame-structure), so that I can assign data and retrieve-fields easily. So my definition is like below

    struct MyDef
    {
    DWORD B1 : 8;
    DWORD B2 : 8;
    DWORD B3 : 8;
    DWORD B4 : 8;
    };

    (Note that, the whole struct contains 32 bits.) Now, I need a way to set some DWORD value to it directly. something like this

    MyDef Def;
    Def.data = 0xAABBCCDD;//This is the requirement. The 'data' needs to be defined in MyDef in such a way that this gets splits to the fields already defined

    (please note that, I need to keep the bit-fields so that I can set the fields as well.) Please help me.

    A L 2 Replies Last reply
    0
    • F FrankStar89

      Hi all, I need to define a structure (its some spec frame-structure), so that I can assign data and retrieve-fields easily. So my definition is like below

      struct MyDef
      {
      DWORD B1 : 8;
      DWORD B2 : 8;
      DWORD B3 : 8;
      DWORD B4 : 8;
      };

      (Note that, the whole struct contains 32 bits.) Now, I need a way to set some DWORD value to it directly. something like this

      MyDef Def;
      Def.data = 0xAABBCCDD;//This is the requirement. The 'data' needs to be defined in MyDef in such a way that this gets splits to the fields already defined

      (please note that, I need to keep the bit-fields so that I can set the fields as well.) Please help me.

      A Offline
      A Offline
      Albert Holguin
      wrote on last edited by
      #2

      Without actually trying this out.... you should be able to take the pointer to the first element and load it with whatever data you want. Assuming your data matches the size of the structure, there should be no issue, be ware of data packing/memory alignment though. Read this before you get yourself into trouble.[^]

      1 Reply Last reply
      0
      • F FrankStar89

        Hi all, I need to define a structure (its some spec frame-structure), so that I can assign data and retrieve-fields easily. So my definition is like below

        struct MyDef
        {
        DWORD B1 : 8;
        DWORD B2 : 8;
        DWORD B3 : 8;
        DWORD B4 : 8;
        };

        (Note that, the whole struct contains 32 bits.) Now, I need a way to set some DWORD value to it directly. something like this

        MyDef Def;
        Def.data = 0xAABBCCDD;//This is the requirement. The 'data' needs to be defined in MyDef in such a way that this gets splits to the fields already defined

        (please note that, I need to keep the bit-fields so that I can set the fields as well.) Please help me.

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        You can do it by using a union[^] thus:

        union MyDef
        {
        DWORD W1;
        struct bits
        {
        DWORD B1 : 8;
        DWORD B2 : 8;
        DWORD B3 : 8;
        DWORD B4 : 8;
        };
        };

        which maps W1 to the same memory space as the four byte fields. You can now refer to the whole DWORD as W1 of the union structure.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups