I'd like to get the highest number with n bits in C++. I've written this piece of code but maybe there is a more efficient way.
int A = 22;  // 10110
int max = pow(2, (int) log2(A) + 1) - 1;  // returns 31 (11111)
This code raises 2 to the power of the number of bits of A and subtracts 1.
Edit
Since the question seems unclear here are some more examples to help understand the result I want to achieve.
- 1000=>- 1111
- 1010=>- 1111
- 100001=>- 111111
- 111=>- 111
 
     
     
     
     
    