x
• Status: Solved
• Priority: Medium
• Security: Public
• Views: 463

Find the Biggest number without using if Statements?

Hai Experts,

I have one doubt,i think its a freequently asked question for all c programmers.
My Question is,

Accept two number from the user and find the biggest number?(dont think that its funny)
i know its easy for all But one constaint is their dont use any if statements or standard C libraries?

[ is it possible using bitwise operators ?]

I expect early replays from experts

deepu
0
deepu chandran
• 8
• 7
• 7
• +5
1 Solution

Commented:
u can also use conditional o[perator
0

Commented:
say if there are two numbers then u can compare them like following

Big_Num=(n1 > n2) ? n1 : n2
IT will evaluate(n1>n2),if n1 is greater then n2 then it will return n1 otherwise it ewill return n2
0

Commented:
Conditional Operator is laso known as ternary operator
(The obscure name "ternary,'' by the way, comes from the fact that the conditional operator is neither unary nor binary; it takes three operands.)
0

Commented:
you can do this:

max= (a > b ? a : b);

although its a replacement of if statement.

Nafis
0

Commented:
Hi deepudeepam,

can try this
int nums[2];
num[0]=a;
num[1]=b;
printf("bigger num is%d",num[a<b]);
Ofcourse this does not handle the situation when a == b

Cheers!
sunnycoder
0

Author Commented:

hai..
Sorry,I am looking for an answer using parity chacking or bitwise operators

deepu
0

Author Commented:
hello Sunnycoder,

Your aswer is nice but it is also checking a condition like if statement,

Actually it was a question asked for a Debugging compatetion.I used a  similar approch but the pannel didnt approve.Please try some other idea

deepu
0

Commented:
Hi deepudeepam,

comparison is not quite same as if statement ...

are loops permitted?

Cheers!
sunnycoder
0

Author Commented:
Hai,

No,It was also not possible
One programmer give one idea,its like this

int a=10,b=20,c;//a and b are our variable c is a temparary variable
c=a-b;         //here we will get the difference

if c is -ve then b is biggest,otherwise a .I think some way to check the parity of that variable

ok thank you for your fast responce

deepu
0

Commented:
why not try this

max=((((a-b) & (1 << 31)) == 0) ? a : b);

Nafis
0

Commented:
or this

max=((a-b) & (1 << 31)) ? b : a;

Nafis
0

Commented:
soo many postings in so little time, sorry deepu didnt your posting, but I think my idea is similar with that programmer.

Nafis
0

Author Commented:
hello nafis,

>>> max=((((a-b) & (1 << 31)) == 0) ? a : b);

Your answer is using the conditional operator.This will not meet my requirement.

deepu
0

Commented:
Hi deepudeepam,

int nums[2];
num[0]=a;
num[1]=b;
printf("bigger num is%d",num[(a-b) & 0x80000000]);

Cheers!
sunnycoder
0

Commented:
the above solution asumes 32bit ints ...
0

Author Commented:
hello sunnycoder,

I am sorry to tell you that your solution is not working.
if a is biggest then ok it will otherwise it will not.
Reason is that time it results one -ve value

ok thank you
deepu
0

Commented:
Hi deepudeepam,

oooops ... forgot the shift

printf("bigger num is%d",num[((a-b) & 0x80000000])>>31);
Cheers!
sunnycoder
0

Commented:
>> printf("bigger num is%d",num[(a-b) & 0x80000000]);

problem in this segment if b > a, then the array bound will overflow causing an unexepected error, what you can do is this

printf("bigger num is%d",num[(((a-b) & 0x80000000) >> 31) & 0x00000001]);

Nafis

0

Commented:
Hi nafis_devlpr,

> printf("bigger num is%d",num[(((a-b) & 0x80000000) >> 31) & 0x00000001]
ANDing with 0x01 is not required since previous AND would have left you with either 0x00 or 0x80000000

Cheers!
sunnycoder
0

Commented:
yes, you are right, but some compilers preserve the sign bits so if a-b is -ve then the shifting with sign bits preserved will result -1, so for the sake of portability the last AND was done.

Nafis
0

Commented:
Hi nafis_devlpr,

Interesting ... which compilers do that?

Cheers!
sunnycoder
0

Commented:
try this on mingw,gcc and vc++ see the results,

int a=24,b=26,c;
c=(((a-b) & (1 << 31)) >> 31);

even I was baffled when I first see that, the compilers are getting more intelligent but sometimes they do disturb the logical facts.

Nafis
0

Commented:
Hi nafis_devlpr,

No compiler at hand ... what was the output?

Cheers!
sunnycoder
0

Author Commented:
hi nafis,

it results -1  in vc++
deepu
0

Commented:
Easy.    Several ways:

(1)  instead of an if(), use a while()

(2)  Go thru the numbers from highest bit to lowest bit.  Whichever number has a bit set that the other one doesnt is the bigger number.
Something like:

#define  NumBits    sizeof(int)*8

unsigned int a, b, ba, bb, index;  int   atot, btot;

a = something;     b = something else;

i = NumBits - 1; atot = btot = 0;
do {
ba = (a >> i   ) & 1;
bb = (b >> i--)  & 1;
atot += ba;   btot += bb;
} while( atot == btot );

index = ( atot - btot + 1) / 2;

printf( "The bigger number is: %c\n", index + 'a' );

Voila!    I think.

0

Author Commented:
hi grg99,

Here you are using conditional checking.
if here is any while then we can do it easy.

Sorry this is not i am expecting

deepu
0

Commented:
Hi deepudeepam,

First the short answer for VC6 (and presumably other compilers doing arithmetic bitwise shifting)

------------------------------------
int num[2];
num[0] = 12;
num[1] = 1231;
int i    = 1+ ((num[1]-num[0]) >> 31));       // yields 1 + (-1)  if (num[1]-num[0]  < 0)
//  yields 1 + (0)   if (num[1]-num[0]  >= 0)

printf("%i is the bigger one", num[i]) ;
--------------------------------------

The reason why ('negative number') & 0x80000000 yields -1 is because VC 6 seems to perform 'arithmetic shifting' rather 'logical shifting'
The former preserves the sign-bit, so the leading sign-bit is never shifted at all.

Check Wikipeda on this... http://en.wikipedia.org/wiki/Bitwise_operation

--------------------------------------------------------

If the machine represents integer values as 'two's complement' representation (which is ANSI recommendation I think)
Then the most significant bit, i.e. the leftmost holds the sign information. a value of 1 indicates a negative sign.

So then if you look at
int a = 8;
int b = 9;
int c = a-b;
int d = b-a;

then c looks like 10000000000000000000000000000001
and d looks like  00000000000000000000000000000001

so simply checking the leftmost bit of the difference would give you what you need
In case of 32 bit integer it would be

(a-b) >> 31  // == 1 if a<b

HOWEVER, when I look at my memory window (VC6 I see a slightly  different picture.
integer values from 000[..32bit..]0000  to FFF[..32bit..]FFF are sorted as

00000000000000000000000000000000   == 0
00000000000000000000000000000001   == 1
00000000000000000000000000000001   == 2

01111111111111111111111111111111    == 2147483647
10000000000000000000000000000000    == -2147483648
10000000000000000000000000000001    == -2147483647
10000000000000000000000000000010    == -2147483646
10000000000000000000000000000100    == -2147483644

11111111111111111111111111111110    == -2
11111111111111111111111111111111    == -1

Hence, (a-b) >> 31 should yield decimal 1 if (a-b) is negative:

(some negative number)
10000000000000000001101000000010 >> 31 = 000000000000000000000000000001

but it doesn't im my VC. Instead the leading binary 1 is preserved in this operation yielding

10000000000000000000110100000001

.. all bits except the leading 1 are shifted,
so I am assuming VC does arithmetic bit shifting.

Knowing that, the trick is done by 1 + ( (a-b) >> 31))
which yields +1,0 respectively rather than 0,-1

Cheers!
Sebastian
0

Commented:
Hi deepudeepam,

Couldn't you use the fact that a/b == 0 or b/a == 0 or a == b?

Paul
0

Commented:
I can't see why the shifting is required.  False is 0 and true is 1, so using sunnycoder's solution,

int nums[2];
num[0]=a;
num[1]=b;
printf("bigger num is%d",num[(a-b) < 0]);
0

Commented:
As I understand the question, there shall be no conditionals at all.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.