Here you can find the source of negate(int[] input)
public static int[] negate(int[] input)
//package com.java2s; /*/*from ww w . ja v a 2 s . co m*/ * Copyright (c) 2009 Stephan Schloepke and innoQ Deutschland GmbH * * Stephan Schloepke: http://www.schloepke.de/ * innoQ Deutschland GmbH: http://www.innoq.com/ * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ public class Main { public static final int[] ZERO_INT_ARRAY = new int[0]; public static final byte[] ZERO_BYTE_ARRAY = new byte[0]; public static int[] negate(int[] input) { int x = input[0]; int j = input.length; if (x == 0) { // if we have a carry propagation it is possible we raise for one element // actually this should rarely happens since it only does happens if the input // is not stripped or the follow up is exactly -1 so that a propagation occurs. j++; } else if (x < 0 && (x >>> 1) > 0) { j++; } int[] result = new int[j]; int i = input.length; boolean carry = true; while (i > 0 && carry) { result[--j] = (~input[--i]) + 1; carry = result[j] == 0; } while (i > 0) { result[--j] = ~input[--i]; } if (j > 0 && carry) { result[--j] = 1; } else if (j > 0 || result[j] == 0) { // we need to shrink the array j = 0; while (j < result.length && result[j] == 0) { j++; } if (result.length == j) { return ZERO_INT_ARRAY; } if (result[j] < 0) { j--; } if (j > 0) { int[] t = new int[result.length - j]; System.arraycopy(result, j, t, 0, t.length); return t; } } return result; } public static byte[] negate(byte[] input) { int x = input[0]; int j = input.length; if (x == 0) { // if we have a carry propagation it is possible we raise for one element // actually this should rarely happens since it only does happens if the input // is not stripped or the follow up is exactly -1 so that a propagation occurs. j++; } else if (x < 0 && (x >>> 1) > 0) { j++; } byte[] result = new byte[j]; int i = input.length; boolean carry = true; while (i > 0 && carry) { result[--j] = (byte) ((~input[--i]) + 1); carry = result[j] == 0; } while (i > 0) { result[--j] = (byte) ~input[--i]; } if (j > 0 && carry) { result[--j] = 1; } else if (j > 0 || result[j] == 0) { // we need to shrink the array j = 0; while (j < result.length && result[j] == 0) { j++; } if (result.length == j) { return ZERO_BYTE_ARRAY; } if (result[j] < 0) { j--; } if (j > 0) { byte[] t = new byte[result.length - j]; System.arraycopy(result, j, t, 0, t.length); return t; } } return result; } }