블로그 이미지
fiadot_old

칼퇴근을 위한 게임 서버 개발 방법론에 대한 심도있는 고찰 및 성찰을 위한 블로그!

Rss feed Tistory
Technical Article/펌 2003. 12. 30. 09:14

0을 제외한 두 자연수중 작은 수를 구하는 방법

Q> if문이나 ?연산자를 사용하지 않고 0을 제외한 두 자연수중 작은 수를 구하는 방법이 있을까요.

A1>

이런거 찾으시는거 맞죠? optimize를 할 때 흔히 사용되는 듯 하더군요.

static int int_max(int a, int b) {
b = a-b;
a -= b & (b>>31);
return a;
}

static int int_abs(int a) {
return a - ((a+a) & (a>>31));
}

static int int_min(int a, int b) {
b = b-a;
a += b & (b>>31);
return a;
}
_________________
--
Taeho Oh ( ohhara@postech.edu, ohhara@plus.or.kr ) http://ohhara.sarang.net
Postech ( Pohang University of Science and Technology ) http://www.postech.edu
PLUS ( Postech Laboratory for Unix Security ) http://www.plus.or.kr



A2>
int a = 4; // a=8
int b = 8; // b=4
int mul = a * b;
int c = (a + mul) / (b + mul);
int d = (b + mul) / (a + mul);
int e = b * c + a * d;
System.out.println(e);

A2-Error>
a == b 일때 성립하지 않습니다.
PS. 하긴 '작은' 것을 구하는 거니까 a == b인 경우는 undefined겠군요;;


A3>
Element Comparison 기반이 아닌 정렬 알고리즘이 있으니 비교하지 않고 비교한다는 것이 가능.
,
TOTAL TODAY