私はいくつかのビット操作に取り組んでおり、マクロとインライン関数を入れた同じコード行の奇妙な異なる出力に遭遇しました。この関数は、 - 番目の位置Lからアクティブなビットを持つ 64 ビット マスクを返します: 。出力が異なる理由を教えてください。N(~0<<N) - (~0<<(N+L))
#include <iostream>
#include <bitset>
using namespace std;
#define ONES (~0UL)
#define MASK(from_bit, nbits) \
  (ONES << (from_bit)) - (ONES << ((from_bit) + (nbits)))
inline unsigned long int mask(size_t from_bit, size_t nbits) {
  return (ONES << from_bit) - (ONES << (from_bit + nbits));
}
int main(int argc, char **argv) {
  cout << "using #define:         " << bitset<64>(MASK(63, 3)) << endl;
  cout << "using inline function: " << bitset<64>(mask(63, 3)) << endl;
  return 0;
}
出力:
$ g++ -o test main.cc
main.cc: In function 'int main(int, char**)':
main.cc:15: warning: left shift count >= width of type
$ ./test
using #define:         1000000000000000000000000000000000000000000000000000000000000000
using inline function: 1000000000000000000000000000000000000000000000000000000000000100
                                                                              ------^
-O3オプションでコンパイル:
$ g++ -O3 -o test2 main2.cc
main.cc: In function 'int main(int, char**)':
main.cc:15: warning: left shift count >= width of type
$ ./test2
using #define:         1000000000000000000000000000000000000000000000000000000000000000
using inline function: 0000000000000000000000000000000000000000000000000000000000000000
                 ------^
コンパイラ情報:
$ g++ --version
i686-apple-darwin11-llvm-g++-4.2 (GCC) 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.9.00)