# Find the absolute value of integer without using branching

Here given code implementation process.

``````// 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)
{
// 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()
{
// 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)
{
// 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()
{
// 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()
{
// 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()
{
// 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
{
// 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.