views:

21455

answers:

19

I always forget :S
How do you remember which number stands for TRUE or FALSE?

(when I started css the colors black and white always confused me. Is white #FFFFFF or #000000. A trick I came up with: black is 0,because z0rr0 is dressed in …)

+2  A: 

The convention is always that 0 is false, anything else is true whether that be 1 = true, or -1 (ie the signed int 0xffffffff) = true.

So, just remember 0 = false, or false is nothing.

gbjbaanb
A: 

For most contexts, 0 is considered FALSE. Any non-zero value is considered TRUE, though 1 is most often used.

Jimmy
1 is not most often used. You will fall into many traps thinging like that. ture == !false is a better way to think about it.
Martin York
+5  A: 

I always remember that most languages are optimistic. So while only 0 = false, everything else = true.

Rick
+40  A: 

FALSE is 0 because there is nothing. TRUE is anything non-zero, because there is SOMETHING. (And, yes, TRUE is often defined as -1 in many languages; and generally anything non-0 is considered to be TRUE).

As for colors - 0 is black because (guess what) - there is nothing. It's dark. No photons coming out. #FFFFFF is white, because white contains all colors.

Vilx-
Tell that a printer, where black contains all colors :-)
Joey
@Joey: The old additive vs subtractive colour mixing problem again
Callum Rogers
+1  A: 

Another easy way to remember it is by the power button on some computers, especially olders ones: 0 means there is no power flowing/off because there is a gap; 1 means there is power flowing/on because there isn't a gap.

Darryl Hein
Or one might think of a transistor...
Chris Conway
+6  A: 

In the Ruby programming language, 0 is not false, so beware if you start playing with it :)

Sebastien Benmbarek
Really? What is false?
Shahin
false and nil are "false", everything else is "true"
Ferruccio
@Ferruccio, that should be stated in the answer then. (@ you only because you could edit the question)
he_the_great
+17  A: 

True is True and False is False.

The representaion of those values as a number is simply an implementation detail that your language / compiler cares about. Its a fools game to start relying on or fiddling around with your languages implementation details for anything, including bool types (enums fit into this category as well)

Tim Jarvis
+14  A: 

My undergrad C professor had a nice way to remember this:
"someone who lies (false) is a nobody (0)"

I guess it sounds better in the original language (Hebrew)

shoosh
I guess it's weird. I had never even thought to justify this convention. I never really questioned it.
BobbyShaftoe
A: 

I learned programming and digital logic at the same time, so the association has always been there for me. I can't say I've ever forgotten it. Remembering it as on/off may help. It probably also helps to learn boolean algebra, since it's basically just like normal algebra except 0 = false and 1 = true (also, no values are allowed to be any other number).

rmeador
+5  A: 

One of the nice things about C99 is that you can

#include <stdbool.h>

and get a predfined bool type as well as predefined true and false.

Norman Ramsey
STL is always a good idea. I don't want to see #define TRUE 1 again.
jim
A: 

It's context-specific, so there's no universal answer to this question.

In my opinion, the question doesn't even make sense: integers and booleans are different domains; there's no reason there should be a universal mapping between them. If you have a need for such a mapping, there's a problem in your programming model.

Jay Bazuzi
+63  A: 

It depends on who you ask - in Shell scripting, 0 is true and anything else indicates false. In C, 0 is false, and anything else is true. In Ruby, nil and false are false and everything else is true.

So, in summary, your question can be answered by "look it up for whatever you're doing"

Paul Betts
+1 because I didn't know that.
Dean
+1 for showing examples, and pointing out the diversity in languages. I'm always suspicious of one-size-fits-all answers.
Ken Paul
@Ken, +1 to you for voting up for the same reason I would
Nathan Fellman
I thought I should note that in Bash, it really isn't true vs false, it is more error versus not error.
he_the_great
Joachim Sauer
In Church encodings for untyped lambda calculus: 0 and false are identical (\xy.y). In Python: 0, "", and [] all evaluate as false. In Lua: 0, "", and {} all eval as true, only nil (something like Python's None, but it's also the value of uninitialized variables, and of missing table elements) and false are false. In Scheme, 0 and "" and '() are again all true. There is wide variation in languages. Unix shells are the only context I know of where *0 is true and other numbers are false*. But then, even in the C standard library, 0 sometimes means no error, other times means there was an error.
profjim
And BASIC actually had 0 for false and −1 for true; stemming from that bitwise and boolean operators were the same :-)
Joey
A: 

Depends on the language, but let me give you a hint for Python.

As you know, zero is identity for addition (n + 0 = n for any n) and zero element for multiplication (n x 0 = 0 for any n).

Now, you must realize that OR, AND and NOT are Boolean counterparts of "ordinary" algebraic operations, addition, multiplication and negation, respectively. But Boolean algebra doesn't have 0 - what value is identity for OR and zero for AND?

It's false. false OR n = n, false AND n = false, for any n.

Python extends this logic to collections as well, taking concatenation as the counterpart to addition (n + [] = n for any list n). So, empty strings, dictionaries and lists mean false as well. It's not the purest model, but it's pretty useful.

A: 

Relative to Python:

I enjoy mathematics, so I'm fairly booked up on the mathematical idea that "zero is not a number."

Ergo, I thought of it as "numbers" are true and "not a number" is false. Which simplifies into "is something there?" and then just becomes another programming convention.

To keep white and black separate in RGB values, I just pretend that the numbers are how much electricity I want to devote to that color, so #000000 translates to "leave it off" (black).

J.T. Hurley
>on the mathematical idea that "zero is not a number."Where does that come from?
recursive
+2  A: 

it only snows when its #FFFFFFreezing - makes it easy to remember in the northeast US. might not work so good at lower latitudes.

A: 

In shell scripting, I wouldn't necessarily say that 0 = true, but rather that for most OS's, the execution of a program is expected to return an error status integer, which will be 0 if the program completed successfully, and a nonzero error code otherwise.

I have done this when I wasn't sure what integer value (1, -1, etc....) a new language used for TRUE (pseudocode):

Let my_FALSE := 0

Let my_TRUE := not(my_FALSE)

Print my_TRUE, my_FALSE, not(not(my_FALSE))

In most cases if you got FALSE right, the first and third numbers will be the same.

A long time ago, some languages made it interesting to do stuff like this, but it doesn't always work.

Let my_TRUE := (0 == 1 - 1)
Let my_FALSE := (1 == 5)
gsarnold
+13  A: 

I always find the following IRC conversation useful:

(morganj): 0 is false and 1 is true, correct?
(alec_eso): 1, morganj
(morganj): bastard.

(from bash.org)

Nader Shirazie
+1, I logged in only to vote this up!
Zaki
+1  A: 

You could note that Lua is similar to ruby in wich nil and false are false and everything else is true.

Isaac Remuant
A: 

Imagine you have a wire. You are asking yourself: Is there a current flowing in the wire?

You go and measure the voltage. If the result is zero than there no current flowing (False). Otherwise there's a current flowing in the wire (True).

Christian