Why Does Bit Shifting in Win32 API and C# BitConverter.ToInt32 Act Differently?

  • Thread starter Thread starter Silicon Waffle
  • Start date Start date
  • Tags Tags
    Bit
AI Thread Summary
The discussion centers on the Win32 API macro MAKEWORD, which combines two bytes into a 16-bit integer by shifting the high byte 8 bits to the left. This behavior is rooted in the historical context of 16-bit Windows applications, where a 'WORD' represented a 16-bit integer. The confusion arises when comparing this with C# methods like BitConverter.ToInt32 and ToInt16, which handle byte arrays differently. The user notes that shifting by 16 bits works in some cases but not in others, indicating a misunderstanding of data types and casting. The critique of the user's MAKEWORD function points out that it incorrectly uses an array of integers instead of bytes or shorts, leading to potential errors in output. A suggested correction emphasizes using a byte array for proper functionality, aligning with the original MAKEWORD's intent.
Silicon Waffle
Messages
160
Reaction score
203
In Win32 API, I see the define macro MAKEWORD
#define MAKEWORD( h, l ) \
( ( ( WORD )( h ) << 8 ) | ( BYTE )( l ) )

Why does it make a shift of 8 bits ?

In C#, they have a BitConverter.ToInt32, which performs something similar to this Makeword and I use this 8 bit shift and it works correctly. But they also have ToInt16 which shifting like this works correctly too in some of my cases. I'm so confused about 32 and 16 in this case.
That is, at some places where I change makeword to make a shift of 16, it works correctly whereas at others where shifting 16 is required, the output then becomes incorrect since shifting 8 is more precise.

It looks like casting is the issue but how
PHP:
int MAKEWORD(const int*& val, const int& index)
{ 
   return (int)(((int)(val[index + 1]) << 16) + (short)(val[index]));
}
 
Last edited:
Technology news on Phys.org
The Win32API MAKEWORD is from a long time ago when Windows applications were 16-bit so a 'WORD' was a 16-bit integer (which would be considered a 'short' with modern compilers). It is taking two bytes (each 8 bits), a 'high' byte ('h') and a low byte ('l'), and shifting them into the proper places to make a 16-bit integer.

The ToInt32 function takes an array and an index and coverts the 4 bytes located from the index into a 32-bit integer. Useful for deserializing information from disk or a network.

I'm not sure what your function MAKEWORD is trying to do (and I'm not familiar with C#), but looks wrong to me because:
1. Your input 'val' is an array of integers. I think that it should be either an array of bytes or shorts.
2. You cast val[index+1] to an int but val[index] to a short.

If your goal was to make something like the MAKEWORD from Win32 API that works on byte arrays:
short MAKEWORD(const byte*& val, const int& index)
{
return ((short)val[index + 1]) << 8 + (short)(val[index]); // WARNING: I don't know C#
}
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...

Similar threads

Replies
7
Views
1K
Replies
7
Views
4K
Replies
3
Views
4K
Replies
5
Views
2K
Replies
7
Views
3K
Replies
30
Views
6K
Replies
16
Views
4K
Back
Top