As you can see, if we set F1 twice, it effectively “unsets” F1 and sets F2. Not what we were after.
So why is this? Well, it’s kind of obvious and I was being a bit of a muppet for not spotting it yesterday. The reason is pretty simple: 1 + 1 = 2. (I told you it was obvious!)
Clearly this is not what we want, but how can we solve this? By using the bitwise OR. If we change the statements slightly as follows:
$f1_or_set= BASE | F1; // $f1_or_set = 1echo"F1 OR set = $f1_or_set\n";
$f1_or_set_twice=$f1_or_set | F1; // $f1_or_set_twice = 1 - huzzah!echo"F1 OR set twice = $f1_or_set_twice\n";
As far as “unsetting” the flags if we use my original method we fall (again) into trouble.
$f1_and_f3= BASE | F1 | F3; // $f1_and_f3 = 5;$unset_f3=$f1_and_f3- F3; // $unset_f3 = 1echo"Unset F3 = $unset_f3\n";
$unset_f1=$f1_and_f3- F1; // $unset_f1 = 4;echo"Unset F1 = $unset_f1\n";
Now, if we try to “unset” F1 twice, we arrive at the problem.
$unset_f1=$f1_and_f3- F1; // $unset_f1 = 3;echo"Unset F1 = $unset_f1\n";
Unsetting F1 twice here effectively turns off F3 and sets F1 and F2 – completely wrong!
Instead, if we use the &~ binary operator mentioned in Jesper’s comment all works as expected. (note: I can’t find mention of this operator in the PHP docs, please someone help me out)
$f1_and_f3= BASE | F1 | F3; // $f1_and_f3 = 5;$unset_f3=$f1_and_f3&~ F3; // $unset_f3 = 1echo"Unset F3 = $unset_f3\n";
$unset_f1=$f1_and_f3&~ F1; // $unset_f1 = 4;echo"Unset F1 = $unset_f1\n";
Even if we try to “unset” a flag twice, it still has the same results:
$unset_f1=$f1_and_f3&~ F1; // $unset_f1 = 4;echo"Unset F1 = $unset_f1\n";
$unset_f1_twice=$unset_f1&~ F1; // $unset_f1_twice = 4;echo"Unset F1 twice = $unset_f1_twice\n";
Also in Jesper’s comment and original post was the use of the left shift operator: <<. After playing around with this it seems very simple to use, as follows:
After all this I think I am a little closer to understanding Bitwise operations, hopefully! Tomorrow I’ll have a crack at testing to see if a flag is turned on or not. Until then…
Update: Thanks to Jesper Noehr of BitBucket fame for pointing out gaping flaws in my post below (see his comment). I strongly advise you disregard all I have said below, because it will get you into a mess, in much the same way it has me. I’m going to sit down when I have a spare 1/2 hour and try to work out exactly what is going on! Many thanks and big kudos to Jesper, I really appreciate the time you took to correct me.
I have always wondered what the point of Bitwise Operators were,to me they seem to belong to a distant past. However, after reading a couple of great blog posts I have at last an understanding of how they can be put to use, and have started playing around with them a bit (ba dum!).
Jesper Noehr has written about using bitwise operators for a flexible permissions scheme within Python and Jonathan Snook has taken the bitwise concept further creating a great calendar app in Javascript. After reading these I thought I better dive in, and an opportunity came along yesterday when I had to code a flagging system within PHP.
We had a bit of a nightmare over at ibrow Towers recently. Some bright spark at the building site down the road managed to cut the pipe supplying our interweb!
Disaster!
Fortunately the culprit managed to refrain from slicing all the way through, stopping just at the point to give us the drip feed equivalent of roughly a modem circa 1997. i.e. r e a l l y slow.
Did we really ever live with that? How did we cope?
But now it is the weekend, meaning that I have some spare time at home to explore the internet again. So here are a few links that I’m going to spend my morning reading.
This is just a quick post to say Happy Birthday this blog. Yes, I began my little journey into the Blogosphere one year ago today. Looking back on that first article, now is a good time to see if I managed to stay focused on the topics that I laid out in the beginning, as well as looking at some stats for the year and why I’m doing this.
You may have noticed I haven’t posted in a while. One of the reasons is because I have been immersed in the world of digital books. In my previous post I mentioned that I purchased myself a new toy, namely the Sony PRS-300 Pocket eReader. Now I’ve had it almost 3 weeks so I feel I’m in a position to write a fairly accurate review and not one based solely on first impressions.
I’ll start with all the fluff that don’t relate to actually reading a book, then I’ll move on to what it is actually like to read.
I bought my Pocket Reader online from WHSmiths for a very reasonable £159. I had weighed up getting the more expensive PRS-600, but decided against it as I don’t need my book to play me music, and whilst it can hold 8000 books compared to “only” 350 of the PRS-300 I’ll be impressed if I read 350 books a year!
This week I purchased myself two new toys. I always do something like this around this time of year because it’s my birthday! This year I decided to get an ergonomic keyboard and an e-reader.
The e-reader was much harder to choose. After much deliberation, googling, reading of reviews etc I eventually landed myself a Sony Pocket Reader PRS-300. I chose this over other e-readers for a number of reasons. Firstly the price was right, I didn’t really want to spend over £200 on what is effectively a fancy book. Secondly I only wanted it for reading, I didn’t care about taking notes, listening to music that kind of stuff. Finally I wanted to to be portable – obviously all readers are portable, but I wanted something that I could easily keep in a coat pocket for when I’m on a bus etc. or generally waiting around for something to happen.
First Impressions
The keyboard was huge! Much more so then I expected, but fortunately I have a big desk, so there was no problem there. I’ve never used an ergonomic keyboard before and I was suprised how comfortable it felt. I really thought I’d be able to enjoy typing. However, the space bar is a nightmare. Really sticky, hard to press down and very “clacky” Others have experienced this problem, but most suggested it gets “broken in” within a few days or week, so I’m not overly worried (yet).
The Sony e-reader is just beautiful. The text is clear, navigation simple, weight nothing to speak of and the size is perfect. I am in love with it. One problem was I said I had to use Windows to connect to it, so I fired up Vista for the first time in about half a year, and after about 20 minutes of installing it crashed. It did this again a couple of times, until I found out a fix – something so do with VB script and premissions. Rather stupidly I forgot to save the link that helped me fix the problem otherwise I would have posted it here. Sorry about that. However, it turns out I can connect to it within Ubuntu, and transfer books onto it – so that is good news.
I’m going to write a more substantial review for both after a couple of weeks usage, so stay tuned – sign up to my RSS feed.
After my week with Emacs I’m still struggling along. One of my main annoyances is the default way some of the modes indent the code. I think the worst offender of this is the default CSS-mode. After frustration when, yet again, I hit the tab key and the cursor rockets about 80 places to the right I decided to Google for a fix.
The internet is great! Clearly it wasn’t just me having a problem, Guido Stevens was also suffering and posted a fix, which he himself found on another blog post by Chris Miller, which Chris in turn found on StokeBloke.com which had been found originally on a now dead post.
Not bad! Four people all having problems with the same thing and posting a fix for others to share. So I thought I’d join the chain, and write this blog post.
As promised in my last post, I have spent a whole week using Emacs. Apart from the odd foray with Nano and Mousepad I haven’t touched Eclipse or Netbeans or any other IDE and managed to stick to Emacs for the full week.
How did it go?
To start off it was slow. Emacs has a notoriously high learning curve, and I pretty much started at the bottom. One of the reasons for doing this was so I could move away from the mouse and it turns out that the mouse is an extremely hard habit to break. So to are the keyboard arrow keys. I kept on finding my hands would instinctively jump off the keyboard and try to double click on something, or try to navigate around the page with the arrows. This is clearly not the way it is done in Emacs!
The next thing I found is that Emacs doesn’t let you indent code files as you want. It seems to have a preferred method and forces it on you. This is very annoying and being a n00b I still haven’t found a way around this.
However, after a couple of days I began to get the hang of it. Still painfully slow, but navigating around the page, buffers and windows was becoming gradually quicker. I began to enjoy using Emacs, even though my right hand kept on unceremoniously lurching to the right from time to time.
By a complete coincidence, on Wednesday I was invited to C-Base here in Berlin for a beginners’ introduction to Emacs. Even though it was all in German, and my Deutsch ist nicht so gut I was blown away by not only the speed but also the huge amount of functionality within Emacs. To see someone who actually knew Emacs inside out was a revelation. I made a huge amount of notes (within Emacs before you ask!) ready to test out for myself. This insight into the “how the pros use Emacs” has really been an eye-opener and I am determined to learn just a fraction of what was on display at the tutorial.
Summing up my week with Emacs.
It was hard work, there was much and is even more left to learn. It is a vast landscape to negotiate, with many nooks and crannies. But once it is mastered I have no doubt that my productivity will be greatly increased.
I have been toying around with the idea of using Emacs for a few months now. Emacs isn’t exactly a stranger to me, as I’ve been using it on and off for years, but I’ve never really tried to fully know it properly. However, recently I have been in front of the computer so long I feel like my hands are going to fall off! Which is why I’ve decided to try to move away from this point and click mouse nonsense towards a more streamline keyboard only work environment.
So I have decided to give Emacs a go for a full week as my single development editor. Previously I’ve been using Eclipse PDT – which became so slow – and Netbeans – which I like a lot, but it is still point and click. One of my inspirations for this was a blog post by BradleyWright. Poor Bradley’s experience must have been so horrifying that he hasn’t posted a follow up for his weeks usage, and this is from several months ago. Brad, if you can hear me, please let me know how it went, I am dying to know! But until then, I am going to try it out for myself.
Wish me luck, and if you have any tips for PHP development within Emacs, please drop a comment below and help me along.