Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

1 = false and 0 = true?

Tags:

c

logic

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.

My questions - are there upside down worlds / parallel universes / coding languages where this logical NOTing is normal? Isn't 1 usually true? Is the coder of the API making an error?

like image 390
Ben Avatar asked Jun 25 '11 01:06

Ben


People also ask

Is 0 true or false in?

The number 0 is considered to be false and all other numbers are considered to be true....

Is 0 True or false in math?

The answer is False.

Is 1 means true or false?

In programming languages value of True is considered as 1. whereas false is zero. therefore In Boolean algebra True + False=1+0=1.

Is a boolean 1 or 0?

Boolean values and operationsConstant true is 1 and constant false is 0. It is considered good practice, though, to write true and false in your program for boolean values rather than 1 and 0.


2 Answers

This upside-down-world is common with process error returns. The shell variable $? 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:

$ 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:

   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. :)

like image 20
sarnold Avatar answered Sep 17 '22 03:09

sarnold


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". strcmp() and memcmp() work like this.

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. 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).

like image 71
caf Avatar answered Sep 17 '22 03:09

caf