c*******9 发帖数: 6411 | 1 Write a function int BitSwapReqd(int A, int B) to determine the number of
bits required to convert integer A to integer B.
I have trouble understanding the question, what does it mean to convert
integer A to integer B?
Thanks a lot |
r****t 发帖数: 10904 | 2 function name is the first thing you need to read. |
g**e 发帖数: 6127 | 3 xor一下然后数一下有几个1
【在 c*******9 的大作中提到】 : Write a function int BitSwapReqd(int A, int B) to determine the number of : bits required to convert integer A to integer B. : I have trouble understanding the question, what does it mean to convert : integer A to integer B? : Thanks a lot
|
e****d 发帖数: 895 | 4 An example to use the compiler to do the calculation.
template struct BIT
{
const static int value = ((M >> N) & 0x01) + BIT::value;
};
template struct BIT
{
const static int value = (M & 0x01);
};
template struct CONVERT
{
const static int value = BIT<(A ^ B), (sizeof(A) - 1)>::value;
};
【在 g**e 的大作中提到】 : xor一下然后数一下有几个1
|
b********h 发帖数: 119 | 5 这个是不是所谓的template meta-programming?
【在 e****d 的大作中提到】 : An example to use the compiler to do the calculation. : template struct BIT : { : const static int value = ((M >> N) & 0x01) + BIT::value; : }; : template struct BIT : { : const static int value = (M & 0x01); : }; : template struct CONVERT
|
b*****e 发帖数: 474 | 6 Great! Learned something today!
But:int struct CONVERT, should use sizeof(A)*8 instead of sizeof(A), haha
Traditional way:
class BitCounter {
public:
static int convert(int i, int j) { return count(i^j); }
static int count(int k) {
int n = 0;
for (; k != 0; k &= (k-1) ) n++;
return n;
}
};
【在 e****d 的大作中提到】 : An example to use the compiler to do the calculation. : template struct BIT : { : const static int value = ((M >> N) & 0x01) + BIT::value; : }; : template struct BIT : { : const static int value = (M & 0x01); : }; : template struct CONVERT
|
e****d 发帖数: 895 | 7 Good catch.
【在 b*****e 的大作中提到】 : Great! Learned something today! : But:int struct CONVERT, should use sizeof(A)*8 instead of sizeof(A), haha : Traditional way: : class BitCounter { : public: : static int convert(int i, int j) { return count(i^j); } : static int count(int k) { : int n = 0; : for (; k != 0; k &= (k-1) ) n++; : return n;
|
g*********s 发帖数: 1782 | 8 a common mistake to me too...
【在 e****d 的大作中提到】 : Good catch.
|