Thread: How to check 32 bit limit

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    136

    Question How to check 32 bit limit

    Hi all,
    can u help me how to find if the sum of two numbers X and Y would cross the 32 bit limit. My development system has all registers of 32 bits and checking them as:

    if (X+Y) > 0xFFFFFFFF

    doesn't help because the system then has to add the numbers for checking and if it did so, in case the sum is over 32 bits, the counter will roll over zero.
    S_ccess is waiting for u. Go Ahead, put u there.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    if( 0xFFFFFFFF - X >= Y )
        it will fit!
    else
        it won't!
    Subtract one from the maximum. Compare that value to the other.

    [edit]
    *forgot to allow for a perfect fit. (ie MAX is 10, X and Y are 5)
    [/edit]

    [edit2]
    Or if we prefer just the simple check, instead of the ugly >=
    Code:
    if( Y > 0xFFFFFFFF - X )
        it won't fit!
    else
        it will fit!
    [/edit2]


    Quzah.
    Last edited by quzah; 09-28-2006 at 02:02 AM. Reason: I'm an edit freak.
    Hope is the first step on the road to disappointment.

  3. #3
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Location
    Melbourne, Australia
    Posts
    210
    I suppose you could do this:
    Code:
    z = x + y;
    
    if ( z - x != y )
    it crossed the limit;

    I have no idea if that would work, but it's worth a try.


    [EDIT]

    Hahaha, quzah's is heaps better than mine - use that one. :P
    it's ironic considerate rarity patron of love higher knowledge engulfs me...

  4. #4
    Registered User
    Join Date
    Dec 2005
    Posts
    136

    Smile

    yeah... i think it will work...
    Thanks buddy.
    S_ccess is waiting for u. Go Ahead, put u there.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. MFC check box question
    By MyglyMP2 in forum Windows Programming
    Replies: 2
    Last Post: 03-09-2009, 05:47 PM
  2. convert 32 bit number to array of digits
    By droseman in forum C Programming
    Replies: 11
    Last Post: 02-18-2009, 09:37 AM
  3. creating an array of rects
    By a1dutch in forum C++ Programming
    Replies: 8
    Last Post: 03-07-2006, 06:15 PM
  4. adding 16bit and 32 bit integers
    By kris_perry2k in forum C Programming
    Replies: 2
    Last Post: 12-08-2005, 09:49 AM
  5. bit patterns of negtive numbers?
    By chunlee in forum C Programming
    Replies: 4
    Last Post: 11-08-2004, 08:20 AM