[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

get full value

Posted on 2006-03-25
2
Medium Priority
?
236 Views
Last Modified: 2010-04-15
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                  
0
Comment
Question by:bachra04
2 Comments
 
LVL 3

Expert Comment

by:leisner
ID: 16290468
you want to fill in the bits given  2**n


unsigned int GetFullVal ( unsigned int a)    
{
        unsigned int answer = 0;
        unsinged int bit;

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

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

        }
        return answer;

}

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.


0
 
LVL 2

Accepted Solution

by:
zulti earned 500 total points
ID: 16290528
if (in == (1 << 31)) return ~0 ;
else return (in << 1) - 1 ;

it should work if in is a power of 2

0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question