- [Instructor] The Bitwise Boolean operators allow you to
perform binary operations on integer values.
This is B print dot CPP
from chapter four of the exercise files.
The purpose of this exercise file is simply to
provide a quick and portable way to display binary values.
The standard printf doesn't provide one.
So I wrote this simple u8 to bstring function
to convert an eight bit byte
to a binary string.
And so when I build and run this, you see
that I get this result in binary, I'm using an or here.
And so these bits ord with those bits looks like that.
And I'm able to print these integer values in binary
by virtue of this u8 to bstring function
which I wrote, for this purpose.
So let's take a look at the Bitwise binary operators.
First of all, we'll just look at not a,
and so a is the integer value five,
which is zero one zero one.
And we have the leading four zeros
because it's such a small number.
And when I build and run this with the not operator
you see it simply flips all of the bits.
So all of those zeros are now ones.
And one zero one is now zero one zero.
There's also a Bitwise or operators.
So I can say a or b.
And when I build and run, each of these bits are ord
with each other and there's also a Bitwise and operator.
And when I build and run that
you'll see that we get all zeros
because that's actually the result of one and zero,
zero and one.
If I change this five to a six
and we build and run, you see that we now get
an anded one there.
There's also a left shift and right shift operator.
So if I left shift a by one,
you see that instead of zero, zero, one, one, zero,
we get one, one, zero, zero,
and likewise I can right shift it by one.
And we get that proper result.
And you can make this any number.
If I right shifted by two
it simply drops one digit off the end.
And obviously if I right shift it by more than that
it drops all of the bits off the end.
And the same with left shift.
I left shift by three, we get that.
If I left shift by seven,
then it ought to just roll them all off the left side.
So that's the Bitwise left shift operator
and the Bitwise right shift operator.
We have an, we have or, we also have exclusive or
and that looks like this.
I want to bring this back to a five here.
And we'll notice that we get the same result as with
or because each of those bits are complimentary
to each other where there's a one here
there's a zero there.
But if I change this to a 15, that's where all
of these bits are on in the right-hand nibble of the bite.
Now you see that our, or looks like that
rather our exclusive or looks like that.
And yet the nonexclusive or will give us all ones.
So the Bitwise operators are available to
perform all the standard boolean arithmetic
and Bitwise operations for integer values.