Can anyone tell me how to write a C program to compare numbers(including negative numbers) without using logical operators?
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.
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.
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.
- 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?
- 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?
- Now you have all information needed to answer the question.
- Hint: you are, of course, allowed to use
if
andelse
statements. - Bonus hint: you may want to use
INT_MIN
fromlimits.h
.
- Hint: you are, of course, allowed to use
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.
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...
You can use the relational operators (<, >, <=, >=) and/or the equality operators (==, !=).
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...
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}
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 if
s would do it handily.