Triad values ​​- is there an agreement?

I was thrown a little call sign, and I'm not sure what the answer is.

Basically - is there an agreement on which values ​​to use in three-dimensional data types? Performing some searches, this does not look like this: I saw:

  • -1 = False, 0 = Unknown / undefined, +1 = True
  • 0 = False, +1 = True, +2 = Unknown / undefined
  • -1 = Unknown / undefined, 0 = False, +1 = True

.. among the others. I would rather use a well-known convention, if any. Otherwise, I will do the following :-) Maybe there is no right answer, but just thought that I was digging a little deeper ...

Edit
I found this one that Microsoft seems to use in the last code: -1 = true, 0 = false, 2 = unknown. I assume that 2 == unknown means that it removes the ambiguity in interpreting + 1 / -1 when it just looks at the raw values ​​in the debugger / dump / memory, etc. Oddly enough, this option applies only for this reason (deletes forgetting which change 1 means "true").

+6
source share
4 answers

As far as I know, there is no agreement for this.

There is not a single convention for boolean values, the most common are:

  • 0 = False, -1 = True
  • 0 = False, 1 = True

Another common (but not universal) convention that would be relevant is to use -1 values ​​for undefined.

If you decide to use -1 for undefined, you can use 0/1 for False / True:

  • -1 = Undefined
  • 0 = false
  • 1 = True

And, of course, I have to mention that the third state should, of course, not be undefined / unknown, but FileNotFound .;)

+3
source

Regardless of the meaning you attach, I would choose your three values ​​as one negative number, one positive number, and zero. This allows you to optimally test the membership in each possible 1- or 2-element subset of the three possible values ​​with only one test and branch (> 0, <0, == 0,> = 0, <= 0,! = 0).

+3
source

I would say that there is nothing like a convention, but I would prefer the third case that you spoke about, because I have seen this in use many times.

Value State ---------- ---------- -1 Undefined 0 False +1 True 

I would not prefer the first two cases, because pure Boolean states are basically defined as 0 = False and 0 <> True , so this can be a bit confusing.

+1
source

If you are looking for a replacement for tri-state bool with int, you can always have a nullable boolean without worrying about standards. Something like in C # bool? triState bool? triState . It may be true, false, or null, which may be considered unknown, but I see that not what you are looking for.

I do not know about the convention. Most likely not from the answers given here. I like your third.

But another perspective, my choice:

 { True, Unknown, False } => 0, 1, 2 // going by enum like values... 

EDIT: After a good point from @LarsTech, I have to clarify. If I know that a variable can have more than two values, I would think that it is an enumeration of it, and therefore I could translate it to {0, 1, 2}. I always follow the pattern for enumeration values ​​like this: "Good, good, poor." This is natural for me, but for many, true β€œ0” is not welcome. You can always have the reverse order.

0
source

Source: https://habr.com/ru/post/889943/


All Articles