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;
}

https://paiza.io/projects/tSc9Z7VAIQyeKOULichxLQ
--- 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;
}

https://paiza.io/projects/QfrdXTLD2Aip4v_5s_RTBg
(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 << ...1 \times (2 \times a) \times (2 \times b) \times (2 \times c) \times ... ?

#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;
}

https://paiza.io/projects/Fm3L4zLBImKC2U4cACLSxw?language=cpp
(3 << 1 << 4 << 1 << 5 << 9) --> 3145728

Process finished with exit code 0

3 \times (2 \times 1) \times (2 \times 4) \times (2 \times 1) \times (2 \times 5) \times (2 \times 9) = 17280 なので仮説崩壊

3 \times (2^1) \times (2^4) \times (2^1) \times (2^5) \times (2^9) = 3145728
こうですね

よく考えたら当たり前なんだよなぁ...(´・ω・`)

雰囲気で書いちゃうけど
a_0 << a_1 << a_2 << a_3 << ... << a_N

\displaystyle a_0 \cdot \prod_{k=1}^{N}{2^{a_k}}

で求めればよさそう
(もっときれいにならん...?

ログインするとコメントできます