Archive for January, 2007

More about bit-shifting and bitwise operations

calculator imageWe were recently asked why we couldn’t have a disc which allowed the user to pick any number of clips from any number available - like 16 from 16 possible, or 20 from 40 possible. Well, the answer is a bit complex, but in fact you *can* make a disc which allows a certain number of clips to be picked from all those available… but not everything is possible.

The answer lies in the way that DVDSP uses internal memory slots, the GPRMs. These are sixteen bits long and so you can fit a number up to 65535 inside a single register. However, it is not necessary to use all that space to store the value of a user choice. For example, if a user chose clip ‘9′ then that number can be held in just four bits. For ease we can divide the register into four sections and so store four user choices per register. The largest four bit number you can get is ‘15′, and so unless you want to allow your user to pick more than 15 clips from their menu, you only need to use four bits at a time.

However, imagine that your user had 24 clips to select from. And imagine that your user wanted to pick clip 24 to play. In this case the register would need to store the number ‘24′ and to do so it would need to use *five* bits (24 in binary is ‘11000′).

OK - this is a challenge, because you can then only store three user choices per register and have a single bit left over. You might naturally think that since we have got eight registers to play with you could easily accommodate all twenty-four clips, but you’d be wrong. Whilst we could store them, we also need memory slots to do our calculations in so that the user choices are placed in the correct spot along the registers, and we keep a count of the number chosen. In short, we need three registers to do the calculations in if we want to keep things short, leaving just five available. This means that you could store up to twenty ‘4-bit’ choices (i.e. no more than 15 clips, but the ability to repeat clips up to 20 times), or fifteen ‘5-bit’ choices. Since the greatest number that will fit in to five bits is 31, the maximum number of clips cannot exceed 31 on the menu.

So, it is a trade off - if we allow more than 15 clips to be on the menu, we need to use larger bit slots and this complicates the maths involved. If we use 15 clips or less we can easily get these into the registers four bits at a time, and we can store all of them in four GPRMs. This leaves four to do calculations in and track playback and other things. Easy.

I have found that the Calculator in OSX is an invaluable tool when doing this work. I used to use ‘Haxor’ to convert from decimal to binary, but Calculator is so much better. If you use it, go to the ‘view’ menu and choose ‘Programmer’ (or press Apple+3) and you get a display of the binary values above the buttons. Even better, you can click on those binary 1’s and 0’s to change them, and the calculator will update its display. This means it is easy to see what values to use in your scripts according to the bits you turn on and off.

The latest project was to pick fifteen from fifteen available. It works and you can download the resulting disk image here. Unfortunately, this was a commercial project and we cannot give the scripts away free - we will gladly build you a similar project if this is what you want. Please contact us here for more details.

selection menu screen