Dude
Dude

Reputation: 39

Bitshifting long int returns wrong data

I'm trying to return two 32 bit int values (_mouseX and _mouseY) from a function by packing them into a 64 bit value first, then unpacking them elsewhere in the code when I use them. However, the data is completely incorrect when I get it through this function. I already confirmed that the original _mouseX and _mouseY data and the SetOrientation functions are fine; the problem is somewhere in the bitshifting or maybe casting or teh types Im using in this GetMouseXY function. Could you please have a look? Thanks.

long int Input::GetMouseXY()
{
    return (_mouseX << 32) |_mouseY;
}

long int mouseY = pInput->GetMouseXY() & 0xFFFFFFFF;
long int mouseX = (pInput->GetMouseXY() >> 32) & 0xFFFFFFFF;
_CamPosition->SetOrientation(XMFLOAT3((float)mouseY, (float)mouseX, 0.f));

Upvotes: 0

Views: 62

Answers (1)

geza
geza

Reputation: 29962

Use (long int)_mouseX<<32|_mouseY, because you need to cast to a 64-bit type before shifting by 32.

Two notes: First, use std::uint64_t, if you need a 64-bit type. Second: why don't you use a struct for this? Like

struct MouseCoord {
    uint32_t x, y;
};

MouseCoord Input::GetMouseXY();

It's much more clear to see, what's going on.

Upvotes: 1

Related Questions