Does this look correct...

  • Thread starter Thread starter craig
  • Start date Start date
C

craig

I need two methods that would allow me to extract the high and low words
from in Int32. Does this look correct? Thanks!!

private Int16 HighWord(Int32 Value)

{

Int16 newValue;

newValue = (Int16)(Value >> 16);

return newValue;

}



private Int16 LowWord(Int32 Value)

{

Int16 newValue;

newValue = (Int16)(Value & 0xffff);

return newValue ;

}
 
craig said:
I need two methods that would allow me to extract the high and low words
from in Int32. Does this look correct? Thanks!!

They should operate fine, however, I would recommend using unsigned values
if possible.
Also, for readabilities sake, you could also use the C# keywords short and
int instead of Int16 and Int32.
 
Thanks for the input, Daniel.

I am curious - what is the reason for preferring unsigned inetegers over
signed integers in a situation like this? It seems to make sense, but is
there a potential for problems using signed inetegers?

Thanks!
 
craig said:
Thanks for the input, Daniel.

I am curious - what is the reason for preferring unsigned inetegers over
signed integers in a situation like this? It seems to make sense, but is
there a potential for problems using signed inetegers?

No, the outcome will remain the same. However, while bitwise operations on a
signed number are...bitwise correct, it may return an unexpected value. For
example the LowWord of a positive integer may end up being a negative short.

For the most part, however, it is more style and clarity than anything else.
By specifying unsigned you directly imply you aren't considering the
integers sign in your operation whereas signed leaves a little doubt(though
not much in this case).
 
Good explaination. Thanks again!!!

Daniel O'Connell said:
No, the outcome will remain the same. However, while bitwise operations on
a signed number are...bitwise correct, it may return an unexpected value.
For example the LowWord of a positive integer may end up being a negative
short.

For the most part, however, it is more style and clarity than anything
else. By specifying unsigned you directly imply you aren't considering the
integers sign in your operation whereas signed leaves a little
doubt(though not much in this case).
 
Back
Top