tags:

views:

1529

answers:

10

Can anyone tell me how to write a C program to compare numbers(including negative numbers) without using logical operators?

+8  A: 

Yeah, you can subtract them and look at the difference.

I know, that leads to the next question: How can you tell if the difference is < 0, = 0 or > 0? Well, you can take care of < 0 by checking if the sign bit is set.

Carl Smotricz
Which leads to the question: how to inspect the difference without logical operators ;)
Stephan202
sir can u show the program how it can be.
Madhan
No. I don't want to do your homework.
Carl Smotricz
outis
+1  A: 

Suppose you have int x and int y. Let int z=x-y, and then repeatedly (31 or 63 times depending on your preferences) take z|=z<<1 and z|=z>>1. If the difference is zero, z=0, and if the difference is nonzero, z=-1. Add one to z and use it to index into an array of function pointers containing the instructions you want to follow in each case.

You can do this more efficiently if you're allowed to use assembly, but since this doesn't sound like a practical problem I don't think those solutions are worth exploring too deeply. If you wanted a practical solution, you'd just use the logic operators :)

Now, the problem statement is highly dubious as computer arithmetic is defined using "logic operators" but if you naively assume (as they probably want you to) that what you type is what you use, the above solution should suffice.

EDIT: You DO NOT need to use if/else statements. If the sign of the comparison is important, bring the sign into the 2s place and use that to index into a function pointer array.

Last sentence is a clever hint, +1.
Carl Smotricz
+7  A: 

We're not going to do your homework, but let me add some hints on how to proceed. We assume that we're going to compare a and b.

  1. As Carl states, you may want to look at the difference d = a - b.
    • Observation: d can be negative, zero or positive.
    • Question: which value of d corresponds to which (in)equality of a and b?
  2. Once you have calculated d and know how to interpret it, you have to find ways to inspect it without using logical operators.
    • Observation: obvious candidates are the bitwise operators.
    • Question: but how do computers (usually) store integers?
    • Hint: have a look at two's complement.
    • Question: how can you tell the difference between a negative and a non-negative number?
  3. Now you have all information needed to answer the question.
    • Hint: you are, of course, allowed to use if and else statements.
    • Bonus hint: you may want to use INT_MIN from limits.h.

Edit: this answer, and most other answers here assume that the OP is not allowed to use logical and relational (comparison) operators. However, the OP only explicitly mentions that logical operators are disallowed. I think the main reason for this misunderstanding is the fact that the problem is trivial to solve using comparison operators.

Stephan202
+2  A: 

Here is a program that compares the two numbers without using relation operators (it also works for negative numbers). check it out.

/------C program-------/

void main()
{
     int a,b,c,temp;
     printf("enter a and b:");
     scanf("%d%d",&a,&b);
     c=a-b;
     temp=c+abs(c);      // to check if the difference is negative or not
     if(temp==0)
     printf("a is smaller than b");
     else 
     printf("a is bigger than b");
     getch();
     }

EDIT: Maddy's revised code (from his comment below) follows.

void main() { 
  int a,b,c,d,temp; 
  printf("enter a and b:"); 
  scanf("%d%d",&a,&b); 
  c=a-b; 
  d=abs(c); 
  temp=c+d; 
  if (c==0) printf("a is equal to b"); 
  else if(temp==0) printf("a is smaller than b"); 
  else printf("a is bigger than b"); 
  printf("%d",d); getch(); 
}

i think now the code works well for all inputs...

Madhan
Giving a full, even though not correct, solution to a homework question is never good. Hence the down vote.
Paulius Maruška
The above code works well. i did not get what is the problem with this code?
Madhan
@Paulius: indeed this is incorrect, but note that it is the OP who posted this answer.
Stephan202
@Maddy: what if *a = b*?
Stephan202
void main(){ int a,b,c,d,temp; printf("enter a and b:"); scanf("%d%d", c=a-b; d=abs(c); temp=c+d; if(c==0) printf("a is equal to b"); else if(temp==0) printf("a is smaller than b"); else printf("a is bigger than b"); printf("%d",d); getch(); }i think now the code works well for all inputs...
Madhan
@Stephan202: Oh, lol. It's SO fault! It used to color the OP posts differently... I took my down-vote back. :)
Paulius Maruška
You've hidden the relation operators in your `abs` call. This said it is possible to implement `abs` with bit twiddling hacks http://graphics.stanford.edu/~seander/bithacks.html#IntegerAbs
tristopia
+3  A: 

You can use the relational operators (<, >, <=, >=) and/or the equality operators (==, !=).

aib
+1: only now I notice that the OP doesn't exclude those. I wonder whether we collectively misinterpreted the question, or that the OP did not properly state his assignment. (I'm inclined to go for the latter.)
Stephan202
See my second solution, near the bottom. I agree that we probably made a big deal out of an almost trivial question. Shame on us! Err, you!
Carl Smotricz
I went for the latter as well, but still wanted to see a strict literal answer here. And maybe point out the pointlessness of such an excercise. (Now, if the question had said "two's complement signed integers" or "arithmetic logic" instead of "C", it would have been another matter.)
aib
A: 

Here is a program that compares the two numbers without using relation operators (it also works for negative numbers).

void main()
{
     int a,b,c,d,temp;
     printf("enter a and b:");
     scanf("%d%d",&a,&b);
     c=a-b;
     d=abs(c);
     temp=c+d;
     if(c==0)
         printf("a is equal to b");
     else if(temp==0)
         printf("a is smaller than b");
     else 
         printf("a is bigger than b");
     printf("%d",d);
     getch();
 }

I think now the code works well for all inputs...

Madhan
@Maddy: select your code and press `ctrl+k` to properly format it here on SO. Also, please properly indent it (like I did for you).
Stephan202
@Maddy, also, next time please update/edit your previous answer instead of posting a new one.
Stephan202
@stephan ok sir,as i am new to this i dont know that.from now onwards, i will follow it.Thank u.
Madhan
A: 

Keep in mind that logic operator are much faster than arithmetic operators.

As an answer for you...

if(x-y){ elements are different}else{ they are equals}
Quamis
You sure bout that statement? In a modern CPU, most (all?) integer 'rithmetic is done in 1 cycle, while logic usually means jumping and jumping usually means dumped pipelines.
Carl Smotricz
actually no:) the JMP instructions are detected by the branch predictor in the CPU and are prefeched before even the need for execution.Arithmetic is faster for 8-32bytes, for MMX is actually slower(not really sure about that, but i guess it would be easy to find out)"Dumped pipelines" happens when doiung calls, not local(small) jumps.
Quamis
+8  A: 

This is Carl's evil twin, here to tell you you're all puny weaklings!

Here's a solution that does not only without logical operators but also without relational operators (including ==) and without if. Just arithmetic and indexing, baby!

#include "stdio.h"
#include "limits.h"
int sign(int number) {
  return (unsigned) number / (unsigned) INT_MIN;
}
int main(int argc, char *argv[]) {
  int a = atoi(argv[1]);
  int b = atoi(argv[2]);
  int dif = a - b;
  int sb1 = sign(dif);
  int sb2 = sign(dif - 1) - sb1;
  int ptr = 2 * sb2 + sb1;
  char *messages[3] = {
    "%d is greater than %d",
    "%d is less than %d",
    "%d is equal to %d" };
  printf(messages[ptr], a, b);
}

Some test results:

$ ./compare 42 69
42 is less than 69

$ ./compare 69 42
69 is greater than 42

$ ./compare 42 -15
42 is greater than -15

$ ./compare 999 999
999 is equal to 999

$ ./compare -9 -10
-9 is greater than -10

The original question, on the other hand, is much easier. Logical operators are just !, &, &&, |, || and !. I think a couple of nested ifs would do it handily.

Carl Smotricz
+1 Because I like this solution as it does not use `if` which feels like cheating.
Peter G.
A: 

u r using logical operator man..................

Ankur
Which man are you talking to?
Peter G.
A: 

if(a-b) printf("They are not equal"); else printf("They are equal");

venakt