📜  计算无分支的整数绝对值(abs)

📅  最后修改于: 2021-05-06 22:10:10             🧑  作者: Mango

如果数字为正,我们不需要做任何事情。我们只想更改负数。由于负数以2的补数形式存储,因此,要获取负数的绝对值,我们必须切换数字的位并将结果加1。
例如,在8位系统中的-2如下存储1 1 1 1 1 1 1 0 0,其中最左边的位是符号位。要获得负数的绝对值,我们必须切换所有位,并将1加到切换后的数字上,即0 0 0 0 0 0 0 0 1 + 1将给出绝对值1 1 1 1 1 1 1 0。还要记住,仅当数字为负(设置了符号位)时,才需要执行这些操作。
方法1
1)将掩码设置为整数右移31(假设整数使用32位存储)。

mask = n>>31 

2)对于负数,上述步骤将掩码设置为1 1 1 1 1 1 1 1 1和0 0 0 0 0 0 0 0(对于正数)。将掩码添加到给定的数字。

mask + n 

3)掩码+ n与掩码的XOR给出绝对值。

(mask + n)^mask 

执行:

C++
#include 
using namespace std;
#define CHARBIT 8
 
/* This function will return absolute value of n*/
unsigned int getAbs(int n)
{
    int const mask = n >> (sizeof(int) * CHARBIT - 1);
    return ((n + mask) ^ mask);
}
 
/* Driver program to test above function */
int main()
{
    int n = -6;
    cout << "Absoute value of " << n << " is " << getAbs(n);
    return 0;
}
 
// This code is contributed by rathbhupendra


C
#include 
#define CHAR_BIT 8
 
/* This function will return absolute value of n*/
unsigned int getAbs(int n)
{
    int const mask = n >> (sizeof(int) * CHAR_BIT - 1);
    return ((n + mask) ^ mask);
}
 
/* Driver program to test above function */
int main()
{
    int n = -6;
    printf("Absoute value of %d is %u", n, getAbs(n));
 
    getchar();
    return 0;
}


Java
// Java implementation of above approach
class GFG {
 
    static final int CHAR_BIT = 8;
    static final int SIZE_INT = 8;
 
    /* This function will return absolute value of n*/
    static int getAbs(int n)
    {
        int mask = n >> (SIZE_INT * CHAR_BIT - 1);
        return ((n + mask) ^ mask);
    }
 
    /* Driver code */
    public static void main(String[] args)
    {
        int n = -6;
        System.out.print("Absoute value of " + n + " is " + getAbs(n));
    }
}
 
// This code is contributed by Rajput-Ji


Python3
# Python3 implementation of above approach
CHARBIT = 8;
SIZE_INT = 8;
 
# This function will return
# absolute value of n
def getAbs(n):
    mask = n >> (SIZE_INT * CHARBIT - 1);
    return ((n + mask) ^ mask);
 
# Driver Code
n = -6;
print("Absolute value of",n,"is",getAbs(n));
 
# This code is contributed by mits


C#
// C# implementation of above approach
using System;
 
class GFG {
 
    static int CHAR_BIT = 8;
    static int SIZE_INT = 8;
 
    /* This function will return absolute value of n*/
    static int getAbs(int n)
    {
        int mask = n >> (SIZE_INT * CHAR_BIT - 1);
        return ((n + mask) ^ mask);
    }
 
    /* Driver code */
    static void Main()
    {
        int n = -6;
        Console.Write("Absoute value of " + n + " is " + getAbs(n));
    }
}
 
// This code is contributed by mits


PHP
> ($SIZE_INT * $CHARBIT - 1);
    return (($n + $mask) ^ $mask);
}
 
// Driver Code
$n = -6;
echo "Absolute value of " . $n .
            " is " . getAbs($n);
 
// This code is contributed by mits
?>


Javascript


c
/* This function will return absolute value of n*/
unsigned int getAbs(int n)
{
    int const mask = n >> (sizeof(int) * CHAR_BIT - 1);
    return ((n ^ mask) - mask);
}


输出:

Absolute value of -6 is 6

方法2:
1)将掩码设置为整数右移31(假设整数使用32位存储)。

mask = n>>31 

2)对数字与掩码进行异或

mask ^ n 

3)从步骤2的结果中减去掩码并返回结果。

(mask^n) - mask 

执行:

C

/* This function will return absolute value of n*/
unsigned int getAbs(int n)
{
    int const mask = n >> (sizeof(int) * CHAR_BIT - 1);
    return ((n ^ mask) - mask);
}