Archive

Archive for November, 2011

Operator precedence

30.11.2011 Leave a comment

Recently I came a cross a bug where the author forgot to use parentheses in a conditional expression. The code went like this:

if (AAA &&
    BBB || CCC || DDD)
{
    /* ... */
}

The bug was obvious, because this is what the author really meant:

if (AAA && (BBB || CCC || DDD))

But this is not how compiler understood it.

There are so many operators, that it’s hard to remember their precedence. Not many people remember immediately whether | (bitwise or) operator has higher or lower precedence than & (bitwise and) or ^ (bitwise xor). Let alone the << and >> (bitwise shift) operators, which in C++ are used as stream operators, and have higher precedence than other bitwise operators. There are also other surprises, such as comparison operators having a higher precedence than bitwise and/or/xor operators.

These days its not uncommon to use more than one language in everyday life, especially various scripting languages come to mind. Many languages share the set of operators, but operator precedence may vary between languages, for example Python’s operator precedence is different than C’s.

All this results in errors when writing code and creates unnecessary maintenance problems.

There are several groups of operators which have obviously higher precedence than others. For example * (multiply) operator has higher precedence than + (add) operator, we were taught this on mathematics lessons in elementary school. Also it’s not a surprise than arithmetic operators have higher precedence than logical operators. But other combinations are ambiguous. Should bitwise operators have higher or lower precedence than arithmetic operators?

To avoid bugs and make the code easier to read for anybody who will be maintaining or extending it, it is a good practice to use parentheses. It’s good to have this rule in coding conventions for any project.

Languages should impose the usage of parentheses in ambiguous situations in their grammars. It is easy to define such grammar rules even in the simplest notations like BNF. For example such rule would forbid mixing various bitwise operators without parentheses or mixing arithmetic and bitwise operators, etc. This would help to avoid subtle bugs which are sometimes difficult to spot.

Categories: Computing

The shift in personal computing

6.11.2011 1 comment

Last month Samsung sold more smartphones than any other manufacturer. Apple is #2 and Nokia is #3. The predictions of some analysts last year that Android will take over are becoming true while Google is trying to make Android a more solid platform and fix some mistakes they’ve made.

It’s interesting to observe the battle between Apple and Google, with multiple hardware vendors involved on the Google side, while other well established cell phone and smartphone manufacturers begin to struggle.

But where is it all going? It looks like in the coming years small mobile devices will replace PCs. We will no longer be chained to a desk. We won’t need to carry around a bulky laptop. Instead we will be carrying a small touchscreen device. Take a look at some designs like the ones from Motorola, where you can take the smartphone and plug it into a bigger device. Your pocket device will become your universal personal computer. You will plug it into a dock, or just connect a monitor. You can do it today with some tablets. Most devices also already support Bluetooth keyboards. Android also supports a mouse. Future designs will simply become more convenient to use.

Mobile devices become more and more versatile. You can write and print documents using both mobile apps and online office suites, you can take, upload and edit photos – some online tools like Picasa provide basic, but easy to use photo editing capabilities, you can browse the Internet, watch movies, listen to music, play games – do most common tasks on a small device which fits in your pocket.

PCs will remain a niche for specialized uses, which require sophisticated software and high hardware specs. Because of that they will become more expensive, which will force even more users to stick with smaller devices.

This does not bode well for existing monopolies.

Categories: Computing