简体   繁体   English

1 = 假 0 = 真?

[英]1 = false and 0 = true?

I came across an is_equals() function in a c API at work that returned 1 for non-equal sql tables (false) and 0 for equal ones (true). I came across an is_equals() function in a c API at work that returned 1 for non-equal sql tables (false) and 0 for equal ones (true). I only realized it after running test cases on my code, one for the positive example and one for the negative and they both failed which at first made little sense.我只是在对我的代码运行测试用例后才意识到这一点,一个用于正面示例,一个用于负面示例,它们都失败了,这起初毫无意义。 The code in the API does not have a bug as the output was recorded correctly in its documentation. API 中的代码没有错误,因为 output 已正确记录在其文档中。

My questions - are there upside down worlds / parallel universes / coding languages where this logical NOTing is normal?我的问题 - 有没有颠倒的世界/平行宇宙/编码语言,这种逻辑注释是正常的? Isn't 1 usually true? 1 通常不是真的吗? Is the coder of the API making an error? API 的编码器是否出错?

It is common for comparison functions to return 0 on "equals", so that they can also return a negative number for "less than" and a positive number for "greater than". 比较函数通常在“等于”时返回0 ,因此它们也可以返回“小于”的负数和“大于”的正数。 strcmp() and memcmp() work like this. strcmp()memcmp()就像这样工作。

It is, however, idiomatic for zero to be false and nonzero to be true, because this is how the C flow control and logical boolean operators work. 然而,惯性是零为假而非零为真,因为这是C流控制和逻辑布尔运算符的工作方式。 So it might be that the return values chosen for this function are fine, but it is the function's name that is in error (it should really just be called compare() or similar). 所以可能是为这个函数选择的返回值很好,但是函数的名称是错误的(它应该只是称为compare()或类似)。

This upside-down-world is common with process error returns. 这种颠倒世界在过程错误返回时很常见。 The shell variable $? shell变量$? reports the return value of the previous program to execute from the shell, so it is easy to tell if a program succeeds or fails: 报告从shell执行的前一个程序的返回值,因此很容易判断程序是成功还是失败:

$ false ; echo $?
1
$ true ; echo $?
0

This was chosen because there is a single case where a program succeeds but there could be dozens of reasons why a program fails -- by allowing there to be many different failure error codes, a program can determine why another program failed without having to parse output. 之所以选择这个是因为有一个程序成功的情况,但程序失败可能有很多原因 - 通过允许有许多不同的故障错误代码,程序可以确定为什么另一个程序失败而不必解析输出。

A concrete example is the aa-status program supplied with the AppArmor mandatory access control tool: 一个具体的例子是随AppArmor 强制访问控制工具提供的aa-status程序:

   Upon exiting, aa-status will set its return value to the
   following values:

   0   if apparmor is enabled and policy is loaded.

   1   if apparmor is not enabled/loaded.

   2   if apparmor is enabled but no policy is loaded.

   3   if the apparmor control files aren't available under
       /sys/kernel/security/.

   4   if the user running the script doesn't have enough
       privileges to read the apparmor control files.

(I'm sure there are more widely-spread programs with this behavior, but I know this one well. :) (我确信有更广泛传播的程序有这种行为,但我很清楚这一点。:)

I suspect it's just following the Linux / Unix standard for returning 0 on success . 我怀疑它只是遵循Linux / Unix标准 成功返回0

Does it really say "1" is false and "0" is true? 真的说“1”是假的而“0”是真的吗?

There's no good reason for 1 to be true and 0 to be false; 没有充分的理由让1为真, 0为假; that's just the way things have always been notated. 这就是事情总是被注意到的方式。 So from a logical perspective, the function in your API isn't "wrong", per se. 因此从逻辑角度来看,API中的函数本身并不“错误”。

That said, it's normally not advisable to work against the idioms of whatever language or framework you're using without a damn good reason to do so, so whoever wrote this function was probably pretty bone-headed, assuming it's not simply a bug. 这就是说,它通常不建议你使用任何语言或框架没有一个该死的好理由这样做的成语工作,所以谁写了这个功能可能是相当骨为首的,假设它不是简单的错误。

It may very well be a mistake on the original author, however the notion that 1 is true and 0 is false is not a universal concept. 这可能是原作者的一个错误,但是1是真,0是假的概念不是一个普遍的概念。 In shell scripting 0 is returned for success, and any other number for failure. 在shell脚本中,为了成功返回0,为失败返回任何其他数字。 In other languages such as Ruby, only nil and false are considered false, and any other value is considered true, so in Ruby both 1 and 0 would be considered true. 在其他语言(如Ruby)中,只有nil和false被认为是false,而任何其他值都被认为是真的,所以在Ruby中,1和0都被认为是真的。

Simple Answer简单的答案

0 = false 
1 = true

I'm not sure if I'm answering the question right, but here's a familiar example: 我不确定我是否正确回答了这个问题,但这是一个熟悉的例子:

The return type of GetLastError() in Windows is nonzero if there was an error, or zero otherwise. 如果出现错误,Windows中GetLastError()的返回类型为非零,否则为零。 The reverse is usually true of the return value of the function you called. 反之通常是您调用的函数的返回值。

Generally we think that 0 is false and 1 is true一般我们认为0为假1为真

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM