Example usage for java.lang Byte toUnsignedLong

List of usage examples for java.lang Byte toUnsignedLong

Introduction

In this page you can find the example usage for java.lang Byte toUnsignedLong.

Prototype

public static long toUnsignedLong(byte x) 

Source Link

Document

Converts the argument to a long by an unsigned conversion.

Usage

From source file:Main.java

public static void main(String... args) {
    byte b = 2;//from w  ww  . ja  v  a 2  s  . c  o  m
    System.out.println(Byte.toUnsignedLong(b));

    b = -2;
    System.out.println(Byte.toUnsignedLong(b));
}

From source file:org.ballerinalang.bre.bvm.BVM.java

private static void execLoadOpcodes(Strand ctx, StackFrame sf, int opcode, int[] operands) {
    int i;// w w w  .j  av a  2  s  .c o  m
    int j;
    int k;
    int pkgIndex;
    int lvIndex; // Index of the local variable

    BValueArray bValueArray;
    BMap<String, BRefType> bMap;

    switch (opcode) {
    case InstructionCodes.IMOVE:
        lvIndex = operands[0];
        i = operands[1];
        sf.longRegs[i] = sf.longRegs[lvIndex];
        break;
    case InstructionCodes.FMOVE:
        lvIndex = operands[0];
        i = operands[1];
        sf.doubleRegs[i] = sf.doubleRegs[lvIndex];
        break;
    case InstructionCodes.SMOVE:
        lvIndex = operands[0];
        i = operands[1];
        sf.stringRegs[i] = sf.stringRegs[lvIndex];
        break;
    case InstructionCodes.BMOVE:
        lvIndex = operands[0];
        i = operands[1];
        sf.intRegs[i] = sf.intRegs[lvIndex];
        break;
    case InstructionCodes.RMOVE:
        lvIndex = operands[0];
        i = operands[1];
        sf.refRegs[i] = sf.refRegs[lvIndex];
        break;
    case InstructionCodes.IALOAD:
        i = operands[0];
        j = operands[1];
        k = operands[2];
        bValueArray = Optional.of((BValueArray) sf.refRegs[i]).get();
        try {
            sf.longRegs[k] = bValueArray.getInt(sf.longRegs[j]);
        } catch (BallerinaException e) {
            ctx.setError(BLangVMErrors.createError(ctx, e.getMessage(), e.getDetail()));
            handleError(ctx);
        }
        break;
    case InstructionCodes.BIALOAD:
        i = operands[0];
        j = operands[1];
        k = operands[2];
        bValueArray = Optional.of((BValueArray) sf.refRegs[i]).get();
        try {
            sf.longRegs[k] = Byte.toUnsignedLong(bValueArray.getByte(sf.longRegs[j]));
        } catch (BallerinaException e) {
            ctx.setError(BLangVMErrors.createError(ctx, e.getMessage(), e.getDetail()));
            handleError(ctx);
        }
        break;
    case InstructionCodes.FALOAD:
        i = operands[0];
        j = operands[1];
        k = operands[2];
        bValueArray = Optional.of((BValueArray) sf.refRegs[i]).get();
        try {
            sf.doubleRegs[k] = bValueArray.getFloat(sf.longRegs[j]);
        } catch (BallerinaException e) {
            ctx.setError(BLangVMErrors.createError(ctx, e.getMessage(), e.getDetail()));
            handleError(ctx);
        }
        break;
    case InstructionCodes.SALOAD:
        i = operands[0];
        j = operands[1];
        k = operands[2];
        bValueArray = Optional.of((BValueArray) sf.refRegs[i]).get();
        try {
            sf.stringRegs[k] = bValueArray.getString(sf.longRegs[j]);
        } catch (BallerinaException e) {
            ctx.setError(BLangVMErrors.createError(ctx, e.getMessage(), e.getDetail()));
            handleError(ctx);
        }
        break;
    case InstructionCodes.BALOAD:
        i = operands[0];
        j = operands[1];
        k = operands[2];
        bValueArray = Optional.of((BValueArray) sf.refRegs[i]).get();
        try {
            sf.intRegs[k] = bValueArray.getBoolean(sf.longRegs[j]);
        } catch (BallerinaException e) {
            ctx.setError(BLangVMErrors.createError(ctx, e.getMessage(), e.getDetail()));
            handleError(ctx);
        }
        break;
    case InstructionCodes.RALOAD:
        i = operands[0];
        j = operands[1];
        k = operands[2];
        BNewArray bNewArray = Optional.of((BNewArray) sf.refRegs[i]).get();
        try {
            sf.refRegs[k] = ListUtils.execListGetOperation(bNewArray, sf.longRegs[j]);
        } catch (BallerinaException e) {
            ctx.setError(BLangVMErrors.createError(ctx, e.getMessage(), e.getDetail()));
            handleError(ctx);
        }
        break;
    case InstructionCodes.JSONALOAD:
        i = operands[0];
        j = operands[1];
        k = operands[2];

        try {
            sf.refRegs[k] = JSONUtils.getArrayElement(sf.refRegs[i], sf.longRegs[j]);
        } catch (BallerinaException e) {
            ctx.setError(BLangVMErrors.createError(ctx, e.getMessage(), e.getDetail()));
            handleError(ctx);
        }
        break;
    case InstructionCodes.IGLOAD:
        // package index
        pkgIndex = operands[0];
        // package level variable index
        i = operands[1];
        // Stack registry index
        j = operands[2];
        sf.longRegs[j] = ctx.programFile.globalMemArea.getIntField(pkgIndex, i);
        break;
    case InstructionCodes.FGLOAD:
        pkgIndex = operands[0];
        i = operands[1];
        j = operands[2];
        sf.doubleRegs[j] = ctx.programFile.globalMemArea.getFloatField(pkgIndex, i);
        break;
    case InstructionCodes.SGLOAD:
        pkgIndex = operands[0];
        i = operands[1];
        j = operands[2];
        sf.stringRegs[j] = ctx.programFile.globalMemArea.getStringField(pkgIndex, i);
        break;
    case InstructionCodes.BGLOAD:
        pkgIndex = operands[0];
        i = operands[1];
        j = operands[2];
        sf.intRegs[j] = ctx.programFile.globalMemArea.getBooleanField(pkgIndex, i);
        break;
    case InstructionCodes.RGLOAD:
        pkgIndex = operands[0];
        i = operands[1];
        j = operands[2];
        sf.refRegs[j] = ctx.programFile.globalMemArea.getRefField(pkgIndex, i);
        break;

    case InstructionCodes.MAPLOAD:
        i = operands[0];
        j = operands[1];
        k = operands[2];
        bMap = (BMap<String, BRefType>) sf.refRegs[i];
        if (bMap == null) {
            handleNullRefError(ctx);
            break;
        }

        IntegerCPEntry exceptCPEntry = (IntegerCPEntry) sf.constPool[operands[3]];
        boolean except = exceptCPEntry.getValue() == 1;
        try {
            sf.refRegs[k] = bMap.get(sf.stringRegs[j], except);
        } catch (BallerinaException e) {
            ctx.setError(BLangVMErrors.createError(ctx, e.getMessage(), e.getDetail()));
            handleError(ctx);
        }
        break;

    case InstructionCodes.JSONLOAD:
        i = operands[0];
        j = operands[1];
        k = operands[2];
        sf.refRegs[k] = JSONUtils.getElement(sf.refRegs[i], sf.stringRegs[j]);
        break;
    default:
        throw new UnsupportedOperationException();
    }
}

From source file:org.ballerinalang.bre.bvm.BVM.java

private static void execBinaryOpCodes(Strand ctx, StackFrame sf, int opcode, int[] operands) {
    int i;/*  w ww  .  jav a  2 s .  c  om*/
    int j;
    int k;
    BDecimal lhsValue;
    BDecimal rhsValue;

    switch (opcode) {
    case InstructionCodes.IADD:
        i = operands[0];
        j = operands[1];
        k = operands[2];
        sf.longRegs[k] = sf.longRegs[i] + sf.longRegs[j];
        break;
    case InstructionCodes.FADD:
        i = operands[0];
        j = operands[1];
        k = operands[2];
        sf.doubleRegs[k] = sf.doubleRegs[i] + sf.doubleRegs[j];
        break;
    case InstructionCodes.SADD:
        i = operands[0];
        j = operands[1];
        k = operands[2];
        sf.stringRegs[k] = sf.stringRegs[i] + sf.stringRegs[j];
        break;
    case InstructionCodes.DADD:
        i = operands[0];
        j = operands[1];
        k = operands[2];
        lhsValue = (BDecimal) sf.refRegs[i];
        rhsValue = (BDecimal) sf.refRegs[j];
        sf.refRegs[k] = lhsValue.add(rhsValue);
        break;
    case InstructionCodes.XMLADD:
        i = operands[0];
        j = operands[1];
        k = operands[2];
        BXML lhsXMLVal = (BXML) sf.refRegs[i];
        BXML rhsXMLVal = (BXML) sf.refRegs[j];

        // Here it is assumed that a refType addition can only be a xml-concat.
        sf.refRegs[k] = XMLUtils.concatenate(lhsXMLVal, rhsXMLVal);
        break;
    case InstructionCodes.ISUB:
        i = operands[0];
        j = operands[1];
        k = operands[2];
        sf.longRegs[k] = sf.longRegs[i] - sf.longRegs[j];
        break;
    case InstructionCodes.FSUB:
        i = operands[0];
        j = operands[1];
        k = operands[2];
        sf.doubleRegs[k] = sf.doubleRegs[i] - sf.doubleRegs[j];
        break;
    case InstructionCodes.DSUB:
        i = operands[0];
        j = operands[1];
        k = operands[2];
        lhsValue = (BDecimal) sf.refRegs[i];
        rhsValue = (BDecimal) sf.refRegs[j];
        sf.refRegs[k] = lhsValue.subtract(rhsValue);
        break;
    case InstructionCodes.IMUL:
        i = operands[0];
        j = operands[1];
        k = operands[2];
        sf.longRegs[k] = sf.longRegs[i] * sf.longRegs[j];
        break;
    case InstructionCodes.FMUL:
        i = operands[0];
        j = operands[1];
        k = operands[2];
        sf.doubleRegs[k] = sf.doubleRegs[i] * sf.doubleRegs[j];
        break;
    case InstructionCodes.DMUL:
        i = operands[0];
        j = operands[1];
        k = operands[2];
        lhsValue = (BDecimal) sf.refRegs[i];
        rhsValue = (BDecimal) sf.refRegs[j];
        sf.refRegs[k] = lhsValue.multiply(rhsValue);
        break;
    case InstructionCodes.IDIV:
        i = operands[0];
        j = operands[1];
        k = operands[2];
        if (sf.longRegs[j] == 0) {
            ctx.setError(
                    BLangVMErrors.createError(ctx, BallerinaErrorReasons.DIVISION_BY_ZERO_ERROR, " / by zero"));
            handleError(ctx);
            break;
        }

        sf.longRegs[k] = sf.longRegs[i] / sf.longRegs[j];
        break;
    case InstructionCodes.FDIV:
        i = operands[0];
        j = operands[1];
        k = operands[2];
        sf.doubleRegs[k] = sf.doubleRegs[i] / sf.doubleRegs[j];
        break;
    case InstructionCodes.DDIV:
        i = operands[0];
        j = operands[1];
        k = operands[2];
        lhsValue = (BDecimal) sf.refRegs[i];
        rhsValue = (BDecimal) sf.refRegs[j];
        sf.refRegs[k] = lhsValue.divide(rhsValue);
        break;
    case InstructionCodes.IMOD:
        i = operands[0];
        j = operands[1];
        k = operands[2];
        if (sf.longRegs[j] == 0) {
            ctx.setError(
                    BLangVMErrors.createError(ctx, BallerinaErrorReasons.DIVISION_BY_ZERO_ERROR, " / by zero"));
            handleError(ctx);
            break;
        }

        sf.longRegs[k] = sf.longRegs[i] % sf.longRegs[j];
        break;
    case InstructionCodes.FMOD:
        i = operands[0];
        j = operands[1];
        k = operands[2];
        sf.doubleRegs[k] = sf.doubleRegs[i] % sf.doubleRegs[j];
        break;
    case InstructionCodes.DMOD:
        i = operands[0];
        j = operands[1];
        k = operands[2];
        lhsValue = (BDecimal) sf.refRegs[i];
        rhsValue = (BDecimal) sf.refRegs[j];
        sf.refRegs[k] = lhsValue.remainder(rhsValue);
        break;
    case InstructionCodes.INEG:
        i = operands[0];
        j = operands[1];
        sf.longRegs[j] = -sf.longRegs[i];
        break;
    case InstructionCodes.FNEG:
        i = operands[0];
        j = operands[1];
        sf.doubleRegs[j] = -sf.doubleRegs[i];
        break;
    case InstructionCodes.DNEG:
        i = operands[0];
        j = operands[1];
        BigDecimal value = ((BDecimal) sf.refRegs[i]).decimalValue();
        sf.refRegs[j] = new BDecimal(value.negate());
        break;
    case InstructionCodes.BNOT:
        i = operands[0];
        j = operands[1];
        sf.intRegs[j] = sf.intRegs[i] == 0 ? 1 : 0;
        break;
    case InstructionCodes.IEQ:
        i = operands[0];
        j = operands[1];
        k = operands[2];
        sf.intRegs[k] = sf.longRegs[i] == sf.longRegs[j] ? 1 : 0;
        break;
    case InstructionCodes.FEQ:
        i = operands[0];
        j = operands[1];
        k = operands[2];
        sf.intRegs[k] = sf.doubleRegs[i] == sf.doubleRegs[j] ? 1 : 0;
        break;
    case InstructionCodes.SEQ:
        i = operands[0];
        j = operands[1];
        k = operands[2];
        sf.intRegs[k] = StringUtils.isEqual(sf.stringRegs[i], sf.stringRegs[j]) ? 1 : 0;
        break;
    case InstructionCodes.BEQ:
        i = operands[0];
        j = operands[1];
        k = operands[2];
        sf.intRegs[k] = sf.intRegs[i] == sf.intRegs[j] ? 1 : 0;
        break;
    case InstructionCodes.DEQ:
        i = operands[0];
        j = operands[1];
        k = operands[2];
        lhsValue = (BDecimal) sf.refRegs[i];
        rhsValue = (BDecimal) sf.refRegs[j];
        sf.intRegs[k] = isDecimalRealNumber(lhsValue) && isDecimalRealNumber(rhsValue)
                && lhsValue.decimalValue().compareTo(rhsValue.decimalValue()) == 0 ? 1 : 0;
        break;
    case InstructionCodes.REQ:
        i = operands[0];
        j = operands[1];
        k = operands[2];
        if (sf.refRegs[i] == null) {
            sf.intRegs[k] = sf.refRegs[j] == null ? 1 : 0;
        } else {
            sf.intRegs[k] = isEqual(sf.refRegs[i], sf.refRegs[j], new ArrayList<>()) ? 1 : 0;
        }
        break;
    case InstructionCodes.REF_EQ:
        i = operands[0];
        j = operands[1];
        k = operands[2];
        sf.intRegs[k] = isReferenceEqual(sf.refRegs[i], sf.refRegs[j]) ? 1 : 0;
        break;
    case InstructionCodes.TEQ:
        i = operands[0];
        j = operands[1];
        k = operands[2];
        if (sf.refRegs[i] == null || sf.refRegs[j] == null) {
            handleNullRefError(ctx);
            break; //TODO is this correct?
        }
        sf.intRegs[k] = sf.refRegs[i].equals(sf.refRegs[j]) ? 1 : 0;
        break;

    case InstructionCodes.INE:
        i = operands[0];
        j = operands[1];
        k = operands[2];
        sf.intRegs[k] = sf.longRegs[i] != sf.longRegs[j] ? 1 : 0;
        break;
    case InstructionCodes.FNE:
        i = operands[0];
        j = operands[1];
        k = operands[2];
        sf.intRegs[k] = sf.doubleRegs[i] != sf.doubleRegs[j] ? 1 : 0;
        break;
    case InstructionCodes.SNE:
        i = operands[0];
        j = operands[1];
        k = operands[2];
        sf.intRegs[k] = !StringUtils.isEqual(sf.stringRegs[i], sf.stringRegs[j]) ? 1 : 0;
        break;
    case InstructionCodes.BNE:
        i = operands[0];
        j = operands[1];
        k = operands[2];
        sf.intRegs[k] = sf.intRegs[i] != sf.intRegs[j] ? 1 : 0;
        break;
    case InstructionCodes.DNE:
        i = operands[0];
        j = operands[1];
        k = operands[2];
        lhsValue = (BDecimal) sf.refRegs[i];
        rhsValue = (BDecimal) sf.refRegs[j];
        sf.intRegs[k] = !isDecimalRealNumber(lhsValue) || !isDecimalRealNumber(rhsValue)
                || lhsValue.decimalValue().compareTo(rhsValue.decimalValue()) != 0 ? 1 : 0;
        break;
    case InstructionCodes.RNE:
        i = operands[0];
        j = operands[1];
        k = operands[2];
        if (sf.refRegs[i] == null) {
            sf.intRegs[k] = (sf.refRegs[j] != null) ? 1 : 0;
        } else {
            sf.intRegs[k] = (!isEqual(sf.refRegs[i], sf.refRegs[j], new ArrayList<>())) ? 1 : 0;
        }
        break;
    case InstructionCodes.REF_NEQ:
        i = operands[0];
        j = operands[1];
        k = operands[2];
        sf.intRegs[k] = isReferenceInequal(sf.refRegs[i], sf.refRegs[j]) ? 1 : 0;
        break;
    case InstructionCodes.TNE:
        i = operands[0];
        j = operands[1];
        k = operands[2];
        if (sf.refRegs[i] == null || sf.refRegs[j] == null) {
            handleNullRefError(ctx);
            break; //TODO is this correct?
        }
        sf.intRegs[k] = (!sf.refRegs[i].equals(sf.refRegs[j])) ? 1 : 0;
        break;
    case InstructionCodes.IAND:
        i = operands[0];
        j = operands[1];
        k = operands[2];
        sf.longRegs[k] = sf.longRegs[i] & sf.longRegs[j];
        break;
    case InstructionCodes.IOR:
        i = operands[0];
        j = operands[1];
        k = operands[2];
        sf.longRegs[k] = sf.longRegs[i] | sf.longRegs[j];
        break;
    case InstructionCodes.IXOR:
        i = operands[0];
        j = operands[1];
        k = operands[2];
        sf.longRegs[k] = sf.longRegs[i] ^ sf.longRegs[j];
        break;
    case InstructionCodes.BILSHIFT:
        i = operands[0];
        j = operands[1];
        k = operands[2];
        sf.longRegs[k] = Byte.toUnsignedLong((byte) (sf.longRegs[i] << sf.longRegs[j]));
        break;
    case InstructionCodes.BIRSHIFT:
        i = operands[0];
        j = operands[1];
        k = operands[2];
        sf.longRegs[k] = Byte.toUnsignedLong((byte) ((byte) sf.longRegs[i] >> sf.longRegs[j]));
        break;
    case InstructionCodes.IRSHIFT:
        i = operands[0];
        j = operands[1];
        k = operands[2];
        sf.longRegs[k] = sf.longRegs[i] >> sf.longRegs[j];
        break;
    case InstructionCodes.ILSHIFT:
        i = operands[0];
        j = operands[1];
        k = operands[2];
        sf.longRegs[k] = sf.longRegs[i] << sf.longRegs[j];
        break;
    case InstructionCodes.IURSHIFT:
        i = operands[0];
        j = operands[1];
        k = operands[2];
        sf.longRegs[k] = sf.longRegs[i] >>> sf.longRegs[j];
        break;
    case InstructionCodes.TYPE_TEST:
        i = operands[0];
        j = operands[1];
        k = operands[2];
        TypeRefCPEntry typeRefCPEntry = (TypeRefCPEntry) sf.constPool[j];
        sf.intRegs[k] = checkIsType(sf.refRegs[i], typeRefCPEntry.getType()) ? 1 : 0;
        break;
    case InstructionCodes.IS_LIKE:
        i = operands[0];
        j = operands[1];
        k = operands[2];
        typeRefCPEntry = (TypeRefCPEntry) sf.constPool[j];
        sf.intRegs[k] = checkIsLikeType(sf.refRegs[i], typeRefCPEntry.getType()) ? 1 : 0;
        break;
    default:
        throw new UnsupportedOperationException();
    }
}