Handling Arithmetic Right Shifts in ObjectPascal

In translating some DSP C++ code to ObjectPascal, I am seeing a quite a few ">>" operators on signed integers (the right operand always being 1 or 2). I know that on signed integers C++ performs an arithmetic right shift as opposed to a logical right shift in Pascal.

Currently I am substituting a 'div 2' or 'div 4' command for these ASR shifts. Is there a better, or more general, way to handle these?

Answers


Yes, assuming that the left operand is a 32-bit variable:

(x>>1)|0x80000000

(x>>2)|0xC0000000

And if you want the general case for x>>N:

(x>>N)|(((1<<N)-1)<<(32-N))

Need Your Help

change values in a list without a loop

python algorithm loops

I want to change the values of a list without using a loop or increase the performence

Why exactly is eval evil?

clojure scheme lisp common-lisp eval

I know that Lisp and Scheme programmers usually say that eval should be avoided unless strictly necessary. I’ve seen the same recommendation for several programming languages, but I’ve not yet seen a