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. Spliiting a DWORD

Spliiting a DWORD

Scheduled Pinned Locked Moved C / C++ / MFC
question
11 Posts 5 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.
  • B Ben Aldhouse

    This is my post accidently put in the lounge... >>>> This should be pretty easy, right? I'm feeling pretty brain dead right now, though... In CTreeCtrl the nodes have a DWORD data item and I want to make use of that as a 24 bit value and an 8 bit value. What would be a nice way of getting and setting the DWORD from two other variables? Thanks, Ben. <<<< Anyway I'm thinking something like (in pseudocode...) getting... 8bitVal = DWordVal mod 2**24 32bitVal = DWordVal / 2**24 setting... DWordVal = 24bitVal * 2**24 + 8bitVal I haven't tried this so is probably wrong and I have a feeling that that there is a nicer way of doing it?

    E Offline
    E Offline
    Electron Shepherd
    wrote on last edited by
    #2

    How about

    DWORD dwData = <value>;

    DWORD dwEightBit = (dwData & 0x000000FF);
    DWORD dwTwentyFourBit = (dwData & 0xFFFFFF00) >> 8;

    and

    DWORD dwEightBit = <value1>;
    DWORD dwTwentyFourBit = <value2>;

    DWORD dwData = (dwTwentyFourBit << 8) | dwEightBit;

    This has just been typed in, not compiled or tested, you understand. The second code snippet assumes value1 <= 255 and value2 <= 16777215.

    Server and Network Monitoring

    B 2 Replies Last reply
    0
    • B Ben Aldhouse

      This is my post accidently put in the lounge... >>>> This should be pretty easy, right? I'm feeling pretty brain dead right now, though... In CTreeCtrl the nodes have a DWORD data item and I want to make use of that as a 24 bit value and an 8 bit value. What would be a nice way of getting and setting the DWORD from two other variables? Thanks, Ben. <<<< Anyway I'm thinking something like (in pseudocode...) getting... 8bitVal = DWordVal mod 2**24 32bitVal = DWordVal / 2**24 setting... DWordVal = 24bitVal * 2**24 + 8bitVal I haven't tried this so is probably wrong and I have a feeling that that there is a nicer way of doing it?

      B Offline
      B Offline
      Ben Aldhouse
      wrote on last edited by
      #3

      Oh god, I know I'm confused. Loads of mistakes. I know. Rather than edit my previous post here are my own ammendments... If the 24 bit value are the high end bits and the 8 bit value are the low end bits... getting... 8bitVal = 32bitVal mod 2**8 24bitVal = 32bitVal / 2**8 setting... 32bitVal = 24bitVal * 2**8 + 8bitVal Still think there may be an easier way of doing this...

      L 1 Reply Last reply
      0
      • E Electron Shepherd

        How about

        DWORD dwData = <value>;

        DWORD dwEightBit = (dwData & 0x000000FF);
        DWORD dwTwentyFourBit = (dwData & 0xFFFFFF00) >> 8;

        and

        DWORD dwEightBit = <value1>;
        DWORD dwTwentyFourBit = <value2>;

        DWORD dwData = (dwTwentyFourBit << 8) | dwEightBit;

        This has just been typed in, not compiled or tested, you understand. The second code snippet assumes value1 <= 255 and value2 <= 16777215.

        Server and Network Monitoring

        B Offline
        B Offline
        Ben Aldhouse
        wrote on last edited by
        #4

        Thanks very much for this. I will have a good look at this later. I'm out of time now. My programming shift is 8am to 8.30am. Have to get myself to the checkouts at the supermarket for my day job now!

        1 Reply Last reply
        0
        • B Ben Aldhouse

          Oh god, I know I'm confused. Loads of mistakes. I know. Rather than edit my previous post here are my own ammendments... If the 24 bit value are the high end bits and the 8 bit value are the low end bits... getting... 8bitVal = 32bitVal mod 2**8 24bitVal = 32bitVal / 2**8 setting... 32bitVal = 24bitVal * 2**8 + 8bitVal Still think there may be an easier way of doing this...

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

          Ben Aldhouse wrote:

          Still think there may be an easier way of doing this.

          Yes, don't go in for complex mathematical expressions, even though the compiler will optimise them. Reread the previous answer and adjust for whichever part is the 8-bit and whichever is the 24-bit. In either case just use simple shifts AND and OR operators thus:

          DWORD dwValue = (bits8 << 24) | bits24;
          // or
          DWORD dwValue = (bits24 << 8) | bits8;

          And to split

          bits8 = dwValue & 0xFF;
          bits24 = dwValue >> 8;
          // or
          bits24 = dwValue & 0xFFFFFF;
          bits8 = dwValue >> 24;

          It's time for a new signature.

          N B 2 Replies Last reply
          0
          • B Ben Aldhouse

            This is my post accidently put in the lounge... >>>> This should be pretty easy, right? I'm feeling pretty brain dead right now, though... In CTreeCtrl the nodes have a DWORD data item and I want to make use of that as a 24 bit value and an 8 bit value. What would be a nice way of getting and setting the DWORD from two other variables? Thanks, Ben. <<<< Anyway I'm thinking something like (in pseudocode...) getting... 8bitVal = DWordVal mod 2**24 32bitVal = DWordVal / 2**24 setting... DWordVal = 24bitVal * 2**24 + 8bitVal I haven't tried this so is probably wrong and I have a feeling that that there is a nicer way of doing it?

            A Offline
            A Offline
            Aescleal
            wrote on last edited by
            #6

            Another option (provided you don't want to transmit your packed 32 bit values to another machine) is to use a union with a set of bit-fields:

            union tree_ctrl_item
            {
            DWORD packed_;
            struct
            {
            DWORD bits_24_ : 24;
            DWORD bits_8_ : 8;
            }
            unpacked_;
            };

            Then you can use the packed and unpacked forms as ordinary structure members and add member functions for construction and extraction of values if you feel so inclined. There are numerous "here be dragons" caveats with this but provided you're consistent in the usage it'll be portable if not binary compatible. Cheers, Ash

            1 Reply Last reply
            0
            • L Lost User

              Ben Aldhouse wrote:

              Still think there may be an easier way of doing this.

              Yes, don't go in for complex mathematical expressions, even though the compiler will optimise them. Reread the previous answer and adjust for whichever part is the 8-bit and whichever is the 24-bit. In either case just use simple shifts AND and OR operators thus:

              DWORD dwValue = (bits8 << 24) | bits24;
              // or
              DWORD dwValue = (bits24 << 8) | bits8;

              And to split

              bits8 = dwValue & 0xFF;
              bits24 = dwValue >> 8;
              // or
              bits24 = dwValue & 0xFFFFFF;
              bits8 = dwValue >> 24;

              It's time for a new signature.

              N Offline
              N Offline
              normanS
              wrote on last edited by
              #7

              Oops - I accidentally gave you a 1 vote instead of a 5. I can't see how to undo a vote! Sorry. (OK, looks like I fixed it. I just voted again, giving 5 - that seems to have incremented the vote count but replaced my previous vote. Good.)

              L 1 Reply Last reply
              0
              • N normanS

                Oops - I accidentally gave you a 1 vote instead of a 5. I can't see how to undo a vote! Sorry. (OK, looks like I fixed it. I just voted again, giving 5 - that seems to have incremented the vote count but replaced my previous vote. Good.)

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

                Yep, looks OK now; many thanks for taking the trouble to vote.

                It's time for a new signature.

                1 Reply Last reply
                0
                • L Lost User

                  Ben Aldhouse wrote:

                  Still think there may be an easier way of doing this.

                  Yes, don't go in for complex mathematical expressions, even though the compiler will optimise them. Reread the previous answer and adjust for whichever part is the 8-bit and whichever is the 24-bit. In either case just use simple shifts AND and OR operators thus:

                  DWORD dwValue = (bits8 << 24) | bits24;
                  // or
                  DWORD dwValue = (bits24 << 8) | bits8;

                  And to split

                  bits8 = dwValue & 0xFF;
                  bits24 = dwValue >> 8;
                  // or
                  bits24 = dwValue & 0xFFFFFF;
                  bits8 = dwValue >> 24;

                  It's time for a new signature.

                  B Offline
                  B Offline
                  Ben Aldhouse
                  wrote on last edited by
                  #9

                  Thanks for this! Your reply is very much appreciated!

                  L 1 Reply Last reply
                  0
                  • E Electron Shepherd

                    How about

                    DWORD dwData = <value>;

                    DWORD dwEightBit = (dwData & 0x000000FF);
                    DWORD dwTwentyFourBit = (dwData & 0xFFFFFF00) >> 8;

                    and

                    DWORD dwEightBit = <value1>;
                    DWORD dwTwentyFourBit = <value2>;

                    DWORD dwData = (dwTwentyFourBit << 8) | dwEightBit;

                    This has just been typed in, not compiled or tested, you understand. The second code snippet assumes value1 <= 255 and value2 <= 16777215.

                    Server and Network Monitoring

                    B Offline
                    B Offline
                    Ben Aldhouse
                    wrote on last edited by
                    #10

                    This has been very helpful to me. Thanks very much. Somehow the world of bitwise operations was eluding me that morning!

                    1 Reply Last reply
                    0
                    • B Ben Aldhouse

                      Thanks for this! Your reply is very much appreciated!

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

                      You're welcome, good luck with your programs.

                      It's time for a new signature.

                      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