Bitwise OR

Following on from last week’s look at the bitwise AND operator, this week I’m going to look at bitwise OR and provide an example of how you can use it.

Bitwise OR is represented by the | symbol.

Can I see an example?

130 | 10 = 138

What is this actually doing?

Bitwise OR compares each bit setting and if either one is set, sets the corresponding output bit to 1. So in the example above, we can see the actual bit comparisons as follows:

10000010    //130
00001010    //10
________
10001010    //138

Note that it doesn’t matter if one or both bits are set in what we are comparing. As long as either one is set, the output bit is set to 1.

What would I use bitwise OR for?

OK, an example follows – it’s much easier to understand the usefulness of these operators when you see them in action.

Turning on an individual bit

Remember last week we used bitwise AND to turn off an individual bit? Well bitwise OR allows you to do the opposite. By using a mask with the bits set that we want to turn on, we can modify the individual bits in any byte.

Let’s say we have a byte, containing 8 bits, which are used as true/false values. Without changing the settings of any of the other bits, we want to set the first bit to be true. We apply the mask 1 (decimal) as follows:

11000100    // 196
00000001    // 1
________
11000101    // 197

The result is that the first bit (that is, the right-most bit) is now set to 1, and the other bit settings remain unchanged.

That’s all there is to it!

Next week we’ll look at bitwise XOR.

 

Bitwise AND

I’m going to spend the next few weeks taking a look at the bitwise operators (AND, NOT, OR, XOR), and how they are used in C/C++.

How often you are faced with problems that use these operators depends on the field you are working in, and the coding preferences of the people that have gone before you. It’s entirely possible to code in C++ for years and never need to know the ins and outs of bits in a byte.

The bitwise operators enable you to work at a very fine-grained level, which is advantageous when you are dealing with limited space, or sending messages with limited capacity.

So let’s get going.

Bitwise AND is represented by the & symbol.

Can I see an example?

128 & 10 = 0

What is this actually doing?

Bitwise AND compares each bit setting and if they match, sets the corresponding output bit to 1. So in the example above, we can see the actual bit comparisons as follows:

10000000    //128
00001010    //10
________
00000000    //0

What would I use bitwise AND for?

Good question. The above example seems totally counter-intuitive, and quite clearly would make you wonder what use this would ever actually have. Below are two examples of using the & operator that should enable you to see its usefulness more clearly.

1. Bit masking to check status

Let’s say I am using a byte to hold the true/false status of 8 items. I want to know if item 5 is true (set to 1). Since I can’t read bits individually, I can simply AND my byte with the appropriate mask and test the result. My mask in this case is decimal 16 (00010000).

01101011    //107
00010000    //16
________
00000000    //0

The result is 0 if bit 5 is not set.

01111011    //123
00010000    //16
________
00010000    //16

And the result is 16 (i.e. the value of the mask) if bit 5 is set.

2. Turning off an individual bit

Now let’s say that I want to turn off bit 5. I can do this with the mask 239  (11101111):

01111011    //123
11101111    //239
________

01101011    //107

Can you see what’s happened here? The bitwise AND comparison checks each bit and sets the result to 1 if they match. Therefore, if I want to turn off bit 5, all I need to do is set my mask to contain zero at this position. That way, bit 5 will never be set to 1 in the result. Setting everything else to 1 in the mask ensures that all other bits remain unchanged.

Note that you can’t turn a bit on with the AND operator, since you must have two values of 1 to get the output of 1.

Next week we’ll take a look at bitwise OR.

 

What is Encapsulation?

This is the final post in the series of the three cornerstones of object oriented programming. Next week I’m going back to a nice low-level subject – the bitwise operators :-).

So. What is encapsulation?

I previously defined this as the ability of objects* to maintain their internal workings independently from the program they are used in.

This is actually the simplest of the three concepts we’ve covered. With respect to C++, encapsulation describes the use of a class to contain both its relevant data, and the methods to manipulate its own data.

In practice

As a simple example, you might define a class called Message. This class can contain various data members, such as the sender’s address, the date the message was written, the destination address, and of course, the actual message inside.

The class can also contain methods to manipulate a Message object. For example it might have a method to set the object status to ‘read’, and another method to print the message to the screen.

The fact that the Message class both contains its own data, and provides a way to interact with that data, is what encapsulation is all about.

It’s such a fundamental part of C++ (and other OO languages), that it is often taken for granted.

Why is this a good thing?

The benefits of encapsulation are:

  • It can restrict access to an object’s data.

If data is declared as private and methods are generated to access that data, it can provide a good level of control over the data and how it is used.

  • It allows the programmer to modify the object under-the-hood without disrupting the rest of the program.

If you want to change the internals of your class, you can do so without having to modify code elsewhere in the program, as long as you keep the methods to access that class the same.

Encapsulation is the class not the object

Note that encapsulation is provided by the class, not the instantiated object (there is a subtle difference). This is because a class can provide the ability to access information about every object that is instantiated from it, whereas a single object is just that.

And that wraps up our mini-guide to OOP. Hope it was useful, and don’t forget to pop back soon for some fun with bitwise operators.

 

*In C++ note that is it officially the class that is the unit of encapsulation, not the individual object itself.