"Because expressing how dumb that was in words just doesn't work." Indeed!

 Facepalm, was exactly how i felt when i was chatting with my friend @Juliana Franco (which is going to be one of the newest students of Msc Software Engineer@FCUL so, congratz on her achievement!), and ‘why?’ you ask, this is why:

(n & 1) == 1

For non programmers, this & represents an AND bit wise operation, in short, this is a condition that verifies if n is an odd number. I, as almost every programing student around the world, have been working with binary numbers and bit wise operations for years and when i saw this i asked myself, why did i failed to see the usability of my knowledge and experience in an useful situation like, for instance, get a better performance out of my code? Why didn’t i think of this? (for non programmers, one thing that you always need to have in mind is the performance of your code, you can have 2 programs to do the exact same task but one needs 1sec to finish and the other needs 1hour, meaning that the first have have a better optimized code than the second, moving on…) If i failed to see something this basic, am i failing to see other amazing little things? Mind blown.

So, if you have any other programing black sorcery that can do magical things (lol), feel free to comment and share them!

On another note, we hit our first milestone, having 25 facebook page likes thus, securing an username for the page.

That’s all for now!


6 thoughts on “>*Facepalm*

  1. hruivo

    Double facepalm. It really annoys me when things like this aren’t part of the common teaching. I bet most of our programming teacher’s we had aren’t aware of this ‘feature’.

    Thanks for the share!

    • Maybe yes, maybe not, if you think about it, we never learned bitwise operators. lol!

      One of the biggest flaws i think Portugal’s education system has is that everything you learn should have a reason and be used in a real situation (and not pointless pseudo simulations), for instance, in an Computer Science or Game Development course like ours, the logic part we learn in Mathematic related subjects should be supported, afterwards, in our Programing related subjects. Specially in college courses where you are supposed to choose an area that pleases you the most, everything should be connected and their ‘usefulness’ explained so you don’t miss stupid things like this one…

      • So you want to be spoon fed? While I agree there are lots of flaws, most things are indeed connected though you may only only realize it later. For example, discrete maths and security or formal automata and AI. Maybe you want to be a game developer and you need algebra and physics too. While all of these things are important they build on each other and lecturers are not going to warn you to pay attention to something just because you will need it one year later, they may not event know that themselves ( a maths lecturer probably knows nothing about security ).

        And of course you never learned bitwise operators, that’s something you learn either by yourself or in a specialized postgraduate degree if we’re talking about games. If you’re learning C++ it’s highly unlikely a lecturer will mention volatile or mutable but can you blame him? He’s trying to teach you the basics and not too much time to do it. So research and learn by yourself, best way to go about it.

        Regarding the post, the best optimizations are the ones made in the design. For example, If you have a poor implementation and/or data structures that don’t fit the problem you can optimize whatever you want and you’re only going to get a “fast slow program”. If you think about the problem carefully you’ll see that improving the design, improves speed but also lends itself to new optimizations. And you most likely will not get a performance improvement by using this kind of “magic”, because compilers already do that for you. So should you trust your compiler? Hell no, different compilers, different platforms, so check what your compiler is spitting out and always profile! And of course, learning low level stuff will make you a better programmer in the long run, so always useful 😉

        You want black sorcery? OK. All the consoles in the current generation have In-order processors and that means (simplifying) that branch prediction is slooooow, incurring L2 cache misses. So here is some code that avoids those nasty ifs and elses:

        if (condition)
        value = a;
        value = b;

        can be turned into this:
        value = (a & condition) | (b & ~condition);
        value = ((a ^ b) & condition) & a;

        As a side note, if you’re interested in parallelism look up Fibers.

        Keep it up!

        PS: Wall of text 😐 :s

    • Damn, my response got filtered. Consider SL=Shift Left and SR=Shigt Right

      1 SL N = 2^N
      M SR N = M/2^N

      (Obviously, this just works on unsigned integers)

      • Read about those in some investigation i did back then, hope someday i remember to use this lol… Although, i’ve successfully implemented a primitive anti memory hack system using the XOR cipher in a game! Simple, sweet and takes little processing, don’t really know why all of those flash games some years ago never had something like this.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s