This Place is Taken

Tuesday, December 10, 2013

The Origin of 0 index arrays

 

Sometimes somebody says something to me, like a whisper of a hint of an echo of something half-forgotten, and it lands on me like an invocation. The mania sets in, and it isn’t enough to believe; I have to know.

I’ve spent far more effort than is sensible this month crawling down a rabbit hole disguised, as they often are, as a straightforward question: why do programmers start counting at zero?

Now: stop right there. By now your peripheral vision should have convinced you that this is a long article, and I’m not here to waste your time. But if you’re gearing up to tell me about efficient pointer arithmetic or binary addition or something, you’re wrong. You don’t think you’re wrong and that’s part of a much larger problem, but you’re still wrong.

For some backstory, on the off chance anyone still reading by this paragraph isn’t an IT professional of some stripe: most computer languages including C/C++, Perl, Python, some (but not all!) versions of Lisp, many others – are “zero-origin” or “zero-indexed”. That is to say, in an array A with 8 elements in it, the first element is A[0], and the last is A[7]. This isn’t universally true, though, and other languages from the same (and earlier!) eras are sometimes one-indexed, going from A[1] to A[8].

While it’s a relatively rare practice in modern languages, one-origin arrays certainly aren’t dead; there’s a lot of blood pumping through Lua these days, not to mention MATLAB, Mathematica and a handful of others. If you’re feeling particularly adventurous Haskell apparently lets you pick your poison at startup, and in what has to be the most lunatic thing I’ve seen on a piece of silicon since I found out the MIPS architecture had runtime-mutable endianness, Visual Basic (up to v6.0) featured the OPTION BASE flag, letting you flip that coin on a per-module basis. Zero- and one-origin arrays in different corners of the same program! It’s just software, why not?

All that is to say that starting at 1 is not an unreasonable position at all; to a typical human thinking about the zeroth element of an array doesn’t make any more sense than trying to catch the zeroth bus that comes by, but we’ve clearly ended up here somehow. So what’s the story there?

The usual arguments involving pointer arithmetic and incrementing by sizeof(struct) and so forth describe features that are nice enough once you’ve got the hang of them, but they’re also post-facto justifications. This is obvious if you take the most cursory look at the history of programming languages; C inherited its array semantics from B, which inherited them in turn from BCPL, and though BCPL arrays are zero-origin, the language doesn’t support pointer arithmetic, much less data structures. On top of that other languages that antedate BCPL and C aren’t zero-indexed. Algol 60 uses one-indexed arrays, and arrays in Fortran are arbitrarily indexed – they’re just a range from X to Y, and X and Y don’t even need to be positive integers.

So by the early 1960′s, there are three different approaches to the data structure we now call an array.

  • Zero-indexed, in which the array index carries no particular semantics beyond its implementation in machine code.
  • One-indexed, identical to the matrix notation people have been using for quite some time. It comes at the cost of a CPU instruction to manage the offset; usability isn’t free.
  • Arbitrary indices, in which the range is significant with regards to the problem you’re up against.

So if your answer started with “because in C…”, you’ve been repeating a good story you heard one time, without ever asking yourself if it’s true. It’s not about*i = a + n*sizeof(x) because pointers and structs didn’t exist. And that’s the most coherent argument I can find; there are dozens of other arguments for zero-indexing involving “natural numbers” or “elegance” or some other unresearched hippie voodoo nonsense that are either wrong or too dumb to rise to the level of wrong.

The fact of it is this: before pointers, structs, C and Unix existed, at a time when other languages with a lot of resources and (by the standard of the day) user populations behind them were one- or arbitrarily-indexed, somebody decided that the right thing was for arrays to start at zero.

So I found that person and asked him.

His name is Dr. Martin Richards; he’s the creator of BCPL, now almost 7 years into retirement; you’ve probably heard of one of his doctoral students Eben Upton, creator of the Raspberry Pi. I emailed him to ask why he decided to start counting arrays from zero, way back then. He replied that…

As for BCPL and C subscripts starting at zero. BCPL was essentially designed as typeless language close to machine code. Just as in machine code registers are typically all the same size and contain values that represent almost anything, such as integers, machine addresses, truth values, characters, etc. BCPL has typeless variables just like machine registers capable of representing anything. If a BCPL variable represents a pointer, it points to one or more consecutive words of memory. These words are the same size as BCPL variables. Just as machine code allows address arithmetic so does BCPL, so if p is a pointer p+1 is a pointer to the next word after the one p points to. Naturally p+0 has the same value as p. The monodic indirection operator ! takes a pointer as it’s argument and returns the contents of the word pointed to. If v is a pointer !(v+I) will access the word pointed to by v+I. As I varies from zero upwards we access consecutive locations starting at the one pointed to by v when I is zero. The dyadic version of ! is defined so that v!i = !(v+I). v!i behaves like a subscripted expression with v being a one dimensional array and I being an integer subscript. It is entirely natural for the first element of the array to have subscript zero. C copied BCPL’s approach using * for monodic ! and [ ] for array subscription. Note that, in BCPL v!5 = !(v+5) = !(5+v) = 5!v. The same happens in C, v[5] = 5[v]. I can see no sensible reason why the first element of a BCPL array should have subscript one. Note that 5!v is rather like a field selector accessing a field in a structure pointed to by v.

This is interesting for a number of reasons, though I’ll leave their enumeration to your discretion. The one that I find most striking, though, is that this is the earliest example I can find of the understanding that a programming language is a user interface, and that there are difficult, subtle tradeoffs to make between resources and usability. Remember, all this was at a time when everything about the future of human-computer interaction was up in the air, from the shape of the keyboard and the glyphs on the switches and keycaps right down to how the ones and zeros were manifested in paper ribbon and bare metal; this note by the late Dennis Ritchie might give you a taste of the situation, where he mentions that five years later one of the primary reasons they went with C’s square-bracket array notation was that it was getting steadily easier to reliably find square brackets on the world’s keyboards.

“Now just a second, Hoye”, I can hear you muttering. “I’ve looked at the BCPL manual and read Dr. Richards’ explanation and you’re not fooling anyone. That looks a lot like the efficient-pointer-arithmetic argument you were frothing about, except with exclamation points.” And you’d be very close to right. That’s exactly what it is – the distinction is where those efficiencies take place, and why.

BCPL was first compiled on an IBM 7094here’s a picture of the console, though the entire computer took up a large room – running CTSS – theCompatible Time Sharing System – that antedates Unix much as BCPL antedates C. There’s no malloc() in that context, because there’s nobody to share the memory core with. You get the entire machine and the clock starts ticking, and when your wall-clock time block runs out that’s it. But here’s the thing: in that context none of the offset-calculations we’re supposedly economizing are calculated at execution time. All that work is done ahead of time by the compiler.

You read that right. That sheet-metal, “wibble-wibble-wibble” noise your brain is making is exactly the right reaction.

Whatever justifications or advantages came along later – and it’s true, you do save a few processor cycles here and there and that’s nice – the reason we started using zero-indexed arrays was because it shaved a couple of processor cycles off of a program’s compilation time. Not execution time; compile time.

Sunday, December 8, 2013

Neelakasham Pachakadal Chuvanna Bhoomi–Road not Taken ?

 

I got the chance to catch another malayalam movie on torrent again, Neelakasham Pachakadal Chuvanna Bhoomi, release in Aug 2013.  I had missed this movie due to all the wrong reasons, and by the time I made it back to kerala, it had gone out of the theatres. It was called the first malayalam road trip movie, and those who watched it told me it was a keeper, another one of those new generation movies, no stereotypes, no superstars, and no formula at all.

It tells the story of two guys, who make a bike journey from their roots in Kerala, to the North Eastern Region of India. On the way, they meet a host of colorful characters, learn a bit of life, get nearly robbed, help others selflessly, and even grow up a little. The cinematography of the movie is awesome, all the promos and pics are slick and stylishly shot. But the real hero turned out to be the background music and the songs. You will be compelled to take out a bike and go on a similar trip yourself.

Now personally, I never understood why grown up men go on long, endearing bike trips at all. Was it the destination that mattered, or the journey itself ? The preferred choice of bikes are those old style British thumpers, with 50 year old engine designs , very little mileage and a lot of noise. They pollute everything around it, and are difficult to maneuver and maintain. I have myself travelled on some bike rides with my friends, and what I learned : in the long run, its uncomfortable, and unsafe on Indian roads. And not to mention , expensive. The same trip would work out lot more cheaper if travelled by train (my favourite Smile). But I guess, it is an adventure, simply to break convention.

Anyway, back to our movie. Kasi (Dulquar Salman), wanted to to go on a solo journey, the reason for this is not revealed at first. But his best buddy (Sunny), intercepts and joins him anyway. The best bike movies are told by buddies anyway: Motorcyle Diaries, Easy Rider. After a fun start and joyous ride, they run into another group of bikers, and end up in Puri, a place know to be a holy city, but now a surfing destination. Ishita, a surfer girl from Pondicherry, is smitten by Kasi, but he is not looking for happiness , it seems. The bike boys leave for Kolkatta, running into Bimalda, the leader of a down trodden village fighting against capitalization. The boys contribute in their own way. Part of the story, and the reason for the journey, is revealed in a flashback.

Apparently, Kasi was an ideological an honest college student union leader. He meets and falls in love with Assi, an girl from Nagaland who is in his college under the North East quota. She has had a violent past, lost both her parents to communal violence, and lives in constant fear of being targeted by the same. Kasi himself hails from a well-to-do influential Muslim family, and wants to give Assi a new life. But the death of a close friend, and questions from Kasi’s orthodox extended family forces Assi to leave his life. Kasi is heartbroken, and feels betrayed, both by his love, and his family. He needs something to take his mind off, and what better way to forget, than a bike journey ?

When the duo reaches Assam, they witness the act of communal violence firsthand, and Kasi decides to turn back. But when he contemplates on the journey so far, accelerated with nice weed smoke, he decides to go meet Assi all the way in Nagaland. Once there, he leaves with her, having made up his mind up once and for all.

The first half of the movie is captivating, you are able to connect to the duo riders, and all the other people they run into. But then the story introduces communal and religion and politics into the equation. At least for me, I was not able to relate to these ideas, those bits could have been very well left out. I think the writer was inspired by MotorCycle Diaries, in which Che Guevera came to understand the troubles of his country men firsthand. Anyway, we are back on the road with the riders, and able to complete the remainder of the journey.

What stood out :

  1. Music. OMG. Both the background score, and all the songs have that new age feel of modern Malayalam music to it. Rex master rocks ! For some reason, the guitar seems to be the best instrument to compliment motorcycle journeys (or any road trip for that matter). Rex uses different styles for the different locations and moods in the movie.
  2. Dulquar: He once again portrays a person fighting with himself. Kasi is depicted as an honest and truthful student union leader. He is not easily corrupted by petty party politics. Its hard to come by such a peson nowadays. When every other movie nowadays has smooch scenes and pre-marital affairs, Kasi and Assi are shown as old school, abstaining from such a relationship. Again, it is hard to come by such a couple.
  3. Thrissur !: Hailing from the lovely town myself, it was fun to note that the flashback story was running in Thrissur Engineering college, and various points in Thrissur town. Also the beach is shown, so brownie points for that.
  4. Too many malayalees: The duo travels in North India, right ? But then, too many people they run into speak the difficult language, including Raghavan, the puncture fixer. Looks like we are here, there ,everywhere !

 

The best way to enjoy this movie is with your best buddies.

The at one point, the story depicts the suicide of a student, due to inability to pay her fees. This is inspired by an actual event: in 2004, Rajani, an engineering student jumped to her death for similar reasons. I remember this because this happened during my third year in Engineering college. The incident sparked fire to the already hot topic of exaggerated fees in many self financing colleges in the state. Every college went into long strikes, disrupting normal college life, and the future of many colleges and their students seemed in trouble. Reminds me of the many strikes and slogan shouting I myself participated in, knowing that there was no easy solution to the problem.  Anyway, the four years I lost in college was probably the worst of my life, that time is lost, and I have no intention of going back.

Thursday, November 28, 2013

Sandwichbike Flat-Pack Wooden Bicycle Goes Into Production

 

 

A flat-pack wooden bicycle that can be assembled in less than an hour has gone into production (+ slideshow).

Sandwichbike by PedalFactory

PedalFactory claims the Sandwichbike can be unpacked and put together in just 45 minutes. "If you can make a sandwich, you can make a Sandwichbike," the company declares.

Sandwichbike by PedalFactory

The single-speed bike is constructed from 19 parts that are packaged and delivered in a box along with the tools required to assemble it.

Sandwichbike by PedalFactory

The frame is made from panels of weatherproofed beech plywood and is held together by milled aluminium cylinders.

Sandwichbike by PedalFactory

Stainless steel spokes sit within the 26-inch tyres. The completed model weighs 17 kilograms.

Sandwichbike by PedalFactory

Pedalfactory was co-founded by designer Basten Leijh, who originally developed the bike with his Amsterdam design studio Bleijh for the 2006 International Bicycle Design Competition in Taiwan.

Sandwichbike by PedalFactory

The bikes are now available to order and the first deliveries in Europe will coincide with the official launch event, taking place in Amsterdam on Sunday. International orders will be dispatched early next year.

Read on for more details from the designers:


Product launch Sandwichbike: innovative designer bike now in production

The Sandwichbike will be launched in Amsterdam on Sunday 1 December 2013. This innovative wooden bicycle that already drew unprecedented attention worldwide in the design stage is now being shipped.

Sandwichbike by PedalFactory

After a period of extensive research and development the bicycle has now gone into production. The Sandwichbike can be delivered worldwide from December 1, 2013 onwards. The prototype was recently exhibited at various fairs and websites and was an instant hit among bicycle lovers and design.

Sandwichbike by PedalFactory

The Sandwichbike is a unique product on all fronts: material, design and production method. Its distinctive frame is composed of two weatherproof beech wood panels. Its advanced production technology makes self-assembly easy while a high quality standard is maintained.

Sandwichbike by PedalFactory

Postal package

The bicycle is flat packed in a box containing the parts as well as all the tools needed. This creates a great unpacking experience. For enthusiasts, putting the bicycle together is part of the charm and the logistical benefits are huge as this enables worldwide delivery. Anyone from Amsterdam to Honolulu can receive a Sandwichbike by post.

Assembling a Sandwichbike is easy and takes less than an hour. "If you can make a sandwich, you can make a Sandwichbike."

Sandwichbike by PedalFactory

Pedalfactory

The Sandwichbike is a Pedalfactory B.V. product. Co-founder Basten Leijh (also: Bleijh Industrial Design Studio) designed and developed this bicycle. Leijh is an expert on bicycle design and innovation. Among many other product innovations Leijh developed a city-bicycle that could be locked by twisting the handlebars.

Readability — An Arc90 Laboratory Experiment

Follow us on Twitter »

Saturday, November 23, 2013

DroidCon India 2013 Kicks off with Beginner’s workshop !

 

I am attending Droidcon 2013 in Bangalore, India. At the beginner’s android workshop began today at 9am at Thoughtworks offices in Koramangala, Bangalore. I must say, I already feel charged, a very enthusiastic crowd, and three over excited trainers.

Thursday, October 31, 2013

10 Useful Features Hidden in VLC, The Swiss Army Knife of Media Players

 

vlc-desktop-window

There’s a good chance you already use the VLC media player. But VLC isn’t just a media player — it’s a complete Swiss Army Knife for digital videos and music, filled with useful and fun features you haven’t found yet.

If you’re just using VLC to play back local media files, you’re only using a tiny fraction of VLC’s capabilities. There’s so much more you can do with VLC, whether you’re using it on Windows, Mac, or Linux.

Convert Media Files

VLC can convert media files between the formats it supports. You could use this to make a video smaller for a mobile device, convert media from an unsupported format to one your device supports, or even to extract the audio from a video and save it as a separate file.

To do this, click the Media menu and select Convert / Save. Load the file you want to convert, click the Convert / Save button, and select the type of file you want to convert it to. Use the Edit selected profile button to tweak the video encoding settings.

vlc-convert

Stream Media Over the Network or Internet

VLC can stream media across the Internet or on your local network. To get started, click the Media menu, select Stream, provide the media file you want to stream and click the Stream button. You’ll be able to set up VLC as a media server so other computers on the network — or even around the world — can connect to your stream and view it.

Of course, if you want to stream over the Internet, you’ll probably need to forward ports on your router.

Record Your Desktop

VLC can load your desktop as an input device. This means that you can use the Convert / Save feature to save a video of your desktop, effectively turning VLC into screen capture software. You could also use this in concert with the Stream feature to broadcast a live stream of your desktop across the network or Internet with no additional software required.

vlc screencast header

Remotely Control Playback From a Browser

VLC has an integrated HTTP server you can enable. Set this up and you can then remotely access your VLC client via a web browser. This would allow you to remotely control a media center PC from a web browser, controlling playback and queuing up audio or video files. You could even use this along with a smartphone to turn your phone into a remote control for VLC. There are mobile apps that function as remote controls for VLC, and these apps use VLC’s web interface to function.

image

Watch YouTube Videos

Want to play a YouTube video outside of your web browser? Just browse to a video on YouTube and copy its full URL — this should look something like the following:

https://www.youtube.com/watch?v=###########

Click the Media menu in VLC, select Open Network Stream, and paste the YouTube video’s URL into the box. VLC will load the video from YouTube and play it in a VLC window on your desktop.

When the video is playing, you could click the Tools menu and select Codec Information. You’ll see the full web address of the MP4 video displayed in the Location box, so you can copy-and-paste into a download manager — or just your web browser — to download the YouTube video to your computer.

Subscribe to Podcasts

VLC can be used to stream podcasts, so you don’t need any additional software if you want to listen to podcasts at your PC. Just click the View menu in VLC and select Playlist. Hover over Podcasts in the sidebar, click the + button, and paste the address of a podcast’s feed into the box. You can then stream the podcast’s episodes from within VLC.

vlc-subscribe-and-listen-to-podcast

Play Internet Radio

Before Pandora and Spotify, there were streaming Internet radio stations. There was a time when Internet users primarily streamed these radio stations from within Winamp, but they live on. You can view a searchable directory of radio stations from within VLC — just open the playlist and select Icecast Radio Directory. Perform a search for the kind of music you want to listen to or browse the list of free streaming radio stations.

Of course, VLC can also stream other Internet radio stations that aren’t included in this directory. You can generally find a “listen” link on their websites that will allow you to listen in a desktop player like VLC.

vlc-internet-radio-directory

Apply Video and Audio Effects

VLC can apply audio effects, video effects, and tweak the way a video’s audio and video line up. Click the Tools menu and select Effects and Filters. From here, you can apply an audio equalizer or video effects, such as cropping, rotating, overlaying, or colorizing a video. From the Synchronization tab, you can tweak the way a video’s audio and video stream line up. This allows you to fix broken videos where the audio and video are out of sync.

vlc-effects

As with other VLC features, these effects can be combined with other features. For example, you could permanently apply effects to a video by enabling these effects before using the Convert / Save feature.

ASCII Playback

ASCII playback isn’t a very useful feature, but it’s certainly an amusing one. In ASCII playback mode, VLC will display a video as ASCII characters rather than play it normally. It’s extremely impractical, but extremely geeky and worth a test drive if you want to surprise and amuse someone.

Click the Tools option in VLC, select Preferences, and click the Video icon. Click the Output box and select Color ASCII art video output. Save your settings, restart VLC, and start playing a new video. This feature works best with simple videos, such as cartoons with large sections of flat color.

vlc-ascii-art

After you’re done, go back into this window, click the Output box, and select Automatic to make VLC play videos normally.

Use a Video Wallpaper

VLC also allows you to set a video as your desktop wallpaper, replacing your desktop background with a video. It’s not very practical and extremely distracting, but hey — it’s something you can’t do with many media players.

To do this, open VLC’s preferences window, click the Video icon, and select DirectX (DirectDraw) video output in the Output box. Restart VLC, load a video, and you’ll be able to click the Video menu and select Set as Wallpaper to turn the video into your desktop wallpaper.

When you’re done, just change VLC’s Output setting back to Automatic and restart it.

VLC is also available for other platforms, giving you excellent compatibility with different media formats on almost any device. In addition to Windows, Mac, and Linux, VLC can run on an Android phone or tablet or an iOS device like an iPhone, iPad, or iPod touch. VLC will also soon be available for Windows 8′s Modern interface and Windows Phone.