#include <iostream>
using namespace std;
long long swap_bits(long long x, int i, int j) {
if (((x >> i) & 1) != ((x >> j) & 1)) {
x ^= (1LL << i) | (1LL << j);
}
return x;
}
int main(int argc, char *argv[]) {
srand(time(nullptr));
long long n;
int i, j;
if (argc == 4) {
n = atol(argv[1]);
i = atol(argv[2]);
j = atol(argv[3]);
} else {
n = rand();
i = rand() % 64;
j = rand() % 64;
}
cout << "n = " << n << ", i = " << i << ", j = " << j << endl;
cout << swap_bits(n, i, j) << endl;
return 0;
}
EPI 5.2 SWAP BITS
最新推荐文章于 2023-12-19 16:08:48 发布