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 ;
}
本文介绍了一种方法,用于获取一个整数中从右数第 i 位的值(i 从 0 开始)。通过使用位运算符,我们可以轻松地检查该位是否为 1 或 0。
6877

被折叠的 条评论
为什么被折叠?



