473,322 Members | 1,510 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Greatest of three numbers

How to find the greatest of three numbers without using any comparison
operator or ternary operator??

Jul 1 '07 #1
30 8185

"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
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
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

"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
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
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
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
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

"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

"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
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
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
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

"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
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
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

"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
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
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
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

"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
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
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

"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

"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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Mike Nolan | last post by:
As far as I can tell, Postgres has no equivalent to greatest and least functions in Oracle. Yes, you can do the same thing with a case statement, but at the expense of writing MUCH longer SQL...
7
by: cess | last post by:
Hi!!! i would like to know if what is lacking in the codes below to have a greatest common denominator of two given(by the user) numbers?? I'm confused and i need your help! import java.io.*;...
3
by: stressedstudent | last post by:
I dont know where I am going wrong so I dont know which part to post, this is what I have, can anyone help me figure out where I am going wrong? THanks for any and all help. // into to c++ //...
2
by: Shawn Minisall | last post by:
I just wrote a program to let the user input a series of whole numbers and tell them which is least and which is greatest based off of a menu. However, the menu isn't kicking in after they pick a...
35
by: aarklon | last post by:
Hi all, The following question is asked frequently in interviews How to find the greatest of 2 numbers without using relational operators ? the solution i have seen is ( a+b + abs(a-b) )...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.