a logic questions

Guys someone asked a question...

if there are two integers in some variables etc.. how can u find which one is smaller and which one is bigger provided u cant use relation operators

if anyone got any idea do tell me ASAP
You could subtract one form another and check whether the result is negative by checking the most significant bit.
That sounds fun!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <limits>

using namespace std;

int max(int a, int b) {
	int c = a - b;
	c &= numeric_limits<int>::min();
	return c ? b : a;
}

int main(int argc, char *argv[]) {
	if( argc < 2 ) {
		cerr << "Supply at least 1 arguments." << endl;
		return 1;
	}
	
	int m = atoi(argv[1]);
	for( int i = 2; i < argc; i++ )
		m = max( m, atoi(argv[i]) );
	cout << "The maximum argument value is " << m << endl;
	
	return 0;
}
Topic archived. No new replies allowed.