Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Integer subtraction problem, help!

Reply
Thread Tools

Integer subtraction problem, help!

 
 
bruce.james.lee@gmail.com
Guest
Posts: n/a
 
      04-03-2005
hi
i have a problem with integer subtraction in C.
printf("%d", c < (a - b));
a is got from a #define and is 0x80000000 and b is got from input and
is also 0x80000000.
c is ffffffff (-1).
Now, this should print 1 (true) but it prints 0!

If I modify this to
d = c < (a - b);
printf("%d", d);
it prints 1 correctly.

I am stumped, please help! i guess something wrong happens during the
comparison, i dont know what.
If i just retain my second method which works, is everything ok? or is
there some risk?

thanks a lot
bruce

 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      04-03-2005
wrote:
> hi
> i have a problem with integer subtraction in C.
> printf("%d", c < (a - b));
> a is got from a #define and is 0x80000000 and b is got from input and
> is also 0x80000000.
> c is ffffffff (-1).
> Now, this should print 1 (true) but it prints 0!


You seem to have fallen into the pit too many C programmers
dig for themselves: Thinking about representations rather than
values. For example, `ffffffff' is *not* -1; it is (if there's
a `0x' at the beginning) 4294967295. Since `a-b' is zero and `c'
is this large positive number, the output is as expected.

> If I modify this to
> d = c < (a - b);
> printf("%d", d);
> it prints 1 correctly.


Did you make any other modifications? Wait: don't answer!
Instead, please post an *actual* *complete* *minimal* example
of the code that baffles you. I'm trying to guess what the
#define's and variable declarations and initializations and
assignments actually looked like, and if my guesses are wide of
the mark I'll give a diagnosis that has nothing to do with your
actual problem. Show the actual problem and somebody will be
likely to help.

Consider: If you're baffled by the inexplicable behavior
of some piece of code, are you in a good position to winnow the
relevant from the irrelevant pieces? No? Then don't try; you're
quite likely (as in this instance) to leave out the important bits.

> I am stumped, please help! i guess something wrong happens during the
> comparison, i dont know what.
> If i just retain my second method which works, is everything ok? or is
> there some risk?


Impossible to say on the scanty evidence available.

--
Eric Sosman
lid

 
Reply With Quote
 
 
 
 
Martin Ambuhl
Guest
Posts: n/a
 
      04-03-2005
wrote:
> hi
> i have a problem with integer subtraction in C.
> printf("%d", c < (a - b));
> a is got from a #define and is 0x80000000 and b is got from input and
> is also 0x80000000.
> c is ffffffff (-1).
> Now, this should print 1 (true) but it prints 0!


Are you _sure_ that an int can hold such values?

The following code should produce, on most implementations, a number of
warnings. Pay attention to the warnings, and then think about what they
and the possibly strange-looking output might mean:

#include <stdio.h>

int main(void)
{
unsigned short usc = 0xffffffff, usa = 0x80000000, usb = 0x80000000;
/* the above line leads to truncation in my implementation, shorts
not being 32 bits */
unsigned long ulc = 0xffffffffL, ula = 0x80000000L, ulb =
0x80000000L;
signed short ssc = 0xffffffff, ssa = 0x80000000, ssb = 0x80000000;
/* the above line leads to truncation in my implementation, shorts
not being 32 bits */
signed long slc = 0xffffffffL, sla = 0x80000000L, slb = 0x80000000L;
printf("[Output for this implementation]\n");
printf("%d < (%d - %d)? %s\n",
ssc, ssa, ssb, (ssc < (ssa - ssb)) ? "yes" : "no");
printf("%ld < (%ld - %ld)? %s\n",
slc, sla, slb, (slc < (sla - slb)) ? "yes" : "no");
printf("%u < (%u - %u) or %x < (%x - %x)? %s\n",
usc, usa, usb,
usc, usa, usb, (usc < (usa - usb)) ? "yes" : "no");
printf("%lu < (%lu - %lu) or %lx < (%lx - %lx)? %s\n\n",
ulc, ula, ulb,
ulc, ula, ulb, (ulc < (ula - ulb)) ? "yes" : "no");

usc = 0xffff;
usa = 0x8000;
usb = 0x8000;
ulc = 0xffffL;
ula = 0x8000L;
ulb = 0x8000L;
ssc = 0xffff;
ssa = 0x8000;
ssb = 0x8000;
/* the above lines lead to truncation in my implementation, shorts
not being 32 bits */
slc = 0xffffL;
sla = 0x8000L;
slb = 0x8000L;
printf("%d < (%d - %d)? %s\n",
ssc, ssa, ssb, (ssc < (ssa - ssb)) ? "yes" : "no");
printf("%ld < (%ld - %ld)? %s\n",
slc, sla, slb, (slc < (sla - slb)) ? "yes" : "no");
printf("%u < (%u - %u) or %x < (%x - %x)? %s\n",
usc, usa, usb,
usc, usa, usb, (usc < (usa - usb)) ? "yes" : "no");
printf("%lu < (%lu - %lu) or %lx < (%lx - %lx)? %s\n\n",
ulc, ula, ulb,
ulc, ula, ulb, (ulc < (ula - ulb)) ? "yes" : "no");
return 0;
}


[Output for this implementation]
-1 < (0 - 0)? yes
-1 < (-2147483648 - -214748364? yes
65535 < (0 - 0) or ffff < (0 - 0)? no
4294967295 < (2147483648 - 214748364 or ffffffff < (80000000 -
80000000)? no

-1 < (-32768 - -3276? yes
65535 < (32768 - 3276? no
65535 < (32768 - 3276 or ffff < (8000 - 8000)? no
65535 < (32768 - 3276 or ffff < (8000 - 8000)? no
 
Reply With Quote
 
ganeshk@gmail.com
Guest
Posts: n/a
 
      04-03-2005
Eric,
Thanks a lot! I solved the problem but I still have a question.
>For example, `ffffffff' is *not* -1; it is (if there's a `0x' at the

beginning)
> 4294967295.


The problem was this:
As mentioned before, I was getting the value of 'a' from a #define. (c
and b are ok since they are got from input and are signed numbers)

#define a 0x80000000
assigned not -2147483648 but some other big positive value to 'a'.

Now, I removed the #define and put this as a statement inside my
function and it works.

But lint gives a warning saying that "initializer is out of range:
0x80000000"

How can I fix it?

Thanks
Bruce

 
Reply With Quote
 
ganeshk@gmail.com
Guest
Posts: n/a
 
      04-03-2005
Thanks a lot! I solved the problem but I still have a question.

>For example, `ffffffff' is *not* -1; it is (if there's a `0x' at the

beginning)
> 4294967295.


The problem was this:
As mentioned before, I was getting the value of 'a' from a #define. (c
and b are ok since they are got from input and are signed numbers)

#define a 0x80000000
assigned not -2147483648 but some other big positive value to 'a'.

Now, I removed the #define and put this as a statement inside my
function and it works.

But lint gives a warning saying that "initializer is out of range:
0x80000000"

How can I fix it?

Thanks
Bruce

 
Reply With Quote
 
bruce.james.lee@gmail.com
Guest
Posts: n/a
 
      04-03-2005
Thanks for the hints. I solved the problem but now I have another
problem!
In
printf("%d", c < (a - b));
'a' is got from #define as I mentioned earlier, now all that is in the
#define is treated as unsigned! So
#define a 0x800000000
assigns not -2147483648 but some big positive value. And this caused
problems in my calculation with signed numbers.
I fixed this by saying
int a = -2147483648 ; inside my function.
but C gives a warningsaying it is out of range. I searched for this new
problem, know why it occurs, but what would be the best solution?

 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      04-03-2005
wrote:
> Eric,
> Thanks a lot! I solved the problem but I still have a question. [...]


... and you have failed to pay any heed to a crucial
part of my earlier response, to wit

>> [...] please post an *actual* *complete* *minimal* example
>> of the code that baffles you. [...]


> How can I fix it?


My crystal ball says your error is on line 42.

--
Eric Sosman
lid
 
Reply With Quote
 
bruce.james.lee@gmail.com
Guest
Posts: n/a
 
      04-03-2005
Eric,
The reason I did not heed your suggestion is simple, the problem got
solved but turned into another. which is just

"int a = 0x80000000;"
gives a warning since a is -2147483648 which is split as an operator
and a positive number by C.

How to remove this warning? (a = -2147483647 - 1 looks dirty)


Eric Sosman wrote:
> wrote:
> > Eric,
> > Thanks a lot! I solved the problem but I still have a question.

[...]
>
> ... and you have failed to pay any heed to a crucial
> part of my earlier response, to wit
>
> >> [...] please post an *actual* *complete* *minimal* example
> >> of the code that baffles you. [...]

>
> > How can I fix it?

>
> My crystal ball says your error is on line 42.
>
> --
> Eric Sosman
> lid


 
Reply With Quote
 
A. Sinan Unur
Guest
Posts: n/a
 
      04-03-2005
wrote in news:1112567684.942695.52570
@f14g2000cwb.googlegroups.com:

Eric Sosman wrote:

>> >> [...] please post an *actual* *complete* *minimal* example
>> >> of the code that baffles you. [...]

....
>> ... and you have failed to pay any heed to a crucial
>> part of my earlier response, to wit


> Eric,
> The reason I did not heed your suggestion is simple, the problem got
> solved but turned into another. which is just


How does that eliminate the importance of posting that satisfies the the
three important criteria listed above???

> "int a = 0x80000000;"


#include <stdio.h>
#include <limits.h>

int main(void) {
printf("The maximum possible value for an int is: %d\n", INT_MAX);
printf("The minimum possible value for an int is: %d\n", INT_MIN);
return 0;
}

The value you are assigning to a, 0x80000000 is an unsigned literal
whose value does not fit in a signed int.

If you want to assign -1 to a, then you should do exactly that:

int a = -1;

Now, that is not so hard, is it?

Sinan
--
A. Sinan Unur <>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/cl...uidelines.html
 
Reply With Quote
 
bruce.james.lee@gmail.com
Guest
Posts: n/a
 
      04-03-2005
Sinan,
I dont think u got the problem. The value I want in 'a' is -2147483648,
not -1.

int a = -2147483648;
will generate a warning.

int a = 0x80000000;
will also generate a warning.

how to eliminate the warning, is my query.

A. Sinan Unur wrote:
> wrote in news:1112567684.942695.52570
> @f14g2000cwb.googlegroups.com:
>
> Eric Sosman wrote:
>
> >> >> [...] please post an *actual* *complete* *minimal* example
> >> >> of the code that baffles you. [...]

> ...
> >> ... and you have failed to pay any heed to a crucial
> >> part of my earlier response, to wit

>
> > Eric,
> > The reason I did not heed your suggestion is simple, the problem

got
> > solved but turned into another. which is just

>
> How does that eliminate the importance of posting that satisfies the

the
> three important criteria listed above???
>
> > "int a = 0x80000000;"

>
> #include <stdio.h>
> #include <limits.h>
>
> int main(void) {
> printf("The maximum possible value for an int is: %d\n",

INT_MAX);
> printf("The minimum possible value for an int is: %d\n",

INT_MIN);
> return 0;
> }
>
> The value you are assigning to a, 0x80000000 is an unsigned literal
> whose value does not fit in a signed int.
>
> If you want to assign -1 to a, then you should do exactly that:
>
> int a = -1;
>
> Now, that is not so hard, is it?
>
> Sinan
> --
> A. Sinan Unur <>
> (reverse each component and remove .invalid for email address)
>
> comp.lang.perl.misc guidelines on the WWW:
> http://mail.augustmail.com/~tadmc/cl...uidelines.html


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What's wrong in this VHDL subtraction? mweb@inwind.it VHDL 3 01-18-2006 09:42 PM
Integer subtraction problem, help! bruce.james.lee@gmail.com C Programming 5 04-04-2005 10:19 PM
Overloaded subtraction operator - memory problem August1 C++ 4 09-21-2004 12:57 AM
Pointer subtraction Kevin C. C Programming 5 05-08-2004 05:31 AM
unsigned short addition/subtraction overflow Andy C Programming 34 01-01-2004 01:57 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57