Now let's use some of the bitwise operators.
The first four bitwise operators that we'll look at, not, and, or, and
xor, are closely related to the logical operators we've seen before
that operate on bool values.
The difference being that bitwise operators manipulate bits, 1s and 0s,
and the logical operators manipulate bool values, true and false.
The first bitwise operator is the not or invert operator.
This operator takes each binary digit, also called a bit,
and it flips its value.
So if we look at this value, we have four 0s, followed by four 1s.
And when we use the binary not operator, it flips these bits.
And the new value will be equal to this, where all the 0s have become 1s and
all the 1s have become 0s.
Now you'll notice that the values on the right still show the base ten
representation of the number,
while their base two equivalent is shown here in the comment.
And it's just important to remember that almost any value, actually I'd say
every value on a computer, at some point can reduce down to zeros and ones.
Now for us it makes a whole lot more sense to look at numbers in base ten
than it does in binary, but at one time that was the way we programmed.
The next bitwise operator is AND.
When we use Bitwise AND, it goes bit by bit through two numbers, and
it produces a one if both bits are one, or zero otherwise.
If you want to relate the bitwise AND Operator to the logical and operator,
we'll want to imagine that each bit is a truth value where zero is false and
one is true.
And if we think of them that way, then we can construct a truth statement.
For example, if it's raining outside and
it's cold outside, then I should wear a heavy jacket.
Let's make our first value here,
which is a 0 that represents false pertain to the isRaining truth value.
So it's false that it's raining outside.
Then let's make our second value here, which is a 1,
which represents true, be part of the truth statement as well.
So, it's true that it is cold outside.
Now because both statements aren't true,
then it's not true that we need to wear a heavy jacket outside.
It's not raining.
It is cold, but because it's not raining and it's cold at the same time,
then we don't need to wear a heavy jacket.
And the 0 that we see here in the result is equivalent to false.
And we would do this process for
each bit in both of the operands until we get our full result.
The same process happens for each bit.
I've added some space before this first number here so
that we can see each bit on top of one another.
So I'm going to fill these in one by one as we perform an AND
operation on each pair of bits.
So for the first we have 0 and 1, that would be 0.
For the second one we have 1 and 1, that would be 1.
And then I'll continue filling this out until we get our final number.
And I'll print out the number just as a sanity check.
And we get the same number here as we do when we're actually using the AND
operator.
Now, the bitwise OR operator works in a similar way.
For each pair of bits, if the first bit or
the second bit is one, then that results in a one.
Otherwise, it results in a zero.
So we can work our way through this one bit at a time, and
we get this binary number.
And if we print it out, we get 239 again.
Next let's look at XOR.
XOR is closely related to the logical not equal to operator.
So for each pair of bits, if the bits are not equal to each other,
or they don't match, then it results in a one.
Otherwise it results as zero.
So for this first pair of bits we have 1 and 1, 1 and 1.
And those are equal to each other so we would result in a false.
We want them to be not equal to each other.
And I'll continue doing this for the rest of the bits.
And we get a result of 90 if we go through each bit,
and this matches our other result.
I'm going to go ahead and skip to the example.
But if you want to learn more about these bitwise operators,
like the left shift operator or
the right shift operator, then check out the links provided in the playground.