# HP Calculator Wiki

### Site Tools

prime:bitwise

This is an old revision of the document!

# 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
LOCAL r;
C:=BITSR(X,N-1);
r:=BITSR(X,N);
RETURN r;
END;

EXPORT BITSLC(X,N) // BITSL WITH CARRY
BEGIN // SET C + RETURN SHIFTED
LOCAL r;
C:=BITSR(BITSL(X,N-1),GETBITS(X)-1);
r:=BITSL(X,N);
RETURN r;
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;