I've been thrown a bit of a poser, and I'm not sure what the answer is.
Basically - is there a convention for what values to use in tristate data-types? Doing some googling around, it doesn't look like there is: I've seen:
- -1 = False, 0 = Not known/undefined, +1 = True
- 0 = False, +1 = True, +2 = Not known/undefined
- -1 =Not known/undefined, 0 = False, +1 = True
..amongst others. I'd rather use a well-known convention if there is one. Otherwise I'll make one up :-) It may well be there is no right answer, but just thought I'd dig a bit deeper...
Edit
Found this one as well that Microsoft seem to use in recent code: -1 = true, 0 = false, 2 = not known. I assume having 2 == unkn开发者_运维知识库own means it removes the ambiguity over interpreting +1/-1 when just looking at the raw values in a debugger/dump/memory etc. Oddly enough, this option appeals just for this reason alone (removes chance of forgetting which variation of 1 means 'true').To my knowledge, there is no convention for that.
There isn't even a single convention for a boolean value, the most common are:
- 0 = False, -1 = True
- 0 = False, 1 = True
Another common (but not universal) convention that would be relevant is the usage of -1 for undefined values.
If you choose to use -1 for undefined, you could use 0/1 for False/True:
- -1 = Undefined
- 0 = False
- 1 = True
And naturally I have to mention that the third state should of course not be undefined/unknown, but FileNotFound. ;)
Regardless of the semantic meanings you attach, I would choose your three values as one negative number, one positive number, and zero. This allows optimal testing for membership in each possible 1- or 2-element subset of the 3 possible values with just a single test and branch (>0, <0, ==0, >=0, <=0, !=0).
I would say there's nothing like convention, but I would prefer the third case you've mentioned because I've seen this in use many times.
Value State
---------- ----------
-1 Undefined
0 False
+1 True
I wouldn't prefer the first two cases because pure boolean states are mostly determined as 0 = False
and 0 <> True
so it might be little confusing.
If you're looking for a substitute for a tri-state bool with int, you can always have the nullable boolean without having to worry about standards. Something like, in C# bool? triState
. It can have values true, false or null which can be considered as unknown - but I see that's not what you're looking for.
I don't know about convention. Most certainly not from the answers given here. I like your third one.
But a different perspective, my pick would be:
{ True, Unknown, False } => 0, 1, 2 // going by enum like values...
EDIT: After a good point by @LarsTech, I should clarify. That if I know there can be more than two values for a variable then I would think like as its an enum and hence I could translate it to { 0, 1, 2 }. I always follow a pattern for enum values like this : "Good, Alright, Poor". It comes naturally to me, but for many true having a "0" is not welcome. One can always have a reverse order.
精彩评论