Posted on by Kalkicode
Code Mathematics

Find the absolute value of integer without using branching

To find the absolute value of an integer without using branching, you can use the following step:

1. Get the size of an integer in bits by multiplying the size of an integer in bytes by 8 (assume). This will give the number of bits needed to represent an integer in memory.

2. Shift the integer to the right by `size-1` bits. This will move the sign bit of the integer to the rightmost position.

3. Add the original integer to the result of the previous step. If the original integer is positive, the sign bit will be 0 and the result will be unchanged. If the original integer is negative, the sign bit will be 1 and the result will be decreased by 1.

4. XOR the result of step 3 with the result of step 2. If the original integer is positive, the sign bit will be 0 and the XOR operation will have no effect. If the original integer is negative, the sign bit will be 1 and the XOR operation will flip all the bits of the result, effectively giving the absolute value.

Program Solution

``````// C program for
// Find the absolute value of integer without using branching
#include <stdio.h>

void absoluteValue(int number)
{

// Get the size of integer in bits
int size = sizeof(int) * 8;

// Shift the number by size of integer bits - 1
int result = number >> (size-1);

// Calculate absolute value
result = ( result + number ) ^ result;

// Display given number
printf(" Number   : %d\n", number);

// Display calculated result
printf(" Absolute : %d\n",result);
}

int main(int argc, char const *argv[])
{
// Test cases
absoluteValue(23);
absoluteValue(-2);
absoluteValue(5);
absoluteValue(-3);

return 0;
}``````

input

`````` Number   : 23
Absolute : 23
Number   : -2
Absolute : 2
Number   : 5
Absolute : 5
Number   : -3
Absolute : 3``````
``````/*
Java Program for
Find the absolute value of integer without using branching
*/
public class FindAbsolute
{
public void absoluteValue(int number)
{
// Get the size of integer in bits
int size = Integer.BYTES * 8;
// Right Shift the number by size of integer bits - 1
int result = number >> (size - 1);
// Calculate absolute value
result = (result + number) ^ result;
// Display given number
System.out.println(" Number : " + number);
// Display calculated result
System.out.println(" Absolute : " + result);
}
public static void main(String[] args)
{
FindAbsolute task = new FindAbsolute();
// Test cases
}
}``````

input

`````` Number : 23
Absolute : 23
Number : -2
Absolute : 2
Number : 5
Absolute : 5
Number : -3
Absolute : 3``````
``````// Include header file
#include <iostream>

using namespace std;
/*
C++ Program for
Find the absolute value of integer without using branching
*/
class FindAbsolute
{
public: void absoluteValue(int number)
{
// Get the size of integer in bits
int size = sizeof(int) * 8;
// Right Shift the number by size of integer bits - 1
int result = number >> (size - 1);
// Calculate absolute value
result = (result + number) ^ result;
// Display given number
cout << " Number : " << number << endl;
// Display calculated result
cout << " Absolute : " << result << endl;
}
};
int main()
{
FindAbsolute *task = new FindAbsolute();
// Test cases
return 0;
}``````

input

`````` Number : 23
Absolute : 23
Number : -2
Absolute : 2
Number : 5
Absolute : 5
Number : -3
Absolute : 3``````
``````// Include namespace system
using System;
/*
Csharp Program for
Find the absolute value of integer without using branching
*/
public class FindAbsolute
{
public void absoluteValue(int number)
{
// Get the size of integer in bits
int size = sizeof(int) * 8;
// Right Shift the number by size of integer bits - 1
int result = number >> (size - 1);
// Calculate absolute value
result = (result + number) ^ result;
// Display given number
Console.WriteLine(" Number : " + number);
// Display calculated result
Console.WriteLine(" Absolute : " + result);
}
public static void Main(String[] args)
{
FindAbsolute task = new FindAbsolute();
// Test cases
}
}``````

input

`````` Number : 23
Absolute : 23
Number : -2
Absolute : 2
Number : 5
Absolute : 5
Number : -3
Absolute : 3``````
``````<?php
/*
Php Program for
Find the absolute value of integer without using branching
*/
class FindAbsolute
{
public	function absoluteValue(\$number)
{
// Get the size of integer in bits
\$size = PHP_INT_SIZE * 8;
// Right Shift the number by size of integer bits - 1
\$result = \$number >> (\$size - 1);
// Calculate absolute value
\$result = (\$result + \$number) ^ \$result;
// Display given number
echo " Number : ".\$number.
"\n";
// Display calculated result
echo " Absolute : ".\$result.
"\n";
}
}

function main()
{
\$task = new FindAbsolute();
// Test cases
}
main();``````

input

`````` Number : 23
Absolute : 23
Number : -2
Absolute : 2
Number : 5
Absolute : 5
Number : -3
Absolute : 3``````
``````/*
Node JS Program for
Find the absolute value of integer without using branching
*/
class FindAbsolute
{
absoluteValue(number)
{
// Get the size of integer in bits
var size = Math.log2(Number.MAX_SAFE_INTEGER) * 8;
// Right Shift the number by size of integer bits - 1
var result = number >> (size - 1);
// Calculate absolute value
result = (result + number) ^ result;
// Display given number
console.log(" Number : " + number);
// Display calculated result
console.log(" Absolute : " + result);
}
}

function main()
{
var task = new FindAbsolute();
// Test cases
}
main();``````

input

`````` Number : 23
Absolute : 23
Number : -2
Absolute : 2
Number : 5
Absolute : 5
Number : -3
Absolute : 3``````
``````import sys
#  Python 3 Program for
#  Find the absolute value of integer without using branching
class FindAbsolute :
def absoluteValue(self, number) :
size = sys.getsizeof(int()) * 8
result = number >> (size - 1)
#  Calculate absolute value
result = (result + number) ^ result
#  Display given number
print(" Number : ", number)
#  Display calculated result
print(" Absolute : ", result)

def main() :
#  Test cases

if __name__ == "__main__": main()``````

input

`````` Number :  23
Absolute :  23
Number :  -2
Absolute :  2
Number :  5
Absolute :  5
Number :  -3
Absolute :  3``````
``````#  Ruby Program for
#  Find the absolute value of integer without using branching
class FindAbsolute
def absoluteValue(number)
#  Get the size of integer in bits
size = (0.size) * 8
#  Right Shift the number by size of integer bits - 1
result = number >> (size - 1)
#  Calculate absolute value
result = (result + number) ^ result
#  Display given number
print(" Number : ", number, "\n")
#  Display calculated result
print(" Absolute : ", result, "\n")
end

end

def main()
#  Test cases
end

main()``````

input

`````` Number : 23
Absolute : 23
Number : -2
Absolute : 2
Number : 5
Absolute : 5
Number : -3
Absolute : 3
``````
``````/*
Scala Program for
Find the absolute value of integer without using branching
*/
class FindAbsolute()
{
def absoluteValue(number: Int): Unit = {

// Right Shift the number by size of integer bits - 1
var result: Int = number >> (4 * 8 - 1);
// Calculate absolute value
result = (result + number) ^ result;
// Display given number
println(" Number : " + number);
// Display calculated result
println(" Absolute : " + result);
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: FindAbsolute = new FindAbsolute();
// Test cases
}
}``````

input

`````` Number : 23
Absolute : 23
Number : -2
Absolute : 2
Number : 5
Absolute : 5
Number : -3
Absolute : 3``````
``````/*
Swift 4 Program for
Find the absolute value of integer without using branching
*/
class FindAbsolute
{
func absoluteValue(_ number: Int)
{
// Get the size of integer in bits
let size: Int = MemoryLayout < Int >.size * 8;
// Right Shift the number by size of integer bits - 1
var result: Int = number >> (size - 1);
// Calculate absolute value
result = (result + number) ^ result;
// Display given number
print(" Number : ", number);
// Display calculated result
print(" Absolute : ", result);
}
}
func main()
{
let task: FindAbsolute = FindAbsolute();
// Test cases
}
main();``````

input

`````` Number :  23
Absolute :  23
Number :  -2
Absolute :  2
Number :  5
Absolute :  5
Number :  -3
Absolute :  3``````
``````/*
Kotlin Program for
Find the absolute value of integer without using branching
*/
class FindAbsolute
{
fun absoluteValue(number: Int): Unit
{
// Right Shift the number by size of integer bits - 1
var result: Int = number shr(4 * 8 - 1);
// Calculate absolute value
result = (result + number) xor result;
// Display given number
println(" Number : " + number);
// Display calculated result
println(" Absolute : " + result);
}
}
fun main(args: Array < String > ): Unit
{
val task: FindAbsolute = FindAbsolute();
// Test cases
}``````

input

`````` Number : 23
Absolute : 23
Number : -2
Absolute : 2
Number : 5
Absolute : 5
Number : -3
Absolute : 3``````

Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

Categories
Relative Post