prime:bitwise

# Bitwise Manipulation on the HP Prime.

BITSR(X) and BITSL(X,n) shift right and left respectively. The second argument says how much to shift. If not specified, it shifts only one position. Rotation and carry are not supported by default. Neither is bit test or selection. However, BITAND, BITNOT, BITOR and BITXOR are supported. BITNOT only takes one argument and inverts the bits. The other functions take as many arguments as you like and the specified logical operation is performed between the bits in all the integers specified.

Here are a group of useful functions to provide more traditional support for bit manipulation.

EXPORT BITLIB()
BEGIN
END;

EXPORT BIT(X,N) // GET NTH BIT OF X
BEGIN // LS BIT IS 0
RETURN BITSR(X,N);
END;

EXPORT BITSRC(X,N) // BITSR WITH CARRY
BEGIN // SET C + RETURN SHIFTED
C:=BITSR(X,N-1);
RETURN BITSR(X,N);
END;

EXPORT BITSLC(X,N) // BITSL WITH CARRY
BEGIN // SET C + RETURN SHIFTED
C:=BITSR(BITSL(X,N-1),GETBITS(X)-1);
RETURN BITSL(X,N);
END;

EXPORT BITROR(X) // ROTATE RIGHT
BEGIN
RETURN BITSR(X,1)+BITSL(X,GETBITS(X)-1);
END;

EXPORT BITROL(X) // ROTATE LEFT
BEGIN
RETURN BITSL(X,1)+BITSR(X,GETBITS(X)-1);
END;