2014-09-24 70 views
-4

初學C(系統)

include <stdio.h> 
int main() { 

int n1 = -1; 
int n2 = 2; 

unsigned int u1 = (unsigned int) n1; 
unsigned int u2 = (unsigned int) n2; 


int result = (n1 < n2) == (-n1 > -n2); 
print f("(%d < %d) == (-%d > -%d) evaluates to %d\n", n1, n2, n1, n2, result); 


/*question 1 
Is result always 1 (true)? 
*/ 

result = ~n1 + ~n2 == ~(n1 + n2); 
printf("(~%d + ~%d) == ~(%d + %d) evaluates to %d\n", n1, n2, n1, n2, result); 

return 0; 

/*question 1 
Is result always 0(false)? 
*/ 
} 

我沒有得到什麼概念#1試圖展示,但我認爲#2基本上詢問是否在分發〜情況。結果總是0嗎?解釋

回答

0

如果通過一個負數乘以一個有效不等式的兩邊,則必須reverse the order of the inequality它是有效的:

(a < b)*-1 -> (-1*a > -1*b) 

因此,對於問題1,result總是或1:

int result = (n1 < n2) == (-n1 > -n2); 

如果n1小於n2,然後-n1總是比-n2更大,所以EQUA對於這種情況,人性測試是正確的。

如果n1不低於n2少,那麼-n1不會比-n2更大,所以平等的測試也是如此的話。因此平等測試總是如此。