static Object bigIntegerBinaryOperation ( BigInteger lhs , BigInteger rhs , int kind ) { switch ( kind ) { // arithmetic case PLUS : return lhs . add ( rhs ) ; case MINUS : return lhs . subtract ( rhs ) ; case STAR : return lhs . multiply ( rhs ) ; case SLASH : return lhs . divide ( rhs ) ; case MOD : case MODX : return lhs . mod ( rhs ) ; case POWER : case POWERX : return lhs . pow ( rhs . intValue ( ) ) ; // bitwise case LSHIFT : case LSHIFTX : return lhs . shiftLeft ( rhs . intValue ( ) ) ; case RSIGNEDSHIFT : case RSIGNEDSHIFTX : return lhs . shiftRight ( rhs . intValue ( ) ) ; case RUNSIGNEDSHIFT : case RUNSIGNEDSHIFTX : if ( lhs . signum ( ) >= 0 ) return lhs . shiftRight ( rhs . intValue ( ) ) ; BigInteger opener = BigInteger . ONE . shiftLeft ( lhs . toString ( 2 ) . length ( ) + 1 ) ; BigInteger opened = lhs . subtract ( opener ) ; BigInteger mask = opener . subtract ( BigInteger . ONE ) . shiftRight ( rhs . intValue ( ) + 1 ) ; return opened . shiftRight ( rhs . intValue ( ) ) . and ( mask ) ; case BIT_AND : case BIT_ANDX : return lhs . and ( rhs ) ; case BIT_OR : case BIT_ORX : return lhs . or ( rhs ) ; case XOR : case XORX : return lhs . xor ( rhs ) ; } throw new InterpreterError ( "Unimplemented binary integer operator" ) ; }
returns Object covering both Integer and Boolean return types