Re: efficiency concern: when to really use unsigned ints and when not to

From: CBFalconer (cbfalconer_at_yahoo.com)
Date: 02/16/04


Date: Mon, 16 Feb 2004 20:12:59 GMT

anony*mouse wrote:
>
... snip ...
>
> As an exercise consider the additional checks that need to be
> taken in the check() function below. How many would be removed
> by making a and b unsigned?
>
> int check(signed int a, signed int b)
> {
> /* a and b are considered untrusted numbers. */
>
          if ((a < 0) || (b < 0)) return 0;
> if (a + b < 50)
> return 1;
> else
> return 0;
> }

The above suffices for any system which has integer overflow
detection. Now consider the ugly tests required if a and b are
unsigned, so that overflow of a + b cannot occur.

-- 
Chuck F (cbfalconer@yahoo.com) (cbfalconer@worldnet.att.net)
   Available for consulting/temporary embedded and systems.
   <http://cbfalconer.home.att.net>  USE worldnet address!


Relevant Pages

  • Re: Haldanes Dilemma - clarifications - and Felsenstein [LONG]
    ... This sounds like a consequence of the theory of 'nearly neutral' selection. ... We take two at random, and return the one with the highest fitness, ... int choose ...
    (sci.bio.evolution)
  • Re: c interview
    ... undefined behaviour because printf is a varidac function. ... stdio.h before using printf and other headers as appropriate before ... Would make it a point that the size of int is assumption to be .... ...
    (comp.lang.c)
  • Re: || putchar(ch == 177 ? ? : ch | 0100) == EOF)
    ... snip 160 lines of obsolete commentary ... void filecopy(FILE *ifp, FILE *ofp) ... int ch; ... Since you expect the file to contain control character, ...
    (comp.lang.c)
  • Re: Add Method
    ... Do you mean to convert the number to a string? ... My guess is that this is an exercise for a 1st-year computer science course ... and it means to convert positive number (probably in an int) into a String ...
    (comp.lang.java.help)
  • Re: Direct computation of integer limits in K&R2?
    ... The original exercise text as provided by Richard Heathfield is: ... Write a program to determine the ranges of char, short, int, and long, both signed and unsigned, by printing appropriate values from standard headers and by direct computation. ... int INTMIN, INTMAX; ...
    (comp.lang.c)