Is there equivalent to HIBYTE, LOBYTE,HIWORD,LOWORD in .NET?

G

Guest

In VC++ 6.0 there were very handy macros to extract bytes from words and
words from double words: HIBYTE, LOBYTE,HIWORD,LOWORD.

Also there were macros to make words from bytes and double words from words:
MAKEWORD and MAKELONG.

I wonder whether there is something similar in .NET?
How can make the same operations (extract, make) in .NET?

Thanks
 
W

William DePalo [MVP VC++]

Steve said:
In VC++ 6.0 there were very handy macros to extract bytes from words and
words from double words: HIBYTE, LOBYTE,HIWORD,LOWORD.

Also there were macros to make words from bytes and double words from
words:
MAKEWORD and MAKELONG.

I wonder whether there is something similar in .NET?
How can make the same operations (extract, make) in .NET?

Well, the internal representations of integers have not changed. Assuming
that you target a 32 bit platform you can just lift the old definitions from
<windef.h>:

#define MAKEWORD(a, b) ((WORD)(((BYTE)((DWORD_PTR)(a) & 0xff)) |
((WORD)((BYTE)((DWORD_PTR)(b) & 0xff))) << 8))
#define MAKELONG(a, b) ((LONG)(((WORD)((DWORD_PTR)(a) & 0xffff)) |
((DWORD)((WORD)((DWORD_PTR)(b) & 0xffff))) << 16))
#define LOWORD(l) ((WORD)((DWORD_PTR)(l) & 0xffff))
#define HIWORD(l) ((WORD)((DWORD_PTR)(l) >> 16))
#define LOBYTE(w) ((BYTE)((DWORD_PTR)(w) & 0xff))
#define HIBYTE(w) ((BYTE)((DWORD_PTR)(w) >> 8))

If you cringe at the sight of such anachronisms you can make functions of
them.

Regards,
Will
 
G

Guest

OK, I don't want to "cringe at the sight of such anachronisms" ...
What is better approach in .NET to manipulate bytes and words?
 
W

William DePalo [MVP VC++]

Steve said:
OK, I don't want to "cringe at the sight of such anachronisms" ...

Sorry, I guess I shouldn't be so glib.

I just meant that many people who think hard on software design issues
consider macros to be evil.

If you are in that camp, then a function is a better choice. And marking a
function as "inline" avoids the overhead associated with function prologs
and epilogs. With that performance penalty gone, there is even less reason
to prefer a macro over a function.
What is better approach in .NET to manipulate bytes and words?

Well, I guess the first thing determine is why you need that manipulation?
If you decide you need it, then just take the code out the macro definition
and insert it into the body of a function.

Alternatively, see if the Convert class has a member to perform the
manipulation that you need

http://msdn.microsoft.com/library/d...cpref/html/frlrfsystemconvertmemberstopic.asp

Regards,
Will
 
T

Tomas Restrepo \(MVP\)

Hi Steve,
In VC++ 6.0 there were very handy macros to extract bytes from words and
words from double words: HIBYTE, LOBYTE,HIWORD,LOWORD.

There are no clear replacement for these, that I remember..
Also there were macros to make words from bytes and double words from
words:
MAKEWORD and MAKELONG.

Not exactly the same, but for many cases, you can use the BitConverter clas
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top