Solved

Find the Biggest number without using if Statements?

Posted on 2006-07-23
30
450 Views
Last Modified: 2012-08-13
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

thanks in advance
deepu
0
Comment
Question by:deepu chandran
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 7
  • 7
  • +5
30 Comments
 
LVL 8

Expert Comment

by:hiteshgupta1
ID: 17165763
u can also use conditional o[perator
0
 
LVL 8

Expert Comment

by:hiteshgupta1
ID: 17165770
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
 
LVL 8

Expert Comment

by:hiteshgupta1
ID: 17165775
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Expert Comment

by:nafis_devlpr
ID: 17165781
you can do this:

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

although its a replacement of if statement.

Nafis
0
 
LVL 45

Accepted Solution

by:
sunnycoder earned 50 total points
ID: 17165792
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
 
LVL 8

Author Comment

by:deepu chandran
ID: 17165795

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

deepu
0
 
LVL 8

Author Comment

by:deepu chandran
ID: 17165814
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
 
LVL 45

Expert Comment

by:sunnycoder
ID: 17165818
Hi deepudeepam,

comparison is not quite same as if statement ...

are loops permitted?

Cheers!
sunnycoder
0
 
LVL 8

Author Comment

by:deepu chandran
ID: 17165831
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
 
LVL 7

Expert Comment

by:nafis_devlpr
ID: 17165894
why not try this

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

Nafis
0
 
LVL 7

Expert Comment

by:nafis_devlpr
ID: 17165902
or this

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

Nafis
0
 
LVL 7

Expert Comment

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

Nafis
0
 
LVL 8

Author Comment

by:deepu chandran
ID: 17166013
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
 
LVL 45

Expert Comment

by:sunnycoder
ID: 17166024
Hi deepudeepam,

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

Cheers!
sunnycoder
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 17166026
the above solution asumes 32bit ints ...
0
 
LVL 8

Author Comment

by:deepu chandran
ID: 17166075
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
 
LVL 45

Expert Comment

by:sunnycoder
ID: 17166097
Hi deepudeepam,

oooops ... forgot the shift

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

Expert Comment

by:nafis_devlpr
ID: 17166114
>> 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
 
LVL 45

Expert Comment

by:sunnycoder
ID: 17166120
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
 
LVL 7

Expert Comment

by:nafis_devlpr
ID: 17166143
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
 
LVL 45

Expert Comment

by:sunnycoder
ID: 17166149
Hi nafis_devlpr,

Interesting ... which compilers do that?

Cheers!
sunnycoder
0
 
LVL 7

Expert Comment

by:nafis_devlpr
ID: 17166183
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
 
LVL 45

Expert Comment

by:sunnycoder
ID: 17166236
Hi nafis_devlpr,

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

Cheers!
sunnycoder
0
 
LVL 8

Author Comment

by:deepu chandran
ID: 17166317
hi nafis,

it results -1  in vc++
deepu
0
 
LVL 22

Expert Comment

by:grg99
ID: 17166653
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
 
LVL 8

Author Comment

by:deepu chandran
ID: 17166741
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
 
LVL 5

Expert Comment

by:bastibartel
ID: 17168164
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
 

--------------------------------------------------------
Now the lengthier answer.

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
 
LVL 16

Expert Comment

by:PaulCaswell
ID: 17170975
Hi deepudeepam,

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

Paul
0
 
LVL 11

Expert Comment

by:cup
ID: 17171682
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
 
LVL 5

Expert Comment

by:bastibartel
ID: 17171998
As I understand the question, there shall be no conditionals at all.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question