Convert DWORD value
-
Before I start, can I just say ARRRRRRRRRGHHHHHHHHHHHH! Sorry about that, this has been doing my head in..... I'm using a 3rd party COM object which they tell me returns "a DWORD defined as an unsigned long of four bytes". According to MSDN, the correct way to represent a DWORD under .Net is as a Unit32 which is not CLS-compliant. See this: http://msdn2.microsoft.com/en-us/library/ac7ay120.aspx In one part of thier API, this value is split into 2 values. I am told that... "The first 16 bits of the long becomes the first short, and the remaining 16 bits become the second short." I'm not quite sure how to achieve the above. They have used the operators >> and & in thier sample code. Having looked at their details on MSDN I am still n the dark. I have addapted a sample and arrived at the following:
ushort argument1 =(ushort)(returnValue & 0xffff); ushort argument2 =(ushort)(returnValue >> 16);
I am unsure if this is doing what I need, if so perhaps somone could explain what is happenning. The only other idea I had was to convert the value into bits and then perform the required manipulation. Any guidance would be much appreciated.
Regards Wayne Phipps ____________ Time is the greatest teacher... unfortunately, it kills all of its students View my Blog
-
Before I start, can I just say ARRRRRRRRRGHHHHHHHHHHHH! Sorry about that, this has been doing my head in..... I'm using a 3rd party COM object which they tell me returns "a DWORD defined as an unsigned long of four bytes". According to MSDN, the correct way to represent a DWORD under .Net is as a Unit32 which is not CLS-compliant. See this: http://msdn2.microsoft.com/en-us/library/ac7ay120.aspx In one part of thier API, this value is split into 2 values. I am told that... "The first 16 bits of the long becomes the first short, and the remaining 16 bits become the second short." I'm not quite sure how to achieve the above. They have used the operators >> and & in thier sample code. Having looked at their details on MSDN I am still n the dark. I have addapted a sample and arrived at the following:
ushort argument1 =(ushort)(returnValue & 0xffff); ushort argument2 =(ushort)(returnValue >> 16);
I am unsure if this is doing what I need, if so perhaps somone could explain what is happenning. The only other idea I had was to convert the value into bits and then perform the required manipulation. Any guidance would be much appreciated.
Regards Wayne Phipps ____________ Time is the greatest teacher... unfortunately, it kills all of its students View my Blog
ushort argument1 =(ushort)(returnValue & 0xffff);
Returns the lower 16 bits more easily seen when written asushort argument1 =(ushort)(returnValue & 0x0000ffff);
The next statement shifts the bits to the right 16 bits. thus 0xffaaffgg>>16 would then become 0x0000ffaa which is then already masked for you.
File Not Found
-
ushort argument1 =(ushort)(returnValue & 0xffff);
Returns the lower 16 bits more easily seen when written asushort argument1 =(ushort)(returnValue & 0x0000ffff);
The next statement shifts the bits to the right 16 bits. thus 0xffaaffgg>>16 would then become 0x0000ffaa which is then already masked for you.
File Not Found
Although I don't really understand it, I thank you for your reply. From your description however, it appears that it will do just what I need. I apologise for my ignorance, I even looked up the operator on MSDN: http://msdn2.microsoft.com/en-us/library/sbf85k1c.aspx It reads.....
_The & operator can function as either a unary or a binary operator.
The unary & operator returns the address of its operand (requires unsafe context).
Binary & operators are predefined for the integral types and bool. For integral types, & computes the logical bitwise AND of its operands.
For bool operands, & computes the logical AND of its operands; that is, the result is true if and only if both its operands are true._:wtf: I read that before my original post and unfortunately it makes no more sense to me after reading your reply.
Regards Wayne Phipps ____________ Time is the greatest teacher... unfortunately, it kills all of its students View my Blog
-
Although I don't really understand it, I thank you for your reply. From your description however, it appears that it will do just what I need. I apologise for my ignorance, I even looked up the operator on MSDN: http://msdn2.microsoft.com/en-us/library/sbf85k1c.aspx It reads.....
_The & operator can function as either a unary or a binary operator.
The unary & operator returns the address of its operand (requires unsafe context).
Binary & operators are predefined for the integral types and bool. For integral types, & computes the logical bitwise AND of its operands.
For bool operands, & computes the logical AND of its operands; that is, the result is true if and only if both its operands are true._:wtf: I read that before my original post and unfortunately it makes no more sense to me after reading your reply.
Regards Wayne Phipps ____________ Time is the greatest teacher... unfortunately, it kills all of its students View my Blog
and Discrete Math go into the topics at depth. Basically 0xF is the hex representation of 1111 in binary (15 in decimal). 1111b (b for binary) << 4 = 11110000b which is 0xF0 in hex. 0xF0 | 0x0F = 0xFF (bitwise or), 0xF0 & 0xF0 = 0x00 (bitwise and). Also 0x0f & 0x01 = 0x01 (bitwise and) I really can't go into it further because it can get fairly immense.
File Not Found
-
and Discrete Math go into the topics at depth. Basically 0xF is the hex representation of 1111 in binary (15 in decimal). 1111b (b for binary) << 4 = 11110000b which is 0xF0 in hex. 0xF0 | 0x0F = 0xFF (bitwise or), 0xF0 & 0xF0 = 0x00 (bitwise and). Also 0x0f & 0x01 = 0x01 (bitwise and) I really can't go into it further because it can get fairly immense.
File Not Found
Thats a great intro, I am familiar with hex but didn't understand how the operator worked. I will read up on Boolean Algebra and Discrete Math as you suggest. I would like to know more but understand that this is beyond the forum. Thanks for your guidance.
Regards Wayne Phipps ____________ Time is the greatest teacher... unfortunately, it kills all of its students View my Blog