Question

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?

Was it helpful?

Solution

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))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top