Bitwise Operators and Precedence

Posted in Uncategorized by Joseph Gordon on April 16, 2010

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?


Open Source Icon Woes

Posted in Programming Support by Joseph Gordon on March 1, 2010

Yay! I’ve finally taken all the necessary steps to start my own open source software. I’ve setup a “Space” for my project I call JUtils with a company called Assembla.

So far I’ve been nothing but impressed. If I had known it would be this easy then I would’ve done it a long time ago. Ok, so that’s not really true. The thing that’s held me back from releasing this for so long is really licensing. I’ve use the software libraries XStream and JGoodies in the project. Both of which have straightforward licensing. My problem has been icons.

I have little experience creating them and so did what any good programmer would do when their stuck and needing some Intellectual Property (IP). I googled for it :). This garnered a great many icons. Some of which I really liked. So I started using them and continued coding. Then came the point at which I’m ready to upload. So I go the locations I’ve unpacked the icon libraries and there’s no license information to be found. A little file that is included with most sourcecode that I’ve ever download is rarely (in my experience) distributed with icon sets.

So I scrap what I have and start over. I’ve found several that I really like:

Name License Type
CrystalClear LGPL
Flavour Extended Public Domain
Farm-Fresh Web Icons CC Attribution 3.0
Must Have Icon Set Public Domain
OpenIconLibrary CC Attribution-Share Alike 3.0

I was able to pick and choose out of these, the 27 icons I needed. I gathered all the licenses and displayed them, with the appropriate urls, in an About dialog in the software. I have no idea if this fulfills the licenses well enough or not. I also don’t know what sort of license I’m allowed to use for my applications or code. Any help here would be appreciated.

Tagged with: , , ,

That Stupid Old Windows Start Menu

Posted in Opinion by Joseph Gordon on November 29, 2009

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:

  1. People are idiots (or at least try really hard to act like them).
  2. 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:

  1. It primarily uses dynamic menu items
  2. The menu-items that are static are now one level deeper than they were.
  3. 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.

So now that I feel better, I can finally go to bed.
Duty Calls

Tagged with: ,