Skip to content

Bitwise Operations for Setting Options

I was programming for 2 years before I ran into these and even then I used them without knowing what they actually did or how they did it. Tutorials on this subject aren’t always useful when thinking about applying bitwise calculations in real situations. This is the first time I’ve used them in my work so I thought a post describing the wonderful things they can do would be a good thing to do. So here we are.

Bitwise Operations for Setting Options

In this example I have a layout class that manages the layout for my GUI components. It aligns them relative to other components or a component’s parent.

The options:

New syntax: << simply means shift all numbers in a binary left.

For example

1 << 2 shifts 1 by 2 places. So in binary 0 0 1 becomes 1 0 0.

So the first 7 options look like this:

Setting Options

Imagine we have an unsigned integer containing 8 bits (uint8). This will be our ‘flag’. We will use this to store the options. How do you set an option? Use the ‘|’ operator.

‘|’ compares two binary numbers and if each corresponding bit is ‘1’ OR either bit is ‘1’ then it will return ‘1’, otherwise ‘0’ is returned.

So to set an option in our flag:

Checking an Option

Nice and easy so far. So how do we check if an option is set? The ‘&’ operator compares two binary numbers and if both corresponding bit is ‘1’ it will return ‘1’, otherwise ‘0’ is returned.

Check if the LayoutConstraintAlignParentBottom option is set:

So the option isn’t set. How about LayoutConstraintAlignParentLeft?

Yes, here you can see the option is set.

Removing an option

Removing is a little more tricky. Again we use the ‘&’ operator. Have you noticed when we check an option, it returns ONLY the option we asked for. This sounds like a good start but we want all the options EXCEPT the one we want to remove. The ‘~’ operator switches all the values in a binary.

So if we apply ‘~’ to the option we want to remove we can then compare it with our flag using the ‘&’ operator. This will remove that option from the flag.

Notice how the correct option and only that option was removed. So a quicker way to write this:

Multiple Options

Now on to some cool stuff. Sometimes many options need to be checked or set. Hexadecimal numbers are very useful here. Lets look at our group of options again.

When the first four values are set to our flag, it looks like this;

If we want to check for ANY of the first four options we need compare this flag with each option. Here’s where hexadecimal numbers can come in handy. 0 0 0 0 1 1 1 1 == 0xF. So we can group these flags in another option. In the above example all of the alignToParent options are grouped in a single option (LayoutConstraintAlignParentFlags = 0xFF = 1 1 1 1 1 1 1 1).

So lets take a longer flag with 16 bits and see if any of the first 8 options are present.

There we are! A few wonderful things you can do with bitwise calculations.

Published iniOS

Be First to Comment

Leave a Reply