Solved

Find the Biggest number without using if Statements?

Posted on 2006-07-23
30
445 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
  • 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
 
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

758 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now