Bitwise Operators and Precedence
I learned something today. I did not enjoy it, but I learned nonetheless. The lesson is on operator precedence with respect to bitwise operators. In C#, consider about the following:
bool x = TestFlags(0x04); // What is x?
public bool TestFlags(uint flags)
{
return flags & 0x04 != 0;
}
Well, it’s a compile error, that’s what it is. This was completely surprising to me. Specifically, the error is:
Error 3 Operator '&' cannot be applied to operands of type 'uint' and 'bool'
Dude! Wait! What? That means that on one side of the ‘&’ is a uint and on the other, a bool. The only way I could see that happing is if the operator ‘!=’ has precedence over ‘&’. Surely that isn’t right! But au contraire, after some googling (Ah teh Google, how I love thee) I discovered that indeed, ‘!=’ has precedence over ‘&’! This fact disagreed completely with my mental model.
After much griping to my coworkers, I started wondering why this was the antithesis of my mental model. I think it’s because of the difference between the precedence of the operators ‘+’ and ‘&’ with respect to operator ‘!=’ or ‘==’. To clarify:
flags & 0x04 != 0 ≡ flags & (0x04 != 0) flags + 0x04 != 0 ≡ (flags + 0x04) != 0)
Right, wrong, or indifferent that dichotomy is why I was initially confused. I also wander, “How much C/C++ has a logical bug because of this subtlety?” Let’s take a look at that code again:
bool x = TestFlags(0x04); // What is x?
public bool TestFlags(uint flags)
{
return flags & 0x04 != 0;
}
With C/C++, it compiles without the need for parenthesizes and x is 0. Of course, in the C++ world an int is automatically cast to a bool and in a C world there is no difference. Therefore, there would be no need to add the ‘!=’ or ‘==’ to the statement. However, will the likelihood of seeing this faux pas increase as younger generations of programmers hit the streets with only higher level language experiences?
That Stupid Old Windows Start Menu
So after getting mad at my old computer for the last time, I thought we’d take advantage of the weekend sales and pick up a new system. So I’m finally am on the dual-core and 64-bit bandwagon. Yeah I hold on to old technology for way too long… But my Socket A Athlon 3000+ was a champ. Oh well, on to new adventures.
Adventures like Windows 7. Yeah. Old system = Ubuntu 9.04, new Dell = Windows 7. Little bit of a change there. Overall, though, I kinda like it. Even though I don’t like the default 64×64 icons on the desktop, it’s super-easy to change. (Because holding CTRL and scrolling while the desktop is selected is intuitive…)
One thing that I always did with XP systems (never have used Vista much) as soon as logged on for the first time was change the theme and the start menu to classic. I’ve decided to leave the theme in Windows 7, and it’s actually grown on me. After only 3 hours or so I actually like it.
The start menu however… Yeah… Truth is, I personally do not like the start menu now. Well, I didn’t like it in XP and the Windows 7 one isn’t too different. So after looking for a solution to my dilemma I stumbled across the incredibly helpful folks here. After spot-reading several posts through the thread, I’ve come to two conclusions:
- People are idiots (or at least try really hard to act like them).
- Windows followers are now more rabid than their Mac analogs.
So, rather than use the worst form of argument, dh0, and say how stupid, blah, blah, and blah, the start menu now looks, I would like to enumerate the functions that I simply do not appeal to me:
- It primarily uses dynamic menu items
- The menu-items that are static are now one level deeper than they were.
- The user is more than encouraged to search for menu-items.
What bothers me more that the horrid new menu, is the abysmal attitudes people have. I am beside myself to understand how folks can be so disparaging about someone simply because they hold an opinion contrary to their own. Look folks, it’s really quite simple:
- When someone asks a question, either answer the question or shut up and go away. Doing anything else is just trolling. Go write a blog about it if you feel so strongly. Trust me, you’ll feel better.
- Allow a person the right to hold a negative opinion about something. I’m not saying you should never try to persuade someone to your way of thinking, just don’t berate or belittle them whilst doing so. It really solves nothing.
Basically, just don’t use dh0 – dh2. If you haven’t read Paul Graham’s article, you should.

leave a comment