现在的位置: 首页 > 综合 > 正文

简单算法–求一个数n的二进制形式的第i位

2014年09月05日 ⁄ 综合 ⁄ 共 222字 ⁄ 字号 评论关闭

      Get bit i for a given number n. (i count from 0 and starts from right)

      i是从右边数起的,并从0开始计数。

int getBit(int num, int i){

    int result = num & (1<<i) ;

    if( result ) {

        return 1; 

    }

    else {

        return 0 ;

    }

    // the if-else statement is equal to the statement return ( bool )result ;

}




抱歉!评论已关闭.