(upbeat music)
- [Bill] Boolean logical operators are used
for testing logical conditions.
There is a Boolean type in C++, and it's called bool.
And so we'll create a couple of variables here,
a will be true and b will be false.
And we can print these values using printf,
and treating them as if they were integers.
And when I build and run this,
you'll notice that I get 1 for a,
because true is 1 and false is 0.
So b will be false.
And in fact, if I were to assign a different value to a,
it's still will print as 1
because it's a Boolean type and any non-0 value is true,
and true is 1 in the Boolean type.
The Boolean type is generally implemented
as the smallest possible size of an integer
which is usually 8 bits on most systems.
So if I try to print the size of it,
size is percent as is %zd,
and use the size of operator on a,
and I'll multiply it by eight to get bits,
and we'll build and run this
you see size is 8 bits.
So on this machine with this compiler,
a Boolean variable is 8 bits,
and it may or may not be different on your computer.
But generally speaking, try to implement it
as the smallest possible size of an integer.
Now C++ has three Boolean logical operators.
And so I can say if a AND b,
so the double ampersand is a logical AND,
then I puts true,
and otherwise will puts false.
When I build and run, we notice that it says false
because the expression a AND b is not true.
So the result is false.
So there's AND, there's OR which is two vertical bars.
And if I build and run that,
we get true because a OR b is true,
because a is true, if I just say a it's true,
there's a NOT operator,
which is an exclamation point or a bang.
And if I build and run that, it says false.
Now, Boolean operators,
the operands to the operators can be the result
of any logical expression.
So I can say 42 is greater than five AND b,
and this will be false because b is false.
And if I say AND a, I'll get true because a is true,
and so both sides of the logical AND expression are true.
42 is greater than five, that's true.
And a is true.
Or I can combine them,
I can say NOT a, and now it's false.
The AND and OR operations perform
what's called a short circuiting evaluation.
And this is useful to know in circumstances
where the evaluation of an operand is significant.
And so if I say a OR b like this,
the B will never be evaluated because a is true,
and it doesn't matter whether b is true
because with the OR expression,
the result is going to be the same either way.
So when I build and run this,
this is true and b is never actually evaluated.
And so the same is true if I say b AND a.
Because b is false, doesn't matter if a is true or not,
so it's never evaluated.
Some people do notice that
there's no Boolean exclusive OR operator.
An XOR operator would evaluate true,
if one and only one of the two operators was true.
There's several reasons for this omission.
According to Dennis Ritchie,
one of the original designers of the C language,
the XOR operator would be superfluous.
And because it cannot be short-circuited,
it would not work like the AND and the OR operators.
The XOR is effectively the same as the not-equals.
If I say b is not-equal to a, that's only going to be true
if one of them is true, and 1 of them is false,
which is the same as the XOR condition.
So I can evaluate that and I get true.
Or for circumstances where the operands may not be
a Boolean type, you can use the NOT operator
in front of each of the operands,
and that will force them into a Boolean condition
and you get the same result.
So the logical operators are simple,
they're straightforward,
they operate on logical conditions,
and they may be used for logically combining conditions.
(upbeat music)