Javascript: Debugging Bitwise Operators

Most people are never going to even use bitwise ops, especially in JavaScript. But if you really must, beware all is not what it seems!

Problem 1’s Complement
1’s complement seems slightly off?!
console.log(~0xFF); //gives -256

Explanation
You might then automatically assume that JavaScript implementation of 1’s complement is fundamental borked, a cursory Google might even back up that. Obviously its hard to believe that handling of bitwise ops are so broken in a language that is so widely used, but given that few if any web developers’ would actually need bitwise ops maybe its the case. (There are plenty of stack overflow posts that recommend the abuse of the ~ operator)

Taking a closer look at the ECMA script definition you see the definition of NOT as the following[1]:

      Let expr be the result of evaluating UnaryExpression.
      Let oldValue be ToInt32(GetValue(expr)).
      Return the result of applying bitwise complement to oldValue. The result is a signed 32-bit integer.

Now this is where things get interesting. You would naturally assume if you give the NOT operator a 8-bit value you’d get an 8-bit field back, but this is clearly not the case. If you want to dig even further the real concrete instance of the value is a 64-bit IEEE 754 representation see [2].

So that neatly explains why ~0xff transforms to -256

1111 1111
becomes
0000 0000 0000 0000 0000 0000 1111 1111
apply ~ (NOT)
1111 1111 1111 1111 1111 1111 0000 0000
aka -256

But that still leaves a problem, how can you easily debug a value that isn’t a Number but is represented as one…

If you’ve got this far there is probably a very good reason you’re using bitwise ops, otherwise you might have given up on them altogether. So it would be somewhat handy to look at the real value, and not the Int32 representation, which console.log and friends are going to give a misleading value for.

Getting at the full representation
A nice simple function to show all the bits for the input value.

function fullBinaryString(input){
//coerce javascript to show full 32bit integer underneath.
return (input>>>0).toString(2);
}
view raw bits.js hosted with ❤ by GitHub

>>> Operator coerce the value input a 32-bit representation by right shifting and filling in with zeros, depending on your usage you can also slice the value, to only show the 8-bits that are needed to debug, like so:

function to8bitString(input){
//clamp the value to 8-bits by and with 0xFF
var clampedValue = (input&0xFF).toString(2);
//pad the string to make it easier to read.
return String("00000000" + clampedValue).slice(-8);
}
view raw bits.js hosted with ❤ by GitHub

General Rule
If dealing with bits, clamp the field to the proper bit length before assignment. Using an 8-Bit clamped array is no guarantee that the value will be assigned properly, there are cases where the value can be assigned to zero instead of the proper bit value. For an 8-bit you would use array[i] = bits&0xFF this will make sure the value is appropriately truncated and does not get evaluated as a Number.

[1] http://www.ecma-international.org/ecma-262/5.1/#sec-11.4.8
[2] http://www.ecma-international.org/ecma-262/5.1/#sec-4.3.19

Use the ODBC predicate? Mental ODBC + Prolog

While I can kind of see the point of why it might be useful, just like Prolog-Java bridges are sometimes useful if it works – which is almost never. It still seems a bit mental to be using mySQL with prolog.

If you really are using prolog, the data you get from prolog and its database (I had the mis-fortune of studying this 3 times) aren’t really congruent with mySQL.

One is highly structured tabular, the other is derived and organised in a hierarchy, if you need storage for you’d probably be better dumping text files, or get with the hipsters in the NoSQL camp.

Prolog in Python (pt. 2) : ryePDX.

tl;dr

Postcards from New York

I never actually thought I would end up buying an iPad or for that matter really liking one if i did. However, sat at 32,000ft typing this review out on an iPad this thing makes absolute sense, and is just about the best gadget I have purchased to date.

For the vegetarians amongst you, there is a very easy analogy the iPhone is a quorn burger, where as the iPad is a full blown New York strip steak with steak sauce, fries and whatever else you care to have on, or with your steak. Yes it doesn’t multi-task, and yes you are limited to the apps that apple approve – but what you do get is a fantastic piece of hardware with some fantastic apps.

The ultimate role of the iPad is neither to be a laptop nor a giant iPhone, it is to be the device you goto on a morning to get your news fix, or when you are sat down and having an argument over who made the engines for the Blackbird. It’s instantly on and instantly there, unlike any device I have ever used you click it happens.

The keyboard is surprisingly good and like the iPhone, it will learn your common mistakes and correct them. Unlike the iPhone however there is built in spell checker, which seems to be apart of the OS so is available to any application that supports it; it’s the simple touches that make life easier.

As far as battery life goes I expect it to easily last the 6hour flight to San Francisco that I am on now, which will involve watching at least 3 episode of FlashForward, and multiple tweets.

All in all I think it may well turn out to be a good buy.

Spotify & Apple Remote

iconIn my continuing saga to be as lazy as possible, I have brought back my Apple Remote into use. It has practically no use except when away from the computer you can change the iTunes track. So been as I use Spotify almost as much as I use iTunes why can’t I do the same.

Turns out you can with a little SIMBL plugin, http://themacbox.co.uk/smr/

Controlling Spotify through Applescript & Quicksilver.

Update Available: http://www.jacktams.co.uk/2009/10/09/spotify-applescripts-updated/

picture-2I have almost solely being listen to music through spotify for the last couple of weeks, sure it doesn’t replace iTunes but its damn good at what it does, all you can eat music for free. http://www.spotify.com/

The Problem:
Spotify can use the built in shortcuts for play/pause, forward and reverse, but if like me you use a different keyboard and effectively ‘dock’ you Macbook its not much use. Enter Quicksilver, the perennial quick-launch and whiz-kid short-cutter for mac. http://bit.ly/yrlr7

I already have a load of triggers set up within quicksilver for everything from make an event in iCal to post a tweet or send email.

So getting Spotify in there, it turns out Applescript is here to help, using the System Events helper you can select a menu item without actually clicking it. Below is the code for the main actions you would want in Spotify. Simply save the apple script, then attach it to a trigger in Quicksilver. Hey Presto, magic! For more details on setting up triggers check the Quicksilver wiki, it explains it alot better than I ever could. http://bit.ly/134dnL

Play Next

tell application "Spotify" to activate
tell application "System Events"
   tell process "Spotify"
      click menu item 3 of menu 1 of menu bar item 5 of menu bar 1
   end tell
end tell

Play Previous

tell application "Spotify" to activate
tell application "System Events"
   tell process "Spotify"
      click menu item 4 of menu 1 of menu bar item 5 of menu bar 1
   end tell
end tell

Play/Pause

tell application "Spotify" to activate
tell application "System Events"
   tell process "Spotify"
      click menu item 1 of menu 1 of menu bar item 5 of menu bar 1
   end tell
end tell

Notes:
Don’t forget to activate assitive device support see http://www.macspeech.com/extensions/faq/kb.php?article=48

last.fm vs TechCrunch

Being the heavy user of Last.fm I was a little concerned when techcrunch broke the story that supposably, last.fm had given the RIAA, information pertaining to users who had scrobbled any tracks from the Upcoming and undrealeased U2 album. The fact I can’t stand the rose tinted glasses wearing, Irish Hypocrite is beside the point.

Firstly, what got me there was no mention of the Data Protection act fair enough last.fm is a CBS company, but its still based in the UK (for the most part) and that would mean the passing of this data would be illegal. You cannot pass data to a third party without consent from the data subject.

Also it seems like there are alot of people and blogs posting unsubstantiated rumours to get traffic and therefore ad revenue etc. I know its always been there but it seems like everybody is at it even big news companies, what little credibilty these sites have is been quickly ruined by hastly researched stories and a all too trigger happy publish finger. 

Last.fm – Keep up the good work.

TechCrunch Article

Last.fm – “TechCrunch is full of shit”

What is Yahoo!s’ Business?

The dust is still settling from Jerry Yang’s announcement that he is to step down as Yahoos’ CEO. I wonder if Yahoo! has any idea what it wants to be. Search, Advertising or Neither.

There is still talk of Microsoft buying Yahoo! Search, which is fair enough take the least toxic bit of Yahoo! But what are you then left with. Yahoo! Mail, Delicious & Flickr they are the only remaining big properties that Yahoo have on the books.

Delicious & Flickr have yet to be monetized and the user base on both platforms would kill the person responsible for upsetting the status quo. That leaves Yahoo! Mail, which has some ads but the willingness of Yahoo! to go with the Google advertising deal means they are likely making no money on that either.

So how is Yahoo making money?

Sure its got deals with BT in the UK providing the homepage and mail service to all BT Broadband customers, and it has some News properties which are well trafficed. User growth is going to be negligible anybody who would want to sign up for a Yahoo! Account probably have, its a problem many big companies face and sure there have been attempts to get the workforce re-invigorated but so far nothing new and astounding has made it through the door, and what has been something a bit different had to be bought in.

I have said many times before that Yahoo! doesn’t have a clear sense of what it wants to be; Search, Advertising, Content or Video, it just doesn’t sit nicely anywhere and it can’t be the agregator for all because Google has sewn that one up however, thats not to say Yahoo! couldn’t compete – they could if they become a strong leader in the tech sector again.

If I where Yahoo! I would invest heavily in cloud computing services, rather than following try and jump ahead. Just like Apple did in 1997.

The Problem with Open – Android vs iPhone.

There has been lots of talk about Apple is doomed to fail with the iPhone as a software Platform and the Android platform is going to be so much better, simple because its open.

Firstly, take a look at the iPhone. Its a fantastically designed device, and the attention to detail is insane – that’s not to say the Android platform isn’t. But it is a sensible justification that the platform needs to slightly closed to allow a continuity of quality to be maintained. Secondly, the iTunes to iPhone facility has yet to face any real competition, its far from perfect but there is a seamless connection from buying to using. You click buy and the app or music is downloaded and synced to your phone, without having to drag and drop, move or do anything manually. However there is growing concern that the closed nature of the platform will be its undoing see Pull My Finger [1] Continue reading “The Problem with Open – Android vs iPhone.”

www.thisinternetisnot.easier

ICANN is full of good ideas, especially on how people use the internet.

The net’s regulator, Icann, voted unanimously to relax the strict rules on so-called “top-level” domain names, such as .com or .uk.

The decision means that companies could turn brands into web addresses, while individuals could use their names – BBC NEWS

Despite the fact it opens up a whole slew of stupid domains, it means you can no longer guess a domain name by picking the brand and adding .com or .co.uk it also means domain squatting is going to get more and more common, even if initially domains cost six figure sums.

ICANN – The idea is stupid, work out a better way, and try to find some way of regulating domain squatting not making up stupid ideas and justifying them by saying it will make the internet easier to use, because it will not!

How I became Inspector Gadget.

There are certain points in your life where you can’t help but look back on the preceding years. Officially I have now left college on study leave, until 20 June, which is my last day ever. So how the hell did I end up at this point.

My first exposure to a computer was a windows 3.1 machine in 1995, it was god awful but I was only five and young kids and technology don’t really get on. It was a good few years before I got a computer of my own, I ended up with a Pentium 1 MX running Windows 95, which didn’t last long. I couldn’t play any games on it, and it was stable as a long pole with a plate on it. So inevetably it was upgraded to a machine running Windows 98 Pentium 2, with a decent graphics card and MPEG decoder card.

 

Its probably at that point that the bug really caught me, from then on in I had a slew of applications and experiments going on the poor computer, which I still have under my desk. Three computers later and I made the big switch to Mac, something which I haven’t regretted, and still manage to keep up with windows excluding Vista which is almost as bad as 3.1. I also managed to pick up Ruby on Rails and a bit of PHP along the way, and ashamed as I am to say it Visual Basic.

People always ask me how I know how computers work. The simple answer is I have been tinkering with them for far too long. Every computer I have owned has been broken replaced upgraded and attacked by me, leading me to come across practically every common error you can get. Its sad to say but I can usually diagnose a hardware fault before the BIOS has finished its self test at boot up, and a software problem by hitting less then 10 commands.

The trend over the last few years is people are using technology every waking moment, but very few know how the stuff works. I love knowing how it works, and couldn’t really care less about using it. I will strip things down take them to bits, rebuild them, and then maybe use them. Because of this I have a collection of gadgets and gizmos that few other people my age can boast. It also means, that college work can sometimes come a distant second to a new gadget or blog post.

I don’t procrastinate as such, I just love technology to distraction. Wait a minute that is technically procrastinating. I don’t know what career I may choose, convergent technologies mean that practically any field is open to me.

Best bit is I know there will never be a boring job, technology is getting more and more exciting the closer we get to the point on the curve we drop off.