Code.Fu: Quickly testing if a positive integer is a power of 2.

If you are a graphics programmer you will often need to test if an integer is a power of 2. To do this you can exploit a neat trick using binary arithmetic. This trick will work with all positive integers.

Lets say the number you are testing is 4. Take a look at the simple binary arithmetic below:

```
4 --> 0100‬ (binary)
- 1 --> 0001
------------------
3 --> 0011

4 --> 0100
& 3 --> 0011
------------------
0 --> 0000

```

In short, you subtract 1 from the number and do a binary ‘AND’ of the result and the original number. If the number is a power of two, the result you get will always be 0.  The only exception is 0 itself, which will return 0 in the above operation. Depending on your application you may or may not want that.

C/C++:

```#define bool int /*remove this line for C++*/

bool is_powof_2(int x)
{
return ((x & (x-1)) == 0);
}

bool is_powof_2_excl_0(int x)
{
return (x != 0) && ((x & (x-1)) == 0);
}
```

Python:

```def is_powof_2(x):
return ((x & (x-1)) == 0)

def is_powof_2_excl_0(x):
return (x != 0) and ((x & (x-1)) == 0)
```

Mail for Windows 10 – Pretty but poorly designed.

I upgraded to Windows 10 a month back. Yeah I know it’s late, but hey, it’s difficult to switch in the middle of a large project. Windows 10 is probably the best windows version since XP, at least from a programmer’s point of view. Oh yes, thank you for bringing back the start menu! That Windows 8 Start Page thingy was really annoying and was the single thing which prevented me from upgrading. The other reason being, no DirectX 12 for Windows 7. Having said that, probably the worst thing on Windows 10 is the Mail application. I am using it for a month now and with each passing day, I get annoyed at it a bit more. Yes I know it’s pretty, but it’s poorly designed. I don’t know what the exact reason was, but by the looks of it every attempt was made to simplify the interface. Well, simplicity is fine, but not when it comes at the cost of efficiency or for that matter legibility. A wise man once said, “Things should be made as simple as possible, not not any simpler.”

Some basic UI mistakes seem to have been made in the app. The mail writer can be confusing to understand with little separation between the mail fields and the message. A small line separation in-between would have been great. It’s get slow as turtle – walking on top of a turtle when a email thread gets large. The incoming-outgoing mail list tree is just a mess with little or no options, no way to resize, no way to switch between list, no quick sorting header, and many more things that I just can’t believe were all taken out. This may work for a phone app, but for a desktop, the mail for windows 10 just doesn’t measure up. One more thing — the icons are just ridiculously big and so are the fonts for the UI in general. They may look good on a phone, but are unnecessary for the desktop. All this could have been tolerated had it not been for the ridiculous bugs in the software. Mails not syncing right, sent mails disappearing, poor IMAP syncing, are just some of them. All this from a company that makes Outlook!

So, for me it’s back to Thunderbird, my good old faithful mail app. It’s been there with me ever since I can remember. Old is apparently gold!

When s#!t happens!

It’s painful when your graphics card dies out, but even more so when it dies out not because you pushed it too hard (overclocked it), but because of a power-surge. I had a trusty HD 4870, and though it wasn’t the newest of the cards it did serve me well for over 4 years now and could still push most games I play at decent framerates. Plus and more importantly, it worked very well for all my current graphics needs. I mostly target DX-9 to DX-10 level hardware and the 4870 was more than enough for that task. Sigh! … it’s dead now!

It so happened that while I was away one weekend, an electrical fault caused current to leak into the earthing terminal (probably busted earthing of the power co.). Since the earthing terminals of most electrical equipment (including most PCs) don’t break when you turn off the switch, the current flowed into my PC and destroyed one of the PSU caps. As a result PCIe supply of PSU shorted out killing the graphics card. Fortunately (and thank God) no data was lost and the HDDs seem quite OK when I ran tests on them.

The only option left was to get a new card and I opted for the R9 270X. No point in going for an older card now that the R9s are DX-12 compatible. Unfortunately The R9s don’t play well with older MoBos and … so I had to get a new MoBo with a new CPU, and a new PSU, with a new cabinet and basically build a new dev machine from scratch, not to mention another 20-hour dev setup after installing a new OS.

Well to anyone who is reading this, my advise is to at the very least have a stabilizer for your PC. A UPS can go a long way in preventing such a thing and the most important thing is to have a good PSU. Go for the known brands – Corsair, Cooler Master, Antec, etc.

The Hobbit – The Battle Of The Five Armies in 4DX.

The Hobbit: The Battle Of The Five Armies

Fourteen years is a long time, but the last and final chapter of the Hobbit trilogy The Battle of the Five Armies brings to conclusion an incredibly brilliant and successful series of the films (6 in total, The Lord of the Rings Trilogy and The Hobbit Trilogy), all of which were a truly remarkable experience. I am a fan of J.R.R Tolkien, a masterful story teller and a great writer — and of Peter Jackson who has been equally remarkable at bringing to life Tolkien’s Middle Earth in all it’s grandeur!

The third Hobbit movie starts off where the previous one (“The Desolation of Smaug”) ends and without giving out too many spoilers, let me just say that the start of the movie is not something to miss out on. The special effects, like in the previous films were, are beautiful.  Martin Freeman (Bilbo), Ian McKellen (Gandalf), Richard Armitage (Thorin) play their parts to perfection. The performance of the rest of the cast is equally impressive. Martin (Bilbo) has been particularly successful at growing the character of Bilbo from an unsure, uncaring, fumbling little hobbit to someone whose council is heeded to even by an egotistic King Thranduil (Lee Pace). If I had ever pictured a hobbit (Bilbo) while reading the “The Hobbit”, Martin would have been him.  Kudos to him for keeping the soul of Tolkien’s work alive though-out the trilogy.

Martin as Bilbo

For The Battle of the Five armies I got an invite to experience the movie in 4DX thanks to Cinepolis (India). 4DX is a new technology that Cinepolis claims, enhances your movie experience by adding an extra dimension of realism. The movie was released in 3D just like the last two hobbit films were, but the 3D experience was enhanced further by adding multi-sensory motion and environment effects which the company calls “4DX”. 4D as in “an extra dimension along with 3D”. In plain English, it means adding movement, weather, lighting, scent effects which are supposed to make you feel like you are right there — living inside the movie. To achieve this experience, your chair is augmented with motion where it yaws, sways and shakes to mimic the motion of the camera. A scent spray is located in front of your chair to send out a stream of scent effects like sweet smells of flowers, or the smell of fresh grass in an open meadow. A blast of cold air is sent when the camera pans over snow covered peaks and streaks of simulated lightning are displayed out across the ceiling to represent thunder & lightning appearing on the screen.

Was the 4DX experience worth it? For the most part I would say Yes. Again… without giving away spoilers let me say that the start of the movie is probably where I enjoyed the effects the most, followed by the glacial effects (cold air breathing down my neck) and finally the meadows of “The Shire” where the flowery smell of the environment was unmistakable. To be fair though I have to say that the technology is new and some chinks remain. There were sections of the movie where effects were misplaced and some effects felt preempted, defeating the suspense of the action that was about to ensue. A scene where Bilbo was just walking down to an injured “friend” was accompanied by shaking of the chair which did not augment the emotion of the scene. However, these were (according to me) only minor creases that need to be ironed out to make 4DX a truly wholesome experience. I would still vouch for 4DX since it tickles those senses which traditionally remained dormant while watching a movie. As for the final question… Did I enjoy it? … Oh! Absolutely I did! :))

As far as the movie goes I would say that it really did feel like an end of a remarkable experience.The Battle of the Five armies is right up there with all other Tolkien-Jackson movies, well worth the praise the series is know for. For me, watching the last movie in 4DX was the proverbial icing on the cake! To quote King Theoden (figuratively)  “If this is to be the end, then I would have such an end, as to be worthy of remembrance.” — and the last of the Hobbit movie was exactly that.

Visual Studio Community Edition (Free).

Microsoft has released free Visual Studio Community Edition. It’s basically a free full featured Visual Studio IDE (apparently with everything included) that can be used to make all kinds of apps. I was even surprised to find support for Python and Git included. Even more surprising was the Apple and Android logos in the “supported platforms” section!

Yes I am Alive!

WOW!!! it’s been a very long time, and yes, I am alive and well 🙂 . It’s just that I had a lot on my hands lately and I haven’t been able to pay any attention to the blog. Hopefully I will be updating the blog more often from now on.

Visual Studio 2010 still too slow!

UPDATE (Nov 2014): Given how many hits this and the Speeding up Visual Studio post are generating, I have to warn you that VS 2010 is seriously old. With the new Visual Studio version you can still compile for XP and there is no point in continuing to use VS 2010 anymore, unless you are already using it — and pushing a deadline. Please read the last section of this post to find the links to the free and commercial version of VS.

(Jan 2011) : Awe sh*t! After 2 months of active use I can say for sure, Visual Studio has some serious problems with speed. I didn’t have the IDE crash on me, but it’s just too slow for any large project. Tried everything possible, cleaned out the cache, recreated the intellisense files but the program still keeps slowing down for no apparent reason. It’s really annoying when the program suddenly gets into a heavy disk access mode, to the point where even typing becomes impossible. I have racked my brains and fiddled with every possible tweak I could find on the web without success. Since our entire project is now moved over to VS 10 it’s too late to turn back now 🙁 !!

UPDATE:  I finally managed to fix Visual Studio 10. Please read the post Speeding up Visual Studio.

Update (Not really): Neither me nor most programmers I know who are using VS 10 could solve this issue satisfactorily. I would recommend moving projects to Visual Studio 2012. It is much better and more stable than VS 2010. It’s been out for a while now and there’s no point in continuing with VS 2010 that clearly has some issues with speed and disk access.

Visual Studio 2012 Express : http://www.microsoft.com/visualstudio/eng/products/visual-studio-express-products

It’s time to move on to the latest version of Visual Studio or if you are an Indie/Hobbyist then to the free Visual Studio Community Edition.

A Happy New Year.

A Happy New Year to all of you.

It’s been a rather slow and uneventful 2010, but  a lot more exiting 2011 (I hope) 😀 .

A decade as a software engineer.

Me in 2000

• Favorite Programming Languages – C, C++.
• Working with (programming languages) – C++.
• Working with (platforms) – Win32, Linux.
• Working on – Finance Software, Stock Market (software), Futures and Options (software).
• Programming Languages – Pascal/Delphi, C, C++.
• Experimenting with – OpenGL, 3D Graphics, Client Sever communication.

Me in 2010

• Favorite Programming Languages – Python, Erlang, Haskell.
• Working with (programming languages) – C++, Python, Lua, HLSL, GLSL.
• Working with (platforms) – Win32, Linux, Mobile.
• Working on – Game Development, Misc Finance Software.
• Programming Languages – Pascal/Delphi, C, C++, Python, Erlang, Haskell, Lua, Javascript, HLSL, GLSL.
• Experimenting with – Haskell, Erlang, Direct3D 11, HLSL/GLSL.

Hmm… not much of a change there. Surprising (…or not)! 😀

Over-patterning software design.

Ah! Design Patterns! Yes those seemingly magical concoctions of code that appear to solve all of the problems plaguing software design. So profound is the initial impact of design patterns, that the engineer begins to believe that he/she has finally found mythical scrolls of wisdom, bestowed upon him/her by divine beings, so much so that after reading through them every design problem can be automatically deconstructed into a set of familiar design patterns. Using them seems to solve every challenge software engineering has to offer — and the engineer begins to believe that all that is ever needed on his/her desk is a copy of those very patterns. Yes, there was a time when I have been guilty of the very same thing.

There is also the misconception that patterns are drop-in replacements to traditional software design practices. It’s tempting to approach a design problem with a pre-packaged solution that patterns seem to offer. “Oh, we have a Composite, that means we need a Visitor for collaboration. So let’s use a Visitor then.” That was easy, but what was missed was the overhead of designing something as a Visitor. No one asked the question why a Visitor was needed, or if  it was indeed needed. Often the only reason given for such design decisions is, “… because a design pattern says so.” That’s not what design patterns advocate at all. Excess use of design patterns while designing software, inadvertently leads to Over-engineering.

This contradicts the popular perception which is of the view that patterns were created to address most commonly occurring design problems. Yes that is true, and no I am not trying to be a design pattern heretic and declare that patterns are useless. Patterns are in fact very useful when applied correctly. It is true that most software designs can broken down into sub-designs which can be collectively solved using a combination of different design patterns. But just because they can be, doesn’t mean they have to be. A designer well versed in design pattern use can quickly find adaptable patterns for most design problems — and can probably get them to work together if he or she understands the modalities of pattern behavior.  There is a dichotomy here; design patterns lead to over-engineering — and they are useful!! What is it then?

The truth lies somewhere in-between. Most problems with “Over-patterning” begin when there is an overbearing urge on the part of a designer to adapt his/her design, and sometimes downright bend it to fit to a design pattern. Just because a pattern fits or solves a problem, doesn’t mean it has to be used. Loading a software design with patterns is a mistake. One must remember, patterns add cost, and by cost I mean engineering cost. Strange — an engineering solution adding an engineering cost? But, thats how it is with any engineering problem in any engineering domain. Ironically if you refer each pattern you will often see these costs clearly pointed out by the authors. Call them disadvantages, limitations, issues or whatever other name you come up with, but the reality is that these issues aren’t trivial. An oversight or a failure to understand the implications of these in the overall design of a software system is what leads to overly complex  or over-engineered solutions.

An excellent article to read with regards to this is Joshua Kerievsky’s Stop Over-engineering.