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

  • Thread starter Silicon Waffle
  • Start date
  • Tags
    Bit
In summary: This would be equivalent to the Win32API definition, but would work with any byte array instead of just longs and shorts.
  • #1
Silicon Waffle
160
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
  • #2
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#
}
 

1. What is bit shifting in the Win32 API?

Bit shifting is a process of manipulating individual bits in a binary number. In the Win32 API, it is commonly used for efficient data storage and manipulation.

2. How do I perform a left shift in C using the Win32 API?

To perform a left shift in C using the Win32 API, you can use the "<<" operator. This operator takes two operands, the first being the number to be shifted and the second being the number of bits to shift by. For example, "5 << 2" would shift the binary number 101 (decimal 5) to the left by 2 bits, resulting in 10100 (decimal 20).

3. Can bit shifting be used for multiplication and division in the Win32 API?

Yes, bit shifting can be used for multiplication and division in the Win32 API. Left shifting a binary number by a certain number of bits is equivalent to multiplying it by 2 to the power of that number. Similarly, right shifting a binary number is equivalent to dividing it by 2 to the power of the number of bits shifted.

4. Is bit shifting a faster alternative to multiplication and division in the Win32 API?

In most cases, bit shifting is faster than multiplication and division in the Win32 API. This is because bit shifting operations are performed at the hardware level, whereas multiplication and division require more complex calculations.

5. Are there any limitations to using bit shifting in the Win32 API?

Yes, there are some limitations to using bit shifting in the Win32 API. For example, when shifting a number to the left, the bits shifted beyond the size of the data type will be lost. This can lead to unexpected results and should be taken into consideration when using bit shifting in your code.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
7
Views
832
  • Programming and Computer Science
Replies
7
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
Replies
3
Views
10K
  • Programming and Computer Science
Replies
30
Views
4K
  • Programming and Computer Science
Replies
4
Views
3K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
5
Views
5K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Programming and Computer Science
Replies
16
Views
3K
Back
Top