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 …)
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 …)
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.
For most contexts, 0 is considered FALSE. Any non-zero value is considered TRUE, though 1 is most often used.
I always remember that most languages are optimistic. So while only 0 = false, everything else = true.
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.
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.
In the Ruby programming language, 0 is not false, so beware if you start playing with it :)
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)
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)
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).
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.
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.
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"
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.
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).
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)
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)
You could note that Lua is similar to ruby in wich nil and false are false and everything else is true.
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).