Menu

HIWORD with negative values

2002-12-10
2012-09-26
  • Derek Baker

    Derek Baker - 2002-12-10

    Can anyone tell me why y2 and y3 give different results, when y prior to shifting is negative (as in the values below)?

    #include <cstdlib>
    #include <iostream>

    using namespace std;

    int main()
    {
        int iVertPos = 7;
        int yCaret = 7;
        int cyChar = 13;
        
        int y;
       
        y = (yCaret- 1- iVertPos) * cyChar;
        y = y << 16;
       
        int y2 = iVertPos + (HIWORD(y)) / cyChar;
        int y3 = iVertPos + (y >> 16) / cyChar;
       
       
        cout << "y: " << y << endl;
        cout << "y2: " << y2 << endl;
        cout << "y3: " << y3 << endl;
       
        cout << endl << endl;
        system("PAUSE");

    return 0;
    }

    Thanks

    Derek

     
    • Derek Baker

      Derek Baker - 2002-12-10

      Sorted it out: dug out the answer on usenet.

      Apparently HIWORD returns an unsigned word, so obviously wouldn't handle my negative value.

      Casting to short does the trick:

      y = iVertPos + (short(HIWORD(lParam)) / cyChar);

      Derek

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.