Java Unsigned Number Create unsignedToSigned(int unsigned, int size)

Here you can find the source of unsignedToSigned(int unsigned, int size)

Description

unsigned To Signed

License

Mozilla Public License

Declaration

private static int unsignedToSigned(int unsigned, int size) 

Method Source Code

//package com.java2s;
/***************************** BEGIN LICENSE BLOCK ***************************
    //from w  ww  .ja v  a2 s . c om
The contents of this file are subject to the Mozilla Public License, v. 2.0.
If a copy of the MPL was not distributed with this file, You can obtain one
at http://mozilla.org/MPL/2.0/.
    
Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
for the specific language governing rights and limitations under the License.
     
Copyright (C) 2012-2016 Sensia Software LLC. All Rights Reserved.
     
******************************* END LICENSE BLOCK ***************************/

public class Main {
    private static int unsignedToSigned(int unsigned, int size) {
        if ((unsigned & (1 << size - 1)) != 0)
            unsigned = -1 * ((1 << size - 1) - (unsigned & ((1 << size - 1) - 1)));
        return unsigned;
    }
}

Related

  1. unsignedShortToInt(byte[] b)
  2. unsignedShortToInt(final byte[] b)
  3. unsignedShortToInt(short n)
  4. unsignedSubOverflow(int operand1, int operand2)
  5. unsignedToBytes(byte b)
  6. unsignedToSigned(int[] ints)
  7. unsignedToSigned(int[] unsignedBytes)
  8. unsignedToSigned(long value, int size)
  9. unsignedToSigned16(char value)