Closed. This question is off-topic。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗? Update the question,因此它是on-topic,用于堆栈溢出。
                        
                        5年前关闭。
                                                                                            
                
        
首先,对不起的标题感到抱歉。这是我的问题:

n > 0为自然数。确定正数k,以使2^i + k = n表示最大可能的i

我如何用C优雅地做到这一点?

最佳答案

int i = 0; j = 1, k, n = (your value);

if ( n > 0 )
{
    while ( 2 * j < n)
    {
        j = 2 * j;
        i++;
    }

    k = n - j;
}

// replace '<' with '<=' if k = 0 is desired where n is a pure power of 2


(nb:标准C中没有幂运算符)

关于c - 获得2的幂的相关结果的优雅方法,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27749998/

10-11 20:58