Java Unsigned Number Create unsignedAddOverflow(int operand1, int operand2)

Here you can find the source of unsignedAddOverflow(int operand1, int operand2)

Description

Returns true, if the addition of both operands as unsigned integers will cause an overflow.

License

Open Source License

Declaration

public static boolean unsignedAddOverflow(int operand1, int operand2) 

Method Source Code

//package com.java2s;
/*/* www .  java2  s  .c  o m*/
 *  This file is part of MRP (http://mrp.codehaus.org/).
 *
 *  This file is licensed to You under the Eclipse Public License (EPL);
 *  You may not use this file except in compliance with the License. You
 *  may obtain a copy of the License at
 *
 *      http://www.opensource.org/licenses/eclipse-1.0.php
 *
 *  See the COPYRIGHT.txt file distributed with this work for information
 *  regarding copyright ownership.
 */

public class Main {
    /** 
     * Returns true, if the addition of both operands as unsigned integers will cause an overflow.
     * At the moment, this converts both values to longs and checks if the 33rd bit (which actually represents the carry)
     * overflows.
     */
    public static boolean unsignedAddOverflow(int operand1, int operand2) {
        long value1 = (long) operand1 & 0xFFFFFFFFL;
        long value2 = (long) operand2 & 0xFFFFFFFFL;

        return ((value1 + value2) & 0x100000000L) != 0;
    }
}

Related

  1. unsigned32ToBytes(long value)
  2. unsigned32ToInt(byte[] bytes)
  3. unsigned4BytesToInt(byte[] buf, int pos)
  4. unsigned8(byte b)
  5. unsigned_byte(byte b)
  6. unsignedArrayCompareLex(byte[] a, byte[] b)
  7. unsignedArrayCompareLex(char[] a, char[] b)
  8. unsignedBinarySearch(final short[] array, final int begin, final int end, final short k)
  9. unsignedByte(int b)