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. Managed C++/CLI
  4. Structures with using bit fields

Structures with using bit fields

Scheduled Pinned Locked Moved Managed C++/CLI
c++
3 Posts 2 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.
  • A Offline
    A Offline
    Amrit Agr
    wrote on last edited by
    #1

    Hi Developers, I am trying to run this chunk of code.

    struct value
    {
    int bit1:1;
    int bit3:4;
    int bit4:4;
    }bit;
    printf("%d\n", sizeof(bit));

    I am getting value "4". I am running this program in 32 bit machine using Microsoft Visual C++ complier. Can you please clarify about the result.

    L 1 Reply Last reply
    0
    • A Amrit Agr

      Hi Developers, I am trying to run this chunk of code.

      struct value
      {
      int bit1:1;
      int bit3:4;
      int bit4:4;
      }bit;
      printf("%d\n", sizeof(bit));

      I am getting value "4". I am running this program in 32 bit machine using Microsoft Visual C++ complier. Can you please clarify about the result.

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

      You have 9 bits in your field and the default alignment of object code in 32bit architecture is word (32 bit) boundaries. So your structure item will be stored in a 32bit word. You could make it smaller by using a #pragma pack[^] statement. By the way, this is the managed C++ forum, your questions really belong in http://www.codeproject.com/Forums/1647/C-Cplusplus-MFC.aspx[^].

      A 1 Reply Last reply
      0
      • L Lost User

        You have 9 bits in your field and the default alignment of object code in 32bit architecture is word (32 bit) boundaries. So your structure item will be stored in a 32bit word. You could make it smaller by using a #pragma pack[^] statement. By the way, this is the managed C++ forum, your questions really belong in http://www.codeproject.com/Forums/1647/C-Cplusplus-MFC.aspx[^].

        A Offline
        A Offline
        Amrit Agr
        wrote on last edited by
        #3

        Thanks a lot Rechard.. for the clarification

        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