# Boolean Operators...

• 10-01-2009
Nean
Boolean Operators...
Hey everyone, I'm just beginning to learn C++ and its just about the first programming language I've ever learned (Aside from VB6) and I have to say its quite intimidating, so I have a few questions about boolean operators, after reading tutorial 2 and was wondering if any of you could elaborate on them and give me a down to earth explanation.

How do boolean operators work? Are they constantly comparing to other numbers? I'm sure if someone could possibly post a little bit of code with these operators in work, I could figure it out, I'm just confused on how to actually use them... If anyone could elaborate, I'd deeply appreciate it.
• 10-01-2009
tabstop
The boolean operators are and, or, and not. They mean "and", "or", and "not" in roughly that order. (I.e., "not" means the opposite; "and" means this "and" that, etc.)
• 10-01-2009
King Mir
Boolean operator are just operators that work on bools. They also work on integers and pointers, where 0 and NULL work like false, and all other values work like true.

The boolean operators are && (AND), ||(OR) and !(NOT). You can also use != and == with bools, which function like XOR and NXOR.
• 10-04-2009
Elysia
Boolean operators work on anything that are boolean expressions. Boolean expressions is anything that can be expressed as true or false. That means they are not just limited to integers.
Example: MyString == "Hello World" is a boolean expression.
• 10-04-2009
StainedBlue
Where T means "true" and F means "false"

AND (&):
Both sides of the expression must be true to produce a "true" result.
(Both must = T)
T & T = T
T & F = F
F & T = F
F & F = F

OR (|):
Either side of the expression can be true to produce a "true" result.
(Both, or one must = T)
T | T = T
T | F = T
F | T = T
F | F = F

NOT (!):
The opposite.
!T = F
!F = T
• 10-04-2009
Elysia
Also, it should be easy to remember that they work just as in "real life".
If this and that is true, then the result must also be true, no?
If you have a negative sentence and then apply a word to negate it, then it would turn positive.

Example: I do not doubt this.
Doubt is usually negative by default, but not turns it positive.

Example: I want this OR that. So if someone gives you that or this (or both), then you accept it.
Example: You want this AND that. So if someone gives you just one, then you do not want it.
• 10-06-2009
rogster001
a slight aside..if we use bool data types should assignment be done using 'true' / 'false' or is it ok to use 1 / 0 /null, is this immutable anyway and just best to keep to same throughout for clarity?
• 10-06-2009
Elysia
You can assign anything that is a boolean expression, ie something that can be represented with true or false.
0 and 1 are boolean expressions so they work fine. But there is no /null.
• 10-06-2009
laserlight
Quote:

Originally Posted by rogster001
if we use bool data types should assignment be done using 'true' / 'false' or is it ok to use 1 / 0 /null, is this immutable anyway and just best to keep to same throughout for clarity?

If you want to explicitly assign a boolean value to a bool variable then use true or false. It is certainly okay to use 1 or 0 instead, but it does not take advantage of what the introduction of bool in C++ offers. null is neither a keyword nor an identifier from the standard library, but you are probably talking about NULL from <cstddef>, which is typically just 0 (and definitely is equal to 0).