locked
MAKELONG equivalent in c# RRS feed

  • Question

  • Is there a way to combine to short values into one long value in C#, similar to the way that the C++ MAKELONG macro does?
    Monday, September 25, 2006 1:48 PM

Answers

  • Yes, using the same shift and bitwise or operations used in the C macro.

    Here is a sample implementation:
    public int MakeLong (short lowPart, short highPart)
    {
       return (int)(((ushort)lowPart) | (uint
    )(highPart << 16));
    }

    Monday, September 25, 2006 4:25 PM
  • Try this:

     

    public int MakeLong (short lowPart, short highPart)
    {
       return (int)(((ushort)lowPart) | (uint)(highPart << 16));
    }
    public static short HiWord(int dword)
    {
        return (short) (dword >> 16);
    }
     
    public static short LoWord(int dword)
    {
        return (short) dword;
    }
    Thursday, September 28, 2006 6:54 PM

All replies

  • Yes, using the same shift and bitwise or operations used in the C macro.

    Here is a sample implementation:
    public int MakeLong (short lowPart, short highPart)
    {
       return (int)(((ushort)lowPart) | (uint
    )(highPart << 16));
    }

    Monday, September 25, 2006 4:25 PM
  • How would I break the returned int value into its two original short values?
    Thursday, September 28, 2006 5:41 PM
  • Try this:

     

    public int MakeLong (short lowPart, short highPart)
    {
       return (int)(((ushort)lowPart) | (uint)(highPart << 16));
    }
    public static short HiWord(int dword)
    {
        return (short) (dword >> 16);
    }
     
    public static short LoWord(int dword)
    {
        return (short) dword;
    }
    Thursday, September 28, 2006 6:54 PM