# Computing INT_MAX and INT_MIN with Bitwise operations

Prerequisites :
INT_MAX and INT_MIN in C/C++ and Applications.
Arithmetic shift vs Logical shift
Suppose you have a 32-bit system :
The INT_MAX would be 01111111111111111111111111111111 and INT_MIN would be 10000000000000000000000000000000. 0 & 1 in most-significant bit position representing the sign bit respectively.
Computing INT_MAX and INT_MIN In C/C++ :
The number 0 is represented as 000…000(32 times).

• We compute the NOT of 0 to get a number with 32 1s. This number is not equal to INT_MAX because the sign bit is 1, i.e. negative number.
• Now, a right shift of this number will produce 011…111 which is INT_MAX.
• INT_MIN is NOT of INT_MAX.

Note :
0 should be taken as unsigned int.
Reason :
If 0 is signed, during Step 2, right shift of 111..111 will yield 111…111. This is because arithmetic right shift preserves the sign of the number.
In Java, we have the feature of logical right shift available to us.

## C++

 `// CPP code to compute INT_MAX and INT_MIN using ` `// bitwise operations ` `#include ` `using` `namespace` `std; `   `void` `printMinMaxValues() ` `{ ` `    ``// 0 saved as unsigned int ` `    ``unsigned ``int` `max = 0; `   `    ``// Computing NOT of 0 ` `    ``max = ~max; ` `    `  `    ``// 1 time arithmetic right shift ` `    ``max = max >> 1; ` `    `  `    ``// Computing INT_MIN ` `    ``int` `min = max; ` `    `  `    ``// INT_MIN = ~INT_MAX ` `    ``min = ~min; ` `    `  `    ``// Printing the result ` `    ``cout << ``"INT_MAX : "` `<< max ` `        ``<< ``" INT_MIN : "` `<< min; ` `} `   `// Driver code ` `int` `main() ` `{ ` `    ``printMinMaxValues(); ` `    ``return` `0; ` `} `

## Java

 `// Java code to compute INT_MAX and INT_MIN using ` `// bitwise operations` `public` `class` `Solution ` `{` `    ``static` `void` `printMinMaxValues()` `    ``{` `        ``int` `max = ``0``;` `    `  `        ``// Computing NOT of 0` `        ``max = ~max;`   `        ``// 1 time logical right shift for INT_MAX` `        ``max = max >>> ``1``;` `        `  `        ``// Computing INT_MIN` `        ``int` `min = max;` `        `  `        ``// INT_MIN = ~INT_MAX` `        ``min = ~max;` `    `  `        ``// Printing the result` `        ``System.out.println(``"INT_MAX "` `+ max + ` `                           ``" INT_MIN "` `+ min);` `    ``}`   `    ``public` `static` `void` `main(String[] args) ` `    ``{` `        ``printMinMaxValues();` `    ``}` `}`

## Python3

 `# Python3 code to compute INT_MAX and INT_MIN using ` `# bitwise operations ` `def` `printMinMaxValues(): ` ` `  `    ``# 0 saved as unsigned int ` `    ``max` `=` `0`   `    ``# Computing NOT of 0 ` `    ``#to signed integer to unsigned integer` `    ``#in Python3, add 1 << 32 to the integer` `    ``max` `=` `~``max` `+` `(``1` `<< ``32``)` `    `  `    ``# 1 time arithmetic right shift ` `    ``max` `=` `max` `>> ``1` `    `  `    ``# Computing INT_MIN ` `    ``min` `=` `max``;` `    `  `    ``# INT_MIN = ~INT_MAX ` `    ``min` `=` `~``min` `    `  `    ``# Printing the result ` `    ``print``(``"INT_MAX :"``, ``max``, ``"INT_MIN :"``, ``min``) `   `# Driver code ` `printMinMaxValues()`   `# This code is contributed by phasing17`

## C#

 `// C# code to compute INT_MAX and INT_MIN using ` `// bitwise operations`   `using` `System;`   `public` `class` `GFG` `{` `    ``static` `void` `printMinMaxValues()` `    ``{` `        ``// 0 saved as unsigned int ` `        ``uint` `max = 0; ` `    `  `        ``// Computing NOT of 0 ` `        ``max = ~max; ` `        `  `        ``// 1 time arithmetic right shift ` `        ``max = max >> 1; ` `        `  `        ``// Computing INT_MIN ` `        ``int` `min = (``int``)max; ` `        `  `        ``// INT_MIN = ~INT_MAX ` `        ``min = ~min; ` `    `  `        ``// Printing the result` `        ``Console.WriteLine(``"INT_MAX "` `+ max + ` `                           ``" INT_MIN "` `+ min);` `    ``}` `    `  `    ``//Driver code` `    ``public` `static` `void` `Main(``string``[] args)` `    ``{` `        ``//Function call` `        ``printMinMaxValues();` `    ``}` `}`   `//This code is contributed by phasing17`

## Javascript

 ``

Output:

`INT_MAX 2147483647 INT_MIN -2147483648`

Time Complexity – O(1)

Space Complexity – O(1)