We help IT Professionals succeed at work.

# get full value

on
Medium Priority
250 Views
I want a create a function

unsigned int GetFullVal ( unsigned int InValue)
{
...
}

that do the following:

input val (binary )  |   output val (binary)
1                    |   1
10                  |   11
100                |   111
1000              |   1111
10000            |   11111
...

I want an elegant solution ?

Thanks,

BT
Comment
Watch Question

## View Solution Only

Commented:
you want to fill in the bits given  2**n

unsigned int GetFullVal ( unsigned int a)
{
unsinged int bit;

if(a == (1 << 31))
return ~0;

for(bit = 1; bit  <= a; bit <<= 1) {

}

}

No check to confirm a is a power of 2.
Need the if at the start to process 2 ** 31. (assuming sizeof(int) == 4) probably replace
31 with ((sizeof(unsigned int) * 8) - 1.

Commented:
if (in == (1 << 31)) return ~0 ;
else return (in << 1) - 1 ;

it should work if in is a power of 2

Not the solution you were looking for? Getting a personalized solution is easy.

##### Thanks for using Experts Exchange.

• View three pieces of content (articles, solutions, posts, and videos)
• Ask the experts questions (counted toward content limit)
• Customize your dashboard and profile