• Advertisement

C++ Bit manipulation returning wrong data

Recommended Posts

Hi, 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));

Share this post


Link to post
Share on other sites
Advertisement

Are the original _mouseX and _mouseY int's? They should be casted to 64-bit numbers before you are able to correctly shift them 32 bits.

Share this post


Link to post
Share on other sites
32 minutes ago, DaBono said:

Are the original _mouseX and _mouseY int's? They should be casted to 64-bit numbers before you are able to correctly shift them 32 bits.

I tried casting them and the data is still wrong.

 

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

Share this post


Link to post
Share on other sites

The mouse (in Windows) is given as a 16 bit signed value, so just use <<16, >>16, and 0xFFFF.

Also, a long int is a 32 bit value, not 64.

Share this post


Link to post
Share on other sites
15 minutes ago, mister345 said:

I tried casting them and the data is still wrong.

 

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

I believe "long int" is still 32bits in Windows. Try using "long long" or (u)int64_t

Share this post


Link to post
Share on other sites
22 minutes ago, Mk_ said:

The mouse (in Windows) is given as a 16 bit signed value, so just use <<16, >>16, and 0xFFFF.

Also, a long int is a 32 bit value, not 64.

tried your theory about 16 bits but still doesnt work. Should it be an unsigned int or not? Couldnt the mouse x or y values be negative? Also, if it should be an unsigned int, maybe it needs to be stored inside a 64 int anyway bc a 32 bit int could get messed up by the sign bit? Just guessing now.

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

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

 

Share this post


Link to post
Share on other sites

Check the definition of _mouseX & Y. Windows desktop uses SIGNED 16 bit values for the mouse, but maybe they're being converted to int by whatever library you're using. If you go the 64bit router use __int64.

Share this post


Link to post
Share on other sites

It's not clear how you get mouse input, but if you use the windows mouse messages (WM_MOUSEMOVE, etc) then the values received there are (signed) 16bit as Mk_ stated.

If bit manipulation is giving you trouble, then you can use unions as an alternative.

Share this post


Link to post
Share on other sites
2 hours ago, mister345 said:

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

As others have stated, that is probably not a 64bit result and you are loosing the x position, change the code as follows to guarantee things:

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

Alternatively, and more difficult to get wrong or mess up later, use a combination of a union and bit fields:

union MousePosition
{
  int64_t Data;
  struct
  {
    int32_t X : 32;
    int32_t Y : 32;
  } Position;
};

Then you can:

int64_t Input::GetMouseXY()
    {
       MousePosition result;
       result.Position.X = _mouseX;
       result.Position.Y = _mouseY;
       return result.Data;
    }

The bit field makes the code more verbose but also more understandable.  It will do the magic of bit shifting and compositing the result for you automatically in a safe manner.  (Note that in the above case, if you really want 32 bit values, you don't need the bitfield, I put it in there since I usually pack more data into that structure.)  Unpacking the data is the following:

MousePosition pos;
pos.Data = theInt64ReturnedAbove;
int32_t x = pos.Position.X;
int32_t y = pos.Position.Y;

 

Share this post


Link to post
Share on other sites
43 minutes ago, All8Up said:

As others have stated, that is probably not a 64bit result and you are loosing the x position, change the code as follows to guarantee things:

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

Alternatively, and more difficult to get wrong or mess up later, use a combination of a union and bit fields:

union MousePosition
{
  int64_t Data;
  struct
  {
    int32_t X : 32;
    int32_t Y : 32;
  } Position;
};

Then you can:

int64_t Input::GetMouseXY()
    {
       MousePosition result;
       result.Position.X = _mouseX;
       result.Position.Y = _mouseY;
       return result.Data;
    }

The bit field makes the code more verbose but also more understandable.  It will do the magic of bit shifting and compositing the result for you automatically in a safe manner.  (Note that in the above case, if you really want 32 bit values, you don't need the bitfield, I put it in there since I usually pack more data into that structure.)  Unpacking the data is the following:

MousePosition pos;
pos.Data = theInt64ReturnedAbove;
int32_t x = pos.Position.X;
int32_t y = pos.Position.Y;

 

Very interesting. I tried your way of using int64_t

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

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

and both x and y rotation start out locked at a 10 digit number around -2100000000, but if you keep looking downwards and do a "somersault" with the mouse so you end up with a pitch of over 360, then do the same "winding up over 360" with the yaw, then suddenly the mouse look works perfectly for both x and y. So for some reason this implementation makes values between 0 and 360 invalid. (Notice I also had to reverse X and Y when passing them into SetOrientation().

Absolutely bizarre. Anyway, the code is here if anyone's curious.

https://github.com/mister51213/DirectX11Engine/blob/Restructure/DirectX11Engine/Input.cpp

https://github.com/mister51213/DirectX11Engine/blob/Restructure/DirectX11Engine/World.cpp

Share this post


Link to post
Share on other sites
10 hours ago, ApochPiQ said:

Post the code for SetOrientation (or a link to the API docs if it isn't your code).

I posted the SetOrientation code further down in my reply, but I dont think the issue is there. What you should really be looking at is the process input function, how it actually gets the mouse X and Y values:

void Input::ProcessInput()
{
    // Update the location of the mouse cursor based on the change of the mouse location during the frame.
    _mouseX += _mouseState.lX;
    _mouseY += _mouseState.lY;

}

The function to read the mouse is:

bool Input::ReadMouse()

    HRESULT result = _mouse->GetDeviceState(sizeof(DIMOUSESTATE), (LPVOID)&_mouseState);
    if (FAILED(result))
    {
        // If the mouse lost focus or was not acquired then try to get control back.
        if ((result == DIERR_INPUTLOST) || (result == DIERR_NOTACQUIRED))
            _mouse->Acquire();
        else
            return false;
    }

}

This DIMOUSESTATE is inside dinput.h, and the mouse device is a IDirectInputDevice8* _mouse, so the API is DirectInput8, to answer your question.

`````````````````````````````````````````````````````````````

[ANSWER TO YOUR INITIAL QUESTION:]

``````````````````````````````````````````````````````````````

void PositionClass::SetOrientation(XMFLOAT3& newOrientation)
{    _orientation = newOrientation;    }

Elsewhere in the code, inside Camera.cpp it creates a view matrix from this orientation, which is used to render the entire scene. So it constructs a XMMatrixRotationRollPitchYaw from the X, Y, and Z values converted to radians, then transforms the forward and up vectors (0,0,1) and (0,1,0) using this orientation matrix, then passes them into the DirectX MXMatrixLookatFunction to construct the view matrix, which is used for rendering. Result being a first person camera with mouse look.

   rotationMatrix = DirectX::XMMatrixRotationRollPitchYaw(_rotationX* 0.0174532925f, _rotationY * 0.0174532925f, _rotationZ * 0.0174532925f);

``````````````````````````````````````````````````````````````

    _viewMatrix = DirectX::XMMatrixLookAtLH(positionVector, lookAtVector, upVector);

Share this post


Link to post
Share on other sites

Please don't tell people what we "should" be doing. I'm voluntarily offering you help with YOUR bug, for free, on my own time. Feel free to reject that offer if you like, but I suspect you posted here to *get* help, not reject it ;-)

My hunch is that your conversion from mouse coordinates to camera transform is actually broken. Since your description of that implementation is vague, I can't confirm or disprove that hunch yet.

 

In any event the burden of proof is on you and nobody else. If you can convince us that your camera transform construction is correct, I'm sure we can narrow down the actual cause of the behavior you're observing.

 

Share this post


Link to post
Share on other sites
39 minutes ago, ApochPiQ said:

Please don't tell people what we "should" be doing. I'm voluntarily offering you help with YOUR bug, for free, on my own time. Feel free to reject that offer if you like, but I suspect you posted here to *get* help, not reject it ;-)

My hunch is that your conversion from mouse coordinates to camera transform is actually broken. Since your description of that implementation is vague, I can't confirm or disprove that hunch yet.

 

In any event the burden of proof is on you and nobody else. If you can convince us that your camera transform construction is correct, I'm sure we can narrow down the actual cause of the behavior you're observing.

 

I'm very sorry for offending you, I didn't mean to. Please accept my apologies and thanks again for all your help. I meant to suggest that the problem was probably in the original function I posted, rather than in SetOrientation(), because when doing it this way, it works fine:

    int mouseX = 0.f; int mouseY = 0.f;
    pInput->GetMouseLocation(mouseX, mouseY);

    _CamPosition->SetOrientation(XMFLOAT3((float)mouseX, (float)mouseY, 0.f));
 

Share this post


Link to post
Share on other sites

I see. That would have been good information for the original post, but oh well - next time :-)

 

Reading your code from GitHub I think you are probably running into a signed/unsigned issue. If _mouseX is an int, casting it to __int64 will sign-extend it. And then shifting by 32 bits may yield undefined behavior, if the value is negative.

Try something like this:

uint64_t PackXY (int x, int y)
{
    uint64_t wideX = static_cast<uint64_t>(*reinterpret_cast<uint32_t *>(&x));
    uint64_t wideY = static_cast<uint64_t>(*reinterpret_cast<uint32_t *>(&y));
    
    return ((wideX & 0xffffffff) << 32) | (wideY & 0xffffffff);
}
      
// Unpack
uint32_t x = packed >> 32;
uint32_t y = packed & 0xffffffff;
      
float fx = static_cast<float>(*reinterpret_cast<int *>(&x));
float fy = static_cast<float>(*reinterpret_cast<int *>(&y));

 

Share this post


Link to post
Share on other sites
31 minutes ago, ApochPiQ said:

I see. That would have been good information for the original post, but oh well - next time :-)

 

Reading your code from GitHub I think you are probably running into a signed/unsigned issue. If _mouseX is an int, casting it to __int64 will sign-extend it. And then shifting by 32 bits may yield undefined behavior, if the value is negative.

Try something like this:


uint64_t PackXY (int x, int y)
{
    uint64_t wideX = static_cast<uint64_t>(*reinterpret_cast<uint32_t *>(&x));
    uint64_t wideY = static_cast<uint64_t>(*reinterpret_cast<uint32_t *>(&y));
    
    return ((wideX & 0xffffffff) << 32) | (wideY & 0xffffffff);
}
      
// Unpack
uint32_t x = packed >> 32;
uint32_t y = packed & 0xffffffff;
      
float fx = static_cast<float>(*reinterpret_cast<int *>(&x));
float fy = static_cast<float>(*reinterpret_cast<int *>(&y));

 

You are a genius! It works now using your method! So I guess it was somehow packing an already signed value into another SIGNED container, thereby messing up the info. An unsigned container is a more reliable container for info that will be bitshifted? Thats my intuitive understanding. Thanks again!

Share this post


Link to post
Share on other sites

The operations simply behave differently depending on whether or not the data type is signed. The data itself is just bits in memory, in any case.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


  • Advertisement
  • Advertisement
  • Popular Tags

  • Advertisement
  • Popular Now

  • Similar Content

    • By francoisdiy
      So I wrote a programming language called C-Lesh to program games for my game maker Platformisis. It is a scripting language which tiles into the JavaScript game engine via a memory mapper using memory mapped I/O. Currently, I am porting the language as a standalone interpreter to be able to run on the PC and possibly other devices excluding the phone. The interpreter is being written in C++ so for those of you who are C++ fans you can see the different components implemented. Some background of the language and how to program in C-Lesh can be found here:

      http://www.codeloader.net/readme.html
      As I program this thing I will post code from different components and explain.
    • By isu diss
      I'm trying to duplicate vertices using std::map to be used in a vertex buffer. I don't get the correct index buffer(myInds) or vertex buffer(myVerts). I can get the index array from FBX but it differs from what I get in the following std::map code. Any help is much appreciated.
      struct FBXVTX { XMFLOAT3 Position; XMFLOAT2 TextureCoord; XMFLOAT3 Normal; }; std::map< FBXVTX, int > myVertsMap; std::vector<FBXVTX> myVerts; std::vector<int> myInds; HRESULT FBXLoader::Open(HWND hWnd, char* Filename, bool UsePositionOnly) { HRESULT hr = S_OK; if (FBXM) { FBXIOS = FbxIOSettings::Create(FBXM, IOSROOT); FBXM->SetIOSettings(FBXIOS); FBXI = FbxImporter::Create(FBXM, ""); if (!(FBXI->Initialize(Filename, -1, FBXIOS))) { hr = E_FAIL; MessageBox(hWnd, (wchar_t*)FBXI->GetStatus().GetErrorString(), TEXT("ALM"), MB_OK); } FBXS = FbxScene::Create(FBXM, "REALMS"); if (!FBXS) { hr = E_FAIL; MessageBox(hWnd, TEXT("Failed to create the scene"), TEXT("ALM"), MB_OK); } if (!(FBXI->Import(FBXS))) { hr = E_FAIL; MessageBox(hWnd, TEXT("Failed to import fbx file content into the scene"), TEXT("ALM"), MB_OK); } FbxAxisSystem OurAxisSystem = FbxAxisSystem::DirectX; FbxAxisSystem SceneAxisSystem = FBXS->GetGlobalSettings().GetAxisSystem(); if(SceneAxisSystem != OurAxisSystem) { FbxAxisSystem::DirectX.ConvertScene(FBXS); } FbxSystemUnit SceneSystemUnit = FBXS->GetGlobalSettings().GetSystemUnit(); if( SceneSystemUnit.GetScaleFactor() != 1.0 ) { FbxSystemUnit::cm.ConvertScene( FBXS ); } if (FBXI) FBXI->Destroy(); FbxNode* MainNode = FBXS->GetRootNode(); int NumKids = MainNode->GetChildCount(); FbxNode* ChildNode = NULL; for (int i=0; i<NumKids; i++) { ChildNode = MainNode->GetChild(i); FbxNodeAttribute* NodeAttribute = ChildNode->GetNodeAttribute(); if (NodeAttribute->GetAttributeType() == FbxNodeAttribute::eMesh) { FbxMesh* Mesh = ChildNode->GetMesh(); if (UsePositionOnly) { NumVertices = Mesh->GetControlPointsCount();//number of vertices MyV = new XMFLOAT3[NumVertices]; for (DWORD j = 0; j < NumVertices; j++) { FbxVector4 Vertex = Mesh->GetControlPointAt(j);//Gets the control point at the specified index. MyV[j] = XMFLOAT3((float)Vertex.mData[0], (float)Vertex.mData[1], (float)Vertex.mData[2]); } NumIndices = Mesh->GetPolygonVertexCount();//number of indices MyI = (DWORD*)Mesh->GetPolygonVertices();//index array } else { FbxLayerElementArrayTemplate<FbxVector2>* uvVertices = NULL; Mesh->GetTextureUV(&uvVertices); int idx = 0; for (int i = 0; i < Mesh->GetPolygonCount(); i++)//polygon(=mostly triangle) count { for (int j = 0; j < Mesh->GetPolygonSize(i); j++)//retrieves number of vertices in a polygon { FBXVTX myVert; int p_index = 3*i+j; int t_index = Mesh->GetTextureUVIndex(i, j); FbxVector4 Vertex = Mesh->GetControlPointAt(p_index);//Gets the control point at the specified index. myVert.Position = XMFLOAT3((float)Vertex.mData[0], (float)Vertex.mData[1], (float)Vertex.mData[2]); FbxVector4 Normal; Mesh->GetPolygonVertexNormal(i, j, Normal); myVert.Normal = XMFLOAT3((float)Normal.mData[0], (float)Normal.mData[1], (float)Normal.mData[2]); FbxVector2 uv = uvVertices->GetAt(t_index); myVert.TextureCoord = XMFLOAT2((float)uv.mData[0], (float)uv.mData[1]); if ( myVertsMap.find( myVert ) != myVertsMap.end() ) myInds.push_back( myVertsMap[ myVert ]); else { myVertsMap.insert( std::pair<FBXVTX, int> (myVert, idx ) ); myVerts.push_back(myVert); myInds.push_back(idx); idx++; } } } } } } } else { hr = E_FAIL; MessageBox(hWnd, TEXT("Failed to create the FBX Manager"), TEXT("ALM"), MB_OK); } return hr; } bool operator < ( const FBXVTX &lValue, const FBXVTX &rValue) { if (lValue.Position.x != rValue.Position.x) return(lValue.Position.x < rValue.Position.x); if (lValue.Position.y != rValue.Position.y) return(lValue.Position.y < rValue.Position.y); if (lValue.Position.z != rValue.Position.z) return(lValue.Position.z < rValue.Position.z); if (lValue.TextureCoord.x != rValue.TextureCoord.x) return(lValue.TextureCoord.x < rValue.TextureCoord.x); if (lValue.TextureCoord.y != rValue.TextureCoord.y) return(lValue.TextureCoord.y < rValue.TextureCoord.y); if (lValue.Normal.x != rValue.Normal.x) return(lValue.Normal.x < rValue.Normal.x); if (lValue.Normal.y != rValue.Normal.y) return(lValue.Normal.y < rValue.Normal.y); return(lValue.Normal.z < rValue.Normal.z); }  
    • By Karol Plewa
      Hi, 
       
      I am working on a project where I'm trying to use Forward Plus Rendering on point lights. I have a simple reflective scene with many point lights moving around it. I am using effects file (.fx) to keep my shaders in one place. I am having a problem with Compute Shader code. I cannot get it to work properly and calculate the tiles and lighting properly. 
       
      Is there anyone that is wishing to help me set up my compute shader?
      Thank you in advance for any replies and interest!
    • By fishyperil
      I'm looking for some references that could help me learn how to program some really basic 2D enemy behaviours.
      I wasn't sure whether to post this here or in the AI section but I think it might be more suitable to be posted here since it has more to do with basic maths than any AI related algorithms.
      Could anyone help recommend some resources (books, posts, videos) that could help me understand how to properly implement the basics of enemy movement in 2d games ? So far I've only managed to get them to chase the player character and to stop moving on collision, but the movement is pretty unrealistic and once the collision occurs the enemies all "pile up" on the player character. I'm doing this in C++ so no guides that explain how to script this using an engine api please.
    • By LifeArtist
      Good Evening,
      I want to make a 2D game which involves displaying some debug information. Especially for collision, enemy sights and so on ...
      First of I was thinking about all those shapes which I need will need for debugging purposes: circles, rectangles, lines, polygons.
      I am really stucked right now because of the fundamental question:
      Where do I store my vertices positions for each line (object)? Currently I am not using a model matrix because I am using orthographic projection and set the final position within the VBO. That means that if I add a new line I would have to expand the "points" array and re-upload (recall glBufferData) it every time. The other method would be to use a model matrix and a fixed vbo for a line but it would be also messy to exactly create a line from (0,0) to (100,20) calculating the rotation and scale to make it fit.
      If I proceed with option 1 "updating the array each frame" I was thinking of having 4 draw calls every frame for the lines vao, polygons vao and so on. 
      In addition to that I am planning to use some sort of ECS based architecture. So the other question would be:
      Should I treat those debug objects as entities/components?
      For me it would make sense to treat them as entities but that's creates a new issue with the previous array approach because it would have for example a transform and render component. A special render component for debug objects (no texture etc) ... For me the transform component is also just a matrix but how would I then define a line?
      Treating them as components would'nt be a good idea in my eyes because then I would always need an entity. Well entity is just an id !? So maybe its a component?
      Regards,
      LifeArtist
  • Advertisement