Posts

Showing posts from February, 2013

The two faces of SHR

Like Marek Mauder in his blog post from 2009, I have run into a problem because of the different implementations of the right shift operator ( shr in Pascal) in different languages. There I was happily translating some code from Java to Object Pascal when I encountered Java's >>> operator. A quick look at the Java docs tells me this is a logical right shift. No problem, just use shr . Next I found Java's >> operator. This time its an arithmetic (sign preserving) right shift. Problem - no Pascal equivalent, and since operation was on signed integers I couldn't just use shr because there's a bug waiting to happen should any of the integers go negative. Marek (above) said that replacing a >> 1 with a div 2 ; worked for him. This is fine for a single bit shift, so I thought I would see if it scaled for shifts of more than one bit. I tried dividing by 2 to the power of the number of bits to shift: function SAR ( Value : LongInt ; Shift : Byte ...