Re: Comparing two numbers
Nick Austin wrote:[color=blue]
> On 16 Jan 2005 16:01:42 -0800, "Peter Nilsson" <airia@acay.com .au>
> wrote:
>
> [...][color=green]
> >The following uses strtol() to convert arguments on the command
> >line, and printf() to output the maximum. Otherwise, it doesn't
> >use any conditional, logical, equality or relational operators,
> >nor does it use any selection or iteration statements.[/color]
>
> [...][color=green]
> >typedef unsigned long q1;
> >q1 q2(q1 q3,q1 q4,q1 q5,q1 >q6)
> >{
> > return(q5-q6)*(((((q3-q4)&(q4-q3)[/color][/color]
)+2)/(((q3-q4)&(q4-q3))+1))-1)+q6;[color=blue][color=green]
> >}[/color]
>
> I don't think that's portable, namely the use of the bitwise[/color]
operator:[color=blue]
>
> ( q3 - q4 ) & ( q4 - q3 )
>
> It seems to me that it assumes twos compliment and could give the
> wrong answer on other architectures.[/color]
"Two's complement" applies to _signed_ integers. The quoted expression
operates on unsigned integers, for which there is only one choice
of representation, namely, pure binary, and for which the behaviour on
overflow is well defined.
--
Peter
Nick Austin wrote:[color=blue]
> On 16 Jan 2005 16:01:42 -0800, "Peter Nilsson" <airia@acay.com .au>
> wrote:
>
> [...][color=green]
> >The following uses strtol() to convert arguments on the command
> >line, and printf() to output the maximum. Otherwise, it doesn't
> >use any conditional, logical, equality or relational operators,
> >nor does it use any selection or iteration statements.[/color]
>
> [...][color=green]
> >typedef unsigned long q1;
> >q1 q2(q1 q3,q1 q4,q1 q5,q1 >q6)
> >{
> > return(q5-q6)*(((((q3-q4)&(q4-q3)[/color][/color]
)+2)/(((q3-q4)&(q4-q3))+1))-1)+q6;[color=blue][color=green]
> >}[/color]
>
> I don't think that's portable, namely the use of the bitwise[/color]
operator:[color=blue]
>
> ( q3 - q4 ) & ( q4 - q3 )
>
> It seems to me that it assumes twos compliment and could give the
> wrong answer on other architectures.[/color]
"Two's complement" applies to _signed_ integers. The quoted expression
operates on unsigned integers, for which there is only one choice
of representation, namely, pure binary, and for which the behaviour on
overflow is well defined.
--
Peter
Comment