Java Clamp clampAdd(long lhs, long rhs)

Here you can find the source of clampAdd(long lhs, long rhs)

Description

Add the supplied arguments and handle overflow by clamping the resulting sum to Long.MinValue if the sum would have been less than Long.MinValue or Long.MaxValue if the sum would have been greater than Long.MaxValue .

License

Apache License

Parameter

Parameter Description
lhs left hand side of sum
rhs right hand side of sum

Return

the sum if no overflow occurs, or the clamped extreme if it does.

Declaration

public static long clampAdd(long lhs, long rhs) 

Method Source Code

//package com.java2s;
/*//w w  w  .j  av a  2 s.  com
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

public class Main {
    /**
     * Add the supplied arguments and handle overflow by clamping the resulting sum to
     * {@code Long.MinValue} if the sum would have been less than {@code Long.MinValue} or
     * {@code Long.MaxValue} if the sum would have been greater than {@code Long.MaxValue}.
     *
     * @param lhs left hand side of sum
     * @param rhs right hand side of sum
     * @return the sum if no overflow occurs, or the clamped extreme if it does.
     */
    public static long clampAdd(long lhs, long rhs) {
        long sum = lhs + rhs;

        // From "Hacker's Delight", overflow occurs IFF both operands have the same sign and the
        // sign of the sum differs from the operands. Here we're doing a basic bitwise check that
        // collapses 6 branches down to 2. The expression {@code lhs ^ rhs} will have the high-order
        // bit set to true IFF the signs are different.
        if ((~(lhs ^ rhs) & (lhs ^ sum)) < 0) {
            if (lhs >= 0) {
                return Long.MAX_VALUE;
            } else {
                return Long.MIN_VALUE;
            }
        }

        return sum;
    }
}

Related

  1. clamp(final T MIN, final T MAX, final T VALUE)
  2. clamp(T min, T val, T max)
  3. clamp(T n, T l, T h)
  4. clamp(T value, T minimum, T maximum)
  5. clamp01(long value)