unsigned subtraction

Go To Last Post
9 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

hello friends,

I am checking a condition if(0-4294966296>=1000) here 0 and 4294966296 are declared as unsigned long ...what will be the result of 0-4294966296? the if condition will satisfies or not.. pleas help me friends...  thank you very much

Last Edited: Thu. Apr 21, 2016 - 07:08 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

1000 is a signed int. 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ecworks wrote:

1000 is a signed int. 

what will the result of if condition? what will happen there? please help me sir

Last Edited: Thu. Apr 21, 2016 - 08:11 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

the subtraction will cause an underflow of course and if both numbers are 32 bit unsigned integers, the result should be 1000 and thus satisfy the the condition

"Some people die at 25 and aren't buried until 75." -Benjamin Franklin

 

What is life's greatest illusion?"  "Innocence, my brother." -Skyrim

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
#include <stdio.h>
#include <stdlib.h>

int main()
{
    if(0UL-4294966296UL>=1000)
    {
        printf("true\n");
    }
    else
    {
        printf("false\n");
    }
    return 0;
}

this prints true. Tested on win10, compiled with minGW.

 

 

"Some people die at 25 and aren't buried until 75." -Benjamin Franklin

 

What is life's greatest illusion?"  "Innocence, my brother." -Skyrim

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

pawi777 wrote:

the subtraction will cause an underflow of course and if both numbers are 32 bit unsigned integers, the result should be 1000 and thus satisfy the the condition

 ok sir thank you very much for your reply...but i don't understand how the subtraction result will be 1000.. i am more happy if you can give me little more explanation or can you give me any article or page link to refer about this more...thankful for your reply....

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

4294966269 in hex is FFFFFC18
Subtract from 0 is 0x3E8 which is 1000
A simple way is to negate and add 1
000003e7 negate
000003e8 add 1
The number effectively 'rolls around' due to the modulus of 2^32. The carry can't propogate as we ran out of digits.

So 1000 is >= 1000

You need to learn binary math.

Last Edited: Thu. Apr 21, 2016 - 08:44 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I think this image might be helpful to understand what's going on, just expand it to 32 bit  in your mind. Every addition can be understood as walking along the circle clockwise and every subtraction as walking counterclockwise. Actually, if you had 33 bits, the last bit would flip and the the result would be correct. But since we don't have it, the information is lost and the result is cut off.

"Some people die at 25 and aren't buried until 75." -Benjamin Franklin

 

What is life's greatest illusion?"  "Innocence, my brother." -Skyrim

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

https://en.wikipedia.org/wiki/Two's_complement

in this article, the issue is explained in detail. Almost all CPUs use the two's complement.

 

Edit: you're using unsigned integers. So this article doesn't really help here. But I recommend to read it nonetheless 

"Some people die at 25 and aren't buried until 75." -Benjamin Franklin

 

What is life's greatest illusion?"  "Innocence, my brother." -Skyrim

 

Last Edited: Thu. Apr 21, 2016 - 09:31 AM