문제

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?

도움이 되었습니까?

해결책

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))
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top