這應該是一個非常基本的東西,但不知何故,我沒有看到問題。由部門(國防部)產生的行爲奇怪
#include <iostream>
template <int dim>
inline
void
i2c(const int & ind, int & i, int &j) {
i = (int) ind/dim;
j = ind & dim;
//j = ind - i*dim;
}
static const int dim = 2;
int main() {
int i,j;
for (unsigned int c = 0; c < dim*dim; c++) {
i2c<dim>(c,i,j);
std::cout<<c<<"/"<<dim<<"="<<i<<"; "<<c<<"&"<<dim<<"="<<j<<std::endl;
}
return 0;
}
這是code。
輸出是:
0/2=0; 0&2=0
1/2=0; 1&2=0
2/2=1; 2&2=2
3/2=1; 3&2=2
如果我使用j = ind - i*dim
- 一切正常。
EDIT1: 有人可以刪除這個問題,這樣我就不會感到羞愧了嗎? ;)
你有什麼期望的輸出? – juanchopanza
你爲什麼要按位AND('&')?問題的名稱提到模數,它是'%' – olevegard
@olevegard狗屎,這是真的!過熱.... :) – Denis