CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: A Question

  1. #1
    Join Date
    Nov 2004
    Posts
    104

    A Question

    Hi All,

    I have an unsigned int that I use to keep track of the frame I am in. Through out my code I need to know the difference between the current frame I am in and other frames that happened in the past.

    If the current frame was 5000 and the frame I am checking against is 4900, then that computation is easy (5000 - 4900 = 100 difference). The problem is when the frame reaches the limit of unsigned ints and loops back to 0, then I need to calculate the difference as follows. Lets say the current frame is 10, and the past frame is 4000. I cannot use 10 - 4000, because the difference is incorrect, so I have to use 10 - 4000 + UINT_MAX.

    I was wondering if anyone had any ideas how to do this in one single formula without having to check if there was a loop around (or had any other ideas)?

  2. #2
    Join Date
    Apr 2004
    Location
    England, Europe
    Posts
    2,492

    Re: A Question

    To flip the sign of unsigned integers, you normally flip the bits and add one:

    Code:
     // 5 - 3 = -2
    unsigned a = 5;
    unsigned b = 3;
    unsigned c = a + ((~b)+1);
    unsigned cc = ((~c)+1);
    I think that should work.

    Why not just use the signed int type?

    Do you really run out of numbers? Even at 1000 FPS you should have 4 million seconds, that's 46 days using a 32 bit unsigned int.

    Alternatively, does your compiler support 64 bit ints?
    Last edited by Zaccheus; November 14th, 2009 at 05:43 AM.
    My hobby projects:
    www.rclsoftware.org.uk

  3. #3
    Join Date
    Nov 2007
    Location
    Birmingham, England
    Posts
    157

    Re: A Question

    Quote Originally Posted by scathenC View Post
    I cannot use 10 - 4000, because the difference is incorrect
    Huh? Processors, When adding or subtracting should remain correct past the wrap around. They have a "carry" flag to idicate that this has happened allowing a 32bit processor to be used to add or subtract a 64bit integer.

    But hey, I might be wrong. Try the following code and then post back its result:
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main(int argc, char* argv[])
    {
    	unsigned int a = 0xFFFFFFFF; // max integer value
    	unsigned int b = a + 1000;
    
    	cout << "a is: " << a << endl
    		 << "b is: " << b << endl
    		 << "difference is: " << b-a << endl;
    
    	return 0;
    }



    Flipping the sign wont help here.
    Quote Originally Posted by Zaccheus View Post
    To flip the sign of unsigned integers, you normally flip the bits and add one:

    Code:
     // 5 - 3 = -2
    unsigned a = 5;
    unsigned b = 3;
    unsigned c = a + ((~b)+1);
    unsigned cc = ((~c)+1);
    I think that should work.
    And "two's complement" ((~x)+1) is the same as (0-x)

    Hope this helps
    Last edited by couling; November 14th, 2009 at 06:27 AM.
    Signature
    Please use: [ code ][/ code ] tags and reasonably correct tabbing. They really help us read your code
    End Signature

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured