By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,213 Members | 2,108 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,213 IT Pros & Developers. It's quick & easy.

Greatest of three numbers

P: n/a
How to find the greatest of three numbers without using any comparison
operator or ternary operator??

Jul 1 '07 #1
Share this Question
Share on Google+
30 Replies


P: n/a

"Amar Kumar Dubedy" <ad*****@yahoo.co.inwrote in message
news:11**********************@e9g2000prf.googlegro ups.com...
How to find the greatest of three numbers without using any comparison
operator or ternary operator??
If the numbers are positive integers

a/b is zero if b is greater than a.

C allows an implict comparison to zero, so

if(a/b) is equivalent to if( b a)

and you can build a first stab around that.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm
Jul 1 '07 #2

P: n/a
Amar Kumar Dubedy wrote, On 01/07/07 07:36:
How to find the greatest of three numbers without using any comparison
operator or ternary operator??
Put them all in a ring, throw weapons in, and wait to see which one
comes out alive.

If it is homework, do it yourself, if you are doing some stupid quiz, do
it yourself. People *might* help if you post an attempt here, but why
should we do it for you?
--
Flash Gordon
Jul 1 '07 #3

P: n/a
Amar Kumar Dubedy said:
How to find the greatest of three numbers without using any comparison
operator or ternary operator??
Ensure that one of the numbers is LDBL_MAX, and simply return LDBL_MAX
from your solving function. That way, you don't need any operators of
any kind at all.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jul 1 '07 #4

P: n/a

"Amar Kumar Dubedy" <ad*****@yahoo.co.inha scritto nel messaggio news:11**********************@e9g2000prf.googlegro ups.com...
How to find the greatest of three numbers without using any comparison
operator or ternary operator??
#include <stdlib.h>
....
if (a - b == abs(a - b))...

(but beware of overflows...)

HTH.
Jul 1 '07 #5

P: n/a
Army1987 wrote:
"Amar Kumar Dubedy" <ad*****@yahoo.co.inha scritto nel messaggio
news:11**********************@e9g2000prf.googlegro ups.com...
>How to find the greatest of three numbers without using any comparison
operator or ternary operator??

#include <stdlib.h>
...
if (a - b == abs(a - b))...
In what way is `==` not a comparison operator?

--
Archetype Hedgehog
"No-one here is exactly what he appears." G'kar, /Babylon 5/

Jul 1 '07 #6

P: n/a
Army1987 said:
>
"Amar Kumar Dubedy" <ad*****@yahoo.co.inha scritto nel messaggio
news:11**********************@e9g2000prf.googlegro ups.com...
>How to find the greatest of three numbers without using any
comparison operator or ternary operator??

#include <stdlib.h>
...
if (a - b == abs(a - b))...
== can be regarded as a comparison operator.
(but beware of overflows...)
That's another problem.

The Right Thing here is to use comparison operators - specifically the
'greater-than' operator:

max = a;
if(b max) { max = b; }
if(c max) { max = c; }

Therefore, the question is mistaken.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jul 1 '07 #7

P: n/a
In article <vJ********************@fe2.news.blueyonder.co.uk> ,
Chris Dollin <eh@electrichedgehog.netwrote:
>Army1987 wrote:
>>How to find the greatest of three numbers without using any comparison
operator or ternary operator??
>#include <stdlib.h>
...
if (a - b == abs(a - b))...
>In what way is `==` not a comparison operator?
Perhaps Army was thinking of "relational operator", which C defines
as not including the equality operators.

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Jul 1 '07 #8

P: n/a
In article <11**********************@e9g2000prf.googlegroups. com>,
Amar Kumar Dubedy <ad*****@yahoo.co.inwrote:
>How to find the greatest of three numbers without using any comparison
operator or ternary operator??
Do you really need to determine which of them it is, or would it be
sufficient to return a number equal to the greatest one?

-- Richard


--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Jul 1 '07 #9

P: n/a

"Richard Tobin" <ri*****@cogsci.ed.ac.ukha scritto nel messaggio news:f6***********@pc-news.cogsci.ed.ac.uk...
In article <vJ********************@fe2.news.blueyonder.co.uk> ,
Chris Dollin <eh@electrichedgehog.netwrote:
>>Army1987 wrote:
>>>How to find the greatest of three numbers without using any comparison
operator or ternary operator??
>>#include <stdlib.h>
...
if (a - b == abs(a - b))...
>>In what way is `==` not a comparison operator?

Perhaps Army was thinking of "relational operator", which C defines
as not including the equality operators.
if (!(a - b - abs(a - b))
(But beware of disasters...)
Jul 1 '07 #10

P: n/a

"Amar Kumar Dubedy" <ad*****@yahoo.co.inwrote in message
news:11**********************@e9g2000prf.googlegro ups.com...
How to find the greatest of three numbers without using any comparison
operator or ternary operator??
well, just a guess, wandering by this group, and people have already tried
answering...

just a guess really (assumes 3 integers 0).

int norz(int a, int b)
{ return(a*ceil((a/b)*((float)b/a))); }
int max(int a, int b)
{ return(norz(a, b)+norz(b, a)); }
int max2(int a, int b, int c)
{ return(max(max(a, b), max(b, c))); }

Jul 1 '07 #11

P: n/a
cr88192 said:
int norz(int a, int b)
{ return(a*ceil((a/b)*((float)b/a))); }
Fails when b is 0.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jul 1 '07 #12

P: n/a
Richard Heathfield wrote:
cr88192 said:
[un-snip]
>just a guess really (assumes 3 integers 0).
>int norz(int a, int b)
{ return(a*ceil((a/b)*((float)b/a))); }

Fails when b is 0.
If b is 0, then you don't have three integers 0.
Jul 1 '07 #13

P: n/a
Harald van D?k said:
Richard Heathfield wrote:
>cr88192 said:
[un-snip]
>>just a guess really (assumes 3 integers 0).
>>int norz(int a, int b)
{ return(a*ceil((a/b)*((float)b/a))); }

Fails when b is 0.

If b is 0, then you don't have three integers 0.
In which case we have demonstrated that the assumption is false. :-)

Note that this assumption was not present in the OP.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jul 1 '07 #14

P: n/a

"Richard Heathfield" <rj*@see.sig.invalidwrote in message
news:0N*********************@bt.com...
Harald van D?k said:
>Richard Heathfield wrote:
>>cr88192 said:
[un-snip]
>>>just a guess really (assumes 3 integers 0).

int norz(int a, int b)
{ return(a*ceil((a/b)*((float)b/a))); }

Fails when b is 0.

If b is 0, then you don't have three integers 0.

In which case we have demonstrated that the assumption is false. :-)

Note that this assumption was not present in the OP.
If we allow that the number must be positive integers then it turns into a
trival problem of C syntax, which I don't mind helping the OP with even if
it is homework.
Make them arbitrary floating point numbers and it is a lot more difficult.
Specify that the operation must not overflow machine precision for any legal
input and I think it might be impossible.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm
Jul 1 '07 #15

P: n/a
Malcolm McLean wrote, On 01/07/07 17:38:
>
"Richard Heathfield" <rj*@see.sig.invalidwrote in message
news:0N*********************@bt.com...
>Harald van D?k said:
>>Richard Heathfield wrote:
cr88192 said:
[un-snip]
just a guess really (assumes 3 integers 0).

int norz(int a, int b)
{ return(a*ceil((a/b)*((float)b/a))); }

Fails when b is 0.

If b is 0, then you don't have three integers 0.

In which case we have demonstrated that the assumption is false. :-)

Note that this assumption was not present in the OP.
If we allow that the number must be positive integers then it turns into
a trival problem of C syntax, which I don't mind helping the OP with
even if it is homework.
Make them arbitrary floating point numbers and it is a lot more
difficult. Specify that the operation must not overflow machine
precision for any legal input and I think it might be impossible.
Since the OP said "numbers" there is no reason to assume only +ve or an
integer type. There was also no license given to loose precision (so no
floating point arithmetic) or have it fail on some inputs. All of which
shows why it was a stupid question. However, there is an easy answer, it
just relies on the user of the program answering the questions the
program asks correctly.
--
Flash Gordon
Jul 1 '07 #16

P: n/a
Amar Kumar Dubedy wrote:
How to find the greatest of three numbers without using any comparison
operator or ternary operator??
if all three are unsigned...

#define N (CHAR_BIT * sizeof (unsigned) - 1)

int /* returns the carry flag of the sum a+b */
carry_flag (unsigned a, unsigned b)
{
int cf [2][2][2]= {{{0,0},{1,0}},{{1,0},{1,1}}};
unsigned s= a + b;
return cf [a >N] [b >N] [s >N];
}

int /* returns 1 iff a < b */
below (unsigned a, unsigned b)
{
return carry_flag (a + ~b, 1)
| carry_flag (a , ~b)

}

unsigned /* returns the greatest of 3 arguments */
greatest (unsigned a, unsigned b, unsigned c)
{
unsigned res [2][2][2];
res [0][0][0]= a; /* or any, all equal */
res [0][0][1]= res [0][1][1]= a;
res [1][0][0]= res [1][0][1]= b;
res [0][1][0]= res [1][1][0]= c;
res [1][1][1]= 0; /* should not happen */
return res [below (a,b)] [below (b,c)] [below (c,a)];
}

--
regis
Jul 1 '07 #17

P: n/a

"Richard Heathfield" <rj*@see.sig.invalidwrote in message
news:ss******************************@bt.com...
cr88192 said:
>int norz(int a, int b)
{ return(a*ceil((a/b)*((float)b/a))); }

Fails when b is 0.
yes, but note, I said, 3 integers 0.

that it fails when one of them is 0, is pretty damn obvious, which is why I
wrote this as an assumption...

now, by adding and subtracting 1 in the right places, it is possible to make
this valid for 3 integers >= 0...

oh well, the main reason I was here was seing if this was a good place to
talk about compiler writing and design (namely, I wrote a C compiler), ...,
but it does not look like it, which is why I have not posted on this subject
here...

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999

Jul 3 '07 #18

P: n/a
cr88192 wrote:
oh well, the main reason I was here was seing if this was a good place to
talk about compiler writing and design (namely, I wrote a C compiler),
..., but it does not look like it, which is why I have not posted on this
subject here...
<OT, I guess>
You're right, this isn't the best group for that, but you might like the
comp.compilers newsgroup for it.
</OT>
Jul 3 '07 #19

P: n/a
Amar Kumar Dubedy <adub...@yahoo.co.inwrote:
How to find the greatest of three numbers without using
any comparison operator or ternary operator??
For integers, just extend the case for 2 numbers...
[Posted Jan '05]

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.

% type max2.c
typedef unsigned long q1;int printf(const char*,...);long
strtol(const char*,char**,int);q1 q2(q1 q3,q1 q4,q1 q5,q1
q6){return(q5-q6)*(((((q3-q4)&(q4-q3) )+2)/(((q3-q4)&(q4-
q3))+1))-1)+q6;}q1 q7(long q8){return q2(q8,0,q2(q8/2,0,1
,0),0);}q1 q9(long q8){return q2(q8,-1,q2(q8/2,-1,1,q2(q8
/2,0,1,0) ),0);}q1 q10(long);q1 q11(long q8){return 0;}q1
q12(long q8){return 1;}q1 q13(long q8){return q10(q8/2);}
q1 q14(long q8){q1(*q15[])(long)={q12,q13};return q15[q2(
q9(q8),1,0,1)](q8);}q1 q10(long q8){q1(*q15[])(long)={q11
,q14};return q15[ q2(q7(q8),1,0 ,1)](q8);}q1 q17(long q3,
long q4){return q2(q10( q3),1,q2(q10(q4),1,q10(q3-q4),1),
q2(q10(q4),1 ,0,q10(q3-q4 )));}long q18(long q3,long q4){
return q3;}long q19(long q3,long q4){return q4;}long q20(
long q3,long q4){long(*q15[])(long,long)={q19,q18};return
q15[q2(q17(q3,q4),1,0,1)](q3,q4);}long q21(const char*q2)
{return strtol(q2,0,10);}int q22(char **q23){printf("%ld"
"\n",q20(q21(q23[1]),q21(q23[2])));return 0;}int q24(char
**q23){return 0;}int main(int q25,char**q23){int(*q15[])(
char**)={q24,q22};return q15[q2(q25,3,1,0)](q23);}

% gcc -ansi -pedantic max2.c -o max2.exe

% max2 -5 42
42

--
Peter

Jul 3 '07 #20

P: n/a
cr88192 said:
>
"Richard Heathfield" <rj*@see.sig.invalidwrote in message
news:ss******************************@bt.com...
>cr88192 said:
>>int norz(int a, int b)
{ return(a*ceil((a/b)*((float)b/a))); }

Fails when b is 0.

yes, but note, I said, 3 integers 0.
Note that your "solution" thus solved a different problem to the one
actually asked.
oh well, the main reason I was here was seing if this was a good place
to talk about compiler writing and design (namely, I wrote a C
compiler),
Presumably you were asked to write a Fortran compiler, and wrote at the
top of the design spec "assuming Fortran means C..."

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jul 3 '07 #21

P: n/a

"Harald van D?k" <tr*****@gmail.comwrote in message
news:f6**********@news2.zwoll1.ov.home.nl...
cr88192 wrote:
>oh well, the main reason I was here was seing if this was a good place to
talk about compiler writing and design (namely, I wrote a C compiler),
..., but it does not look like it, which is why I have not posted on this
subject here...

<OT, I guess>
You're right, this isn't the best group for that, but you might like the
comp.compilers newsgroup for it.
</OT>
sadly, it is moderated and filled with people obsessing on parsers...

as a result, had mostly been hanging around alt.lang.asm and comp.lang.misc.

here seems to be mostly a place for newbs to ask questions...
OT, basic description of effort:

now, my parser, well, it is a big mass of hand-written C code, not much to
say about that (I suspect most people with any interest in compiler or
interpreter writing, maybe run into the problem of syntax, or get some crude
interpreter or translator working, and die off after this point).

parser is uniteresting, hard-coded recusive-descent...

now, IMO, the parser is the easy part...
the compilation process, type handling, and code generation. IMO, this is
where most of the work seems to reside (note, I compile all the way down to
machine code using purely my own code).

currently the thing is about 30 kloc, but a lot is still incomplete.

note that it is a multi-stage compiler (currently 5 major stages, with some
of these stages making multiple passes).
I am aiming for calling-convention level compatibility with gcc.

in general, it is designed for run-time compilation and linking (so that it
can operate similarly to a script language), but also have full (and direct)
access to the OS and host app.

at present it can also load object files and static libraries (GNU-ar
format), and can output object files, albeit for all these presently only
PE/COFF is supported (what little I have run it on linux, it is restricted
to what can be done with libdl).

another eventual goal is to allow its use for tasks along the lines of
'eval' (dynamically compiling and running small pieces of code, such as
single expressions), self-writing programs, ...
other possible eventual features:
reflection, ...
compiler extensions for high-level features (garbage collection, dynamic
types, lexical varaibles, closures, ...).

as noted, given a C core these would be less nice than in a true script
language (and totally craping all over the purity of C), but it could still
be worthwhile (well, probably still far less horrid than Objective-C, more
like what would happen if C, JavaScript, Self, ... collided, and more
keeping in line with the basic style and aesthetic of the C family...).

as is usual, probably requiring a special header, 'variant.h', which would
enable all this bizareness (and probably "#error" if used in something like
gcc...).
note:
this is really only my first statically-typed language, me previously
writing dynamically typed script languages (the final of which using JIT and
type inference, I had figured maybe it wouldn't be too huge of a leap from
one to another, but have been regularly underestimating things).

well, it was less easy than could be hoped, good 4+ solid months of coding
getting this beast written, internally, a very different thing than the
script language...

however, the framework should be able to handle both styles absent too much
additional effort (far less than writing the damn thing).
or something...

Jul 3 '07 #22

P: n/a
cr88192 said:

<snip>
here seems to be mostly a place for newbs to ask questions...
Mostly, alas, that is true. In fact, the quality of the answers usually
far exceeds the quality of the questions.
OT, basic description of effort:

now, my parser, well, it is a big mass of hand-written C code, not
much to say about that (I suspect most people with any interest in
compiler or interpreter writing, maybe run into the problem of syntax,
or get some crude interpreter or translator working, and die off after
this point).

parser is uniteresting, hard-coded recusive-descent...

now, IMO, the parser is the easy part...
Well, the lexer is the easy part, actually. The parser is a bit harder
than the lexer.

<project description snipped>

Sounds like you're having lots of fun.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Jul 3 '07 #23

P: n/a
cr88192 wrote:
>
"Harald van D?k" <tr*****@gmail.comwrote in message
news:f6**********@news2.zwoll1.ov.home.nl...
><OT, I guess>
You're right, this isn't the best group for that, but you might like the
comp.compilers newsgroup for it.
</OT>

sadly, it is moderated and filled with people obsessing on parsers...
Topics move around. If you want c.c to talk about something else,
/post/ about something else.

--
Interpreting Hedgehog
Notmuchhere: http://www.electrichedgehog.net/

Jul 3 '07 #24

P: n/a

"Richard Heathfield" <rj*@see.sig.invalidwrote in message
news:V9******************************@bt.com...
cr88192 said:

<snip>
>here seems to be mostly a place for newbs to ask questions...

Mostly, alas, that is true. In fact, the quality of the answers usually
far exceeds the quality of the questions.
yeah, sad really...

>OT, basic description of effort:

now, my parser, well, it is a big mass of hand-written C code, not
much to say about that (I suspect most people with any interest in
compiler or interpreter writing, maybe run into the problem of syntax,
or get some crude interpreter or translator working, and die off after
this point).

parser is uniteresting, hard-coded recusive-descent...

now, IMO, the parser is the easy part...

Well, the lexer is the easy part, actually. The parser is a bit harder
than the lexer.
true, but usually the lexer is easy enough, and done in the same pass as the
parser. as a result, I don't usually distinguish them.

so, what is the lexer?
maybe a few hundred loc?...
(source file, 514 loc, 130 going to line-number handling, 95 whitespace and
comments, 290 tokenizer).

parser? about 3 kloc total...

preprocessor, 741 loc;
expressions, 764 loc;
statements, 429 loc;
misc (definitions, args lists, ...), 733 loc.

very little actual "mechanics" are implemented by the parser (beyond
generating the ASTs).

the compiler is split into several stages:
upper (still needs a lot of work), 2.4 kloc, converts ASTs to a kind of IL
(I call RPNIL or RIL);
lower, 6.5 kloc (converts RPNIL to assembler, rather horrible code);
assembler, 10.25 kloc (obvious enough what this does...).
so, yeah, technically it is fairly small, but a lot of this code can be a
lot of effort to write and debug...

<project description snipped>

Sounds like you're having lots of fun.
if I am lucky it could actually be useful...

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999

Jul 3 '07 #25

P: n/a

"Richard Heathfield" <rj*@see.sig.invalidwrote in message
news:xs******************************@bt.com...
cr88192 said:
>>
"Richard Heathfield" <rj*@see.sig.invalidwrote in message
news:ss******************************@bt.com...
>>cr88192 said:

int norz(int a, int b)
{ return(a*ceil((a/b)*((float)b/a))); }

Fails when b is 0.

yes, but note, I said, 3 integers 0.

Note that your "solution" thus solved a different problem to the one
actually asked.
ok.

yes, 'number' and 'counting-number' are not necissarily the same.

>oh well, the main reason I was here was seing if this was a good place
to talk about compiler writing and design (namely, I wrote a C
compiler),

Presumably you were asked to write a Fortran compiler, and wrote at the
top of the design spec "assuming Fortran means C..."
hmm...

actually, this project was done for my own reasons:
it could be useful for my larger-scale project...

but, it has consumed more time and effort than I had originally expected (I
suspect my frequent depressive runs slow me down...).

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999

Jul 3 '07 #26

P: n/a
Richard Heathfield <rj*@see.sig.invalidwrote:
Note that your "solution" thus solved a different problem to the one
actually asked.
Given that the question actually asked was homework, maybe that was a
good thing...

--
C. Benson Manica | I *should* know what I'm talking about - if I
cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
Jul 3 '07 #27

P: n/a
On Jul 1, 2:36 am, Amar Kumar Dubedy <adub...@yahoo.co.inwrote:
How to find the greatest of three numbers without using any comparison
operator or ternary operator??
This is probably not what your professor wants (which is why I'm
willing to show it), but assuming c99 is available and that every
integer can be exactly represented as long double it seems to work:

#include <stdio.h>
#include <math.h>

int max(int a, int b, int c)
{
return fmaxl(fmaxl(a,b),c);
}

int main(void) {
int a= 5;
int b= 10;
int c= -3;

printf("%d\n", max(a,b,c));
return 0;
}

Is it in theory possible that a long double can't exactly represent
the largest possible integer and this will fail? If so, I'm sure
someone will say so. I don't use floats much, and lost interest
deciding whether DECIMAL_DIG or any of the other set of defines in
float.h is relevant here.
-David

Jul 3 '07 #28

P: n/a
Richard Heathfield <rj*@see.sig.invalidwrites:
cr88192 said:
<snip>
>here seems to be mostly a place for newbs to ask questions...

Mostly, alas, that is true. In fact, the quality of the answers usually
far exceeds the quality of the questions.
[...]

Well, the opposite would be worse.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jul 3 '07 #29

P: n/a
regis wrote:
Amar Kumar Dubedy wrote:
>How to find the greatest of three numbers without using any comparison
operator or ternary operator??
#define N (CHAR_BIT * sizeof (unsigned) - 1)

int /* returns the carry flag of the sum a+b */
carry_flag (unsigned a, unsigned b)
{
int cf [2][2][2]= {{{0,0},{1,0}},{{1,0},{1,1}}};
unsigned s= a + b;
return cf [a >N] [b >N] [s >N];
}

int /* returns 1 iff a < b */
below (unsigned a, unsigned b)
{
return carry_flag (a + ~b, 1)
| carry_flag (a , ~b)

}
Hmm, the function below() shown above is in fact above_or_equal().
The function shown below is indeed below() with a clearer logic:

int carry_flag (unsigned a, unsigned b, unsigned carry_in)
{
int cf [2][2][2]= {{{0,0},{1,0}},{{1,0},{1,1}}};
unsigned s= a + b + carry_in;
return cf [a >N] [b >N] [s >N];
}

int below (unsigned a, unsigned b)
{
return 1 - carry_flag (a, ~ b, 1);
}
Jul 4 '07 #30

P: n/a
regis <r...@dil.univ-mrs.frwrote:
Amar Kumar Dubedy wrote:
How to find the greatest of three numbers without using
any comparison operator or ternary operator??

if all three are unsigned...

#define N (CHAR_BIT * sizeof (unsigned) - 1)
The expression is flawed since unsigned ints can have
padding bits.
int /* returns the carry flag of the sum a+b */
carry_flag (unsigned a, unsigned b)
{
int cf [2][2][2]= {{{0,0},{1,0}},{{1,0},{1,1}}};
unsigned s= a + b;
return cf [a >N] [b >N] [s >N];
}
return ( (a >1)
+ (b >1)
+ (((a & 1) + (b & 1)) >1) ) / (-1u/2+1);

--
Peter

Jul 4 '07 #31

This discussion thread is closed

Replies have been disabled for this discussion.