Spliiting a DWORD
-
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?
-
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?
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 andvalue2
<= 16777215. -
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?
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...
-
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 andvalue2
<= 16777215.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!
-
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...
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.
-
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?
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
-
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.
-
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.)
-
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.
Thanks for this! Your reply is very much appreciated!
-
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 andvalue2
<= 16777215.This has been very helpful to me. Thanks very much. Somehow the world of bitwise operations was eluding me that morning!
-
Thanks for this! Your reply is very much appreciated!