Using the Bitwise Operators
Now it is time to move on to the final operator that we will discuss in this guidebook. And these will be known as the bitwise operators.
This is a type of operator that we can use, and at first glance, it is going to seem like it is the same thing as the logical operators that we already talked about.
The main difference that you will see between these bitwise operators and the previous logical operators is that the bitwise options are going to take a value only if it is binary, and then turn it into something that is a Boolean result.
These are going to be values that will represent true or false, but they will come out as a 0 or 1, and then we can see these come as the output in the whole process.
These binary values are sometimes going to be difficult to work with overall because a lot of non-programmers will not understand how these are supposed to work in some of the codes that they want to write.
Let’s take a look at an example of some of the different bitwise operators that you are able to work with to see how they can be handled here.
For this, let’s assume that I = 0, h = 1, j =0 and g = 1
- “&” this is the operator that is known as the Bitwise AND.
It is going to assign 1 to the positions where both of the operands have 1.
For example, g & h will give you 1.
- “|” this is the bitwise OR.
It is going to assign 1 to the positions when there is at least one of the operands with a 1.
For instance, doing h | 1 will give you a result of 1.
- “^” this is the exclusive OR that will work well for binary data.
Just like working with the logical values, this kind of operator is only going to give you 1 in the areas that the operand has a 1.
For example, if you do g^j you will get a result of 1.
The bitwise operator is going to be a bit more work when it comes to the different operators that we are able to work with.
And there are not necessarily a lot of times when we would want to bring out this kind of operator in the codes that we are going to do.
But this is a good option to work with and know about so we can see how this is going to work when we do need it.
You will find that all of these categories of operators will be important to the codes that we want to write along the way.
They are all a bit different and are able to handle things in a different manner as you go, but they can still help you to get a lot done and see some good and powerful results in the codes that you want to write.
Take some time to study these and see how they work, so that you are better able to add them to your own codes quickly and efficiently.