"Gloria, Jing" wrote:
Has anybody implemented a function to do Logical Shift Right operations on an integer in GNU PASCAL? SHR operator will do an arithmetic shift right. Or any suggestions on how to do this?
I am using gpc version 20010623, based on 2.95.2 19991024 (release) on Solaris 7 in an Intel box..
Please don't use HTML.
Not especially fast, but portable on 2's complement machines:
FUNCTION lsr(i : integer) : integer; (* Avoiding all integer overflows *)
BEGIN (* lsr *) IF i >= 0 THEN lsr := i DIV 2 (* usual case *) ELSE IF (i + 1) = -maxint THEN lsr := maxint ELSE BEGIN i := (i + maxint + 1) DIV 2; (* now positive *) lsr := i + (maxint DIV 2) + 1; END; END; (* lsr - UNTESTED *)