views:

338

answers:

11

I guess this is trivial for most of good1 programmers, but I'm so used to programming using true and false2 that when I encounter 0 and 1, I can never remember which one means true and which one means false.

Any suggestions?

1Good: I mean one who knows C, of course :)
2I am a Java developer, as you have guessed ;)

+1  A: 

Map both to On and Off. I think most programmers would map both sets the same way: 1/true both going to 'On', while 0/false both go to 'Off'.

Joel Coehoorn
+9  A: 

The mnemonic is "how much truth is in this?" Zero integer means zero truth. Anything else is nonzero truth. :)

chaos
Indeed, note the "anything nonzero is truth"! Anything nonzero is usually interpreted as `true`.
onnodb
Ah, but in some languages, -1 is also true. How can you have negative truth?
Adam Davis
The same way you can have 27 or [1,5] or "falsehood\n" truth: magic.
chaos
Also: 'some languages'? Are there any languages that allow interpretation of an arbitrary value in boolean context where -1 *isn't* true?
chaos
"At least one sheep in Scotland is black on one side" - I don't know every single language, therefore I cannot say "every" or even "most" so some is the safest term.
Adam Davis
Seems a little sheepish to me.
chaos
+2  A: 

Remember "nothing == false", "something == true"

dwc
+2  A: 

No mnemonic - and it gets even more complex if you come from a hardware background. But for programmers, just ask the question:

Is any bit set?

The answer is either true or false, and is the result. Only 0 (even in signed integers) has no bits set.

Adam Davis
Except for the old ones-compliment machines! I remember tracing a bug on a CDC-Cyber mainframe in school when ALL the conditions were true making all the bits 1 which is the 2nd representation for 0!
n8wrl
Ouch! One's complement machines leave me shivering. On the other hand, I never did like how unbalanced twos complement is - how the negative numbers always got one more than the positive numbers. It's like a tug of war where the bad guys always win by just a little bit...
Adam Davis
+4  A: 

I have a co-worker who simply has a Post-It note on his wall beside his desk:

False = 0
True != 0

Rob Kennedy
+2  A: 

"Oh No!"
(Oh == 0)

abyx
+2  A: 

This is complicated by the fact that in the shell (sh, bash) true is 0 and false is 1:

$ true
$ echo $?
0
$ false
$ echo $?
1
starblue
that's because there's only one 0 and only one successful outcome, but there are as possible errors as there are positive return codes :)
hop
in shells i don't think of return codes as true/false, rather as error code. obviously 0 means no error.
Javier
They can be used in conditional statements, so they are as boolean as are integers in C.
starblue
A: 

love c - because you can't multiply lies. :)

Avram
+3  A: 

If you are really having that much trouble with it, I would use the language to abstract it away.

e.g. in C

#define TRUE 1
#define FALSE 0

In general I would avoid having constants lying around in code anyways.

Consider,

if(my_var == TRUE)

as opposed to,

if(my_var == 1)

Though, here again you need to make sure you are testing for the right thing,

if(my_var != FALSE)

will catch more cases.

Cheers!

Christian

tremoloqui
Never test for true or false using a comparison operator. That's redundant and sometimes error-prone. Use "if(my_var)" instead.
David Thornley
+3  A: 

Haven't you ever noticed that everyday items' power switches use a circle for off, and a line for on?

It's not much of a jump to link them up.

Off = circle = zero = false

On = line = one = true

Imbue
Yeah, use something like a closed circuit to indicate off, and something like an open circuit to indicate on. Who thought up that standard?
David Thornley
Computer scientists. :)
chaos
A: 

It depends on the language.

  • C: ( 0 ? "never happens" : "false") and ( 1 ? "true" : "never happens")

  • Ruby, ELisp: both 0 and 1 are true

  • bash (or cmd.exe): the true command (from coreutils) exits with a 0 status code and the false command exits with a non-zero status code

Many modern popular programming languages have strong C heritage therefore they consider 0 to be false and 1 (or any non-zero numbers) to be true.

Don't use mnemonics for boolean, use your language's idioms to test trueness.

J.F. Sebastian