3

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

Scott Dorman
  • 42,236
  • 12
  • 79
  • 110
Thierry Roy
  • 8,452
  • 10
  • 60
  • 84

11 Answers11

9

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

chaos
  • 122,029
  • 33
  • 303
  • 309
  • Indeed, note the "anything nonzero is truth"! Anything nonzero is usually interpreted as `true`. – onnodb Feb 16 '09 at 19:17
  • Ah, but in some languages, -1 is also true. How can you have negative truth? – Adam Davis Feb 16 '09 at 19:19
  • The same way you can have 27 or [1,5] or "falsehood\n" truth: magic. – chaos Feb 16 '09 at 21:20
  • Also: 'some languages'? Are there any languages that allow interpretation of an arbitrary value in boolean context where -1 *isn't* true? – chaos Feb 16 '09 at 21:22
  • "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 Feb 16 '09 at 23:11
  • Seems a little sheepish to me. – chaos Feb 16 '09 at 23:28
  • Would it blow your mind if I pointed out that this is not a mnemonic? :) – Lightness Races in Orbit Feb 20 '11 at 18:59
  • @Tomalak Geret'kal: I dunno, would it blow your mind to find out that "mnemonic device" means "trick for aiding memory" and is not actually limited to retarded nursery rhymes that form initialisms? – chaos Feb 20 '11 at 19:05
4

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

False = 0
True != 0

Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467
4

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
  • 3,198
  • 3
  • 25
  • 22
  • 1
    Never test for true or false using a comparison operator. That's redundant and sometimes error-prone. Use "if(my_var)" instead. – David Thornley Feb 16 '09 at 21:20
3

"Oh No!"
(Oh == 0)

abyx
  • 69,862
  • 18
  • 95
  • 117
3

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
  • 55,348
  • 14
  • 97
  • 151
3

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
  • 3,897
  • 6
  • 40
  • 42
  • 1
    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 Feb 16 '09 at 21:22
2

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

dwc
  • 24,196
  • 7
  • 44
  • 55
2

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

Adam Davis
  • 91,931
  • 60
  • 264
  • 330
  • 1
    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 Feb 16 '09 at 19:47
  • 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 Feb 16 '09 at 20:43
1

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
  • 399,467
  • 113
  • 570
  • 794
1

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.

jfs
  • 399,953
  • 195
  • 994
  • 1,670
0

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

Avram
  • 4,267
  • 33
  • 40