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. Dividing the bits in a 16 bit short value

Dividing the bits in a 16 bit short value

Scheduled Pinned Locked Moved C / C++ / MFC
questiondata-structures
6 Posts 3 Posters 1 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.
  • C Offline
    C Offline
    ChemmieBro
    wrote on last edited by
    #1

    I have a structure: typedef struct{ unsigned short Id : 8; unsigned short Name: 8; unsigned short Length: 11; unsigned short Value: 5; } MyStruct; I also have an array of bytes (unsigned char): I want to stick that structure into the first four bytes in the array. Array[0] = MyStruct->Id; Array[1] = MyStruct->Name; ...then what? How can I divide the last two values and put them into the [2] and [3] position in the byte array?

    J CPalliniC 2 Replies Last reply
    0
    • C ChemmieBro

      I have a structure: typedef struct{ unsigned short Id : 8; unsigned short Name: 8; unsigned short Length: 11; unsigned short Value: 5; } MyStruct; I also have an array of bytes (unsigned char): I want to stick that structure into the first four bytes in the array. Array[0] = MyStruct->Id; Array[1] = MyStruct->Name; ...then what? How can I divide the last two values and put them into the [2] and [3] position in the byte array?

      J Offline
      J Offline
      JudyL_MD
      wrote on last edited by
      #2

      try a union[^]

      1 Reply Last reply
      0
      • C ChemmieBro

        I have a structure: typedef struct{ unsigned short Id : 8; unsigned short Name: 8; unsigned short Length: 11; unsigned short Value: 5; } MyStruct; I also have an array of bytes (unsigned char): I want to stick that structure into the first four bytes in the array. Array[0] = MyStruct->Id; Array[1] = MyStruct->Name; ...then what? How can I divide the last two values and put them into the [2] and [3] position in the byte array?

        CPalliniC Offline
        CPalliniC Offline
        CPallini
        wrote on last edited by
        #3

        Please note sizes are identical. Methods (1) quick & dirty: copy memory

        MyStruct m;
        unsigned char a[4];
        memcpy(a, &m, sizeof(a));

        (2) perhaps more elegant: use union

        typedef union
        {
        MyStruct m;
        unsigned char a[4];
        }MyUnion;

        MyUnion mu;
        unsigned char array[4];
        // initialize mu.m.Id, mu.m.Name, mu.m.Length, mu.m.Value
        for (i=0; i<4; i++)
        {
        array[i]=mu.a[i];
        }

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

        In testa che avete, signor di Ceprano?

        C 1 Reply Last reply
        0
        • CPalliniC CPallini

          Please note sizes are identical. Methods (1) quick & dirty: copy memory

          MyStruct m;
          unsigned char a[4];
          memcpy(a, &m, sizeof(a));

          (2) perhaps more elegant: use union

          typedef union
          {
          MyStruct m;
          unsigned char a[4];
          }MyUnion;

          MyUnion mu;
          unsigned char array[4];
          // initialize mu.m.Id, mu.m.Name, mu.m.Length, mu.m.Value
          for (i=0; i<4; i++)
          {
          array[i]=mu.a[i];
          }

          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

          C Offline
          C Offline
          ChemmieBro
          wrote on last edited by
          #4

          It looks like the memcpy might work. I didn't really want to go the route of using a Union. I was actually looking for a method to do this using bit shifting. Something like... unsigned char NewChar = MyStruct->Length << 8; which would put the 8 leftmost bits in Length into the NewChar, which you could put into Array[2]; but then, how do I get the other three bits from Length and merge them with the 5 bits from MyStruct->Value? I kind of know what I want to do it, but I don't know exactly how, what commands to use or what I'm doing.

          CPalliniC 1 Reply Last reply
          0
          • C ChemmieBro

            It looks like the memcpy might work. I didn't really want to go the route of using a Union. I was actually looking for a method to do this using bit shifting. Something like... unsigned char NewChar = MyStruct->Length << 8; which would put the 8 leftmost bits in Length into the NewChar, which you could put into Array[2]; but then, how do I get the other three bits from Length and merge them with the 5 bits from MyStruct->Value? I kind of know what I want to do it, but I don't know exactly how, what commands to use or what I'm doing.

            CPalliniC Offline
            CPalliniC Offline
            CPallini
            wrote on last edited by
            #5

            Just for the academy (since memcpy is more concise and probably faster).

            MyStruct m;
            unsigned char a[4];
            a[2] = (unsigned char) m.Length; // this cuts off the 3 MSB bits.
            a[3] = (m.Length >> 8) | (m.Value << 3);

            :)

            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
            This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

            In testa che avete, signor di Ceprano?

            C 1 Reply Last reply
            0
            • CPalliniC CPallini

              Just for the academy (since memcpy is more concise and probably faster).

              MyStruct m;
              unsigned char a[4];
              a[2] = (unsigned char) m.Length; // this cuts off the 3 MSB bits.
              a[3] = (m.Length >> 8) | (m.Value << 3);

              :)

              If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
              This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

              C Offline
              C Offline
              ChemmieBro
              wrote on last edited by
              #6

              Cool. That's what I was looking for originally. But the memcpy works. THANKS!

              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