Open4
シフト演算子のつかいかたetc
書いて覚える
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define plvar(var) cout << #var << " : " << (var) << endl;
int main() {
ll n = 65535;
rep(a, 4) {
cout << "--- a: " << a << " ---" << endl;
plvar((n << a));
plvar((n << a << a));
plvar((n << (a << a)));
plvar(((n << a) << a));
}
return 0;
}
--- a: 0 ---
(n << a) : 65535
(n << a << a) : 65535
(n << (a << a)) : 65535
((n << a) << a) : 65535
--- a: 1 ---
(n << a) : 131070
(n << a << a) : 262140
(n << (a << a)) : 262140
((n << a) << a) : 262140
--- a: 2 ---
(n << a) : 262140
(n << a << a) : 1048560
(n << (a << a)) : 16776960
((n << a) << a) : 1048560
--- a: 3 ---
(n << a) : 524280
(n << a << a) : 4194240
(n << (a << a)) : 1099494850560
((n << a) << a) : 4194240
Process finished with exit code 0
左結合みたい
#include <bits/stdc++.h>
using namespace std;
#define println(var) cout << #var << " --> " << (var) << endl
int main() {
println((1 << 2));
println((1 << 2 << 3));
println((1 << 2 << 3 << 4));
println((1 << 2 << 3 << 4 << 5));
return 0;
}
(1 << 2) --> 4
(1 << 2 << 3) --> 32
(1 << 2 << 3 << 4) --> 512
(1 << 2 << 3 << 4 << 5) --> 16384
Process finished with exit code 0
1 << a << b << c << ...
は
#include <bits/stdc++.h>
using namespace std;
#define println(var) cout << #var << " --> " << (var) << endl
int main() {
println((3 << 1 << 4 << 1 << 5 << 9));
return 0;
}
(3 << 1 << 4 << 1 << 5 << 9) --> 3145728
Process finished with exit code 0
こうですね
よく考えたら当たり前なんだよなぁ...(´・ω・`)
雰囲気で書いちゃうけど
a_0 << a_1 << a_2 << a_3 << ... << a_N
は
で求めればよさそう
(もっときれいにならん...?