• [Aizu] ITP2_10_A~B: Bit Operation


    前言

    ITP系列之位运算, 具体内容参见bitset

    题目链接

    ITP2_10_A: Bit Operation I
    ITP2_10_B: Bit Operation II

    求解

    第一题

    第一次代码

    // file name: 逻辑操作1: 32bit值, 按位取反, 左移, 右移
    // Written by: by_sknight
    // Date: 2019/6/13
     
    #include <bits/stdc++.h>
    using namespace std;
     
    string bits_of(unsigned int x) {
        string str;
        char ch;
        for (int i = 0; i < 32; i++) {
            ch = (x / (int)pow(2, 31 - i)) % 2 + '0';
            str += ch;
        }
        return str;
    }
     
    int main(void) {
        ios::sync_with_stdio(false);
        cin.tie(0);
     
        unsigned int x; cin >> x;
        cout << bits_of(x) << endl;
        cout << bits_of(pow(2, 32) - 1 - x) << endl;
        cout << bits_of(x * 2) << endl;
        cout << bits_of(x / 2) << endl;
    }
    

    第二次代码

    // file name: 逻辑操作1: 32bit值, 按位取反, 左移, 右移
    // Written by: by_sknight
    // Date: 2019/6/13
     
    #include <bits/stdc++.h>
    using namespace std;
     
    int main(void) {
        ios::sync_with_stdio(false);
        cin.tie(0);
     
        unsigned int x; cin >> x;
        bitset<32> b1(x), b2, b3, b4;
        cout << b1 << endl;
        b2 = b1;
        b2.flip();
        cout << b2 << endl;
        b3 = b1 << 1;
        cout << b3 << endl;
        b4 = b1 >> 1;
        cout << b4 << endl;
    }
    

    别人的代码

    3160717 Solution for ITP2_10_A by c7c7

    #include <bits/stdc++.h>
    #define int long long
    using namespace std;
    main(){
      int x;
      cin>>x;
      bitset<32>b(x);
      cout<<b<<endl;
      cout<<(~b)<<endl;
      cout<<(b<<1)<<endl;
      cout<<(b>>1)<<endl;
    }
    

    第二题

    第一次代码

    // file name: 逻辑操作1: 32bit值, 按位取反, 左移, 右移
    // Written by: by_sknight
    // Date: 2019/6/13
     
    #include <bits/stdc++.h>
    using namespace std;
     
    string bits_of(unsigned int x) {
        string str;
        char ch;
        for (int i = 0; i < 32; i++) {
            ch = (x / (int)pow(2, 31 - i)) % 2 + '0';
            str += ch;
        }
        return str;
    }
     
    int main(void) {
        ios::sync_with_stdio(false);
        cin.tie(0);
     
        unsigned int a, b; cin >> a >> b;
        cout << bits_of(a & b) << endl;
        cout << bits_of(a | b) << endl;
        cout << bits_of(a ^ b) << endl;
    }
    

    提交的时候忘记了修改最前面的描述了

    第二次代码

    // file name: 逻辑操作2: 两个数按位与,或,非
    // Written by: by_sknight
    // Date: 2019/6/13
     
    #include <bits/stdc++.h>
    using namespace std;
     
    int main(void) {
        ios::sync_with_stdio(false);
        cin.tie(0);
     
        unsigned int a, b; cin >> a >> b;
        bitset<32> bs_a(a), bs_b(b), bs;
        bs = bs_a & bs_b;
        cout << bs << endl;
        bs = bs_a | bs_b;
        cout << bs << endl;
        bs = bs_a ^ bs_b;
        cout << bs << endl;
    }
    

    别人的代码

    #include <bits/stdc++.h>
    #define int long long
    using namespace std;
    main(){
      int x,y;
      cin>>x>>y;
      bitset<32>a(y),b(x);
      //cout<<b<<endl;
      //cout<<(~b)<<endl;
      //cout<<(b<<1)<<endl;
      //cout<<(b>>1)<<endl;
      cout<<(a&b)<<endl;
      cout<<(a|b)<<endl;
      cout<<(a^b)<<endl;
    }
    

    总结

    基础的位运算符掌握的不够到位, 代码不够整洁

  • 相关阅读:
    ubuntu的php7与apache2的环境搭建
    git服务器搭建post-receive 钩子部署服务端代码
    node redis安装与使用
    微信小程序坑集
    微信小程序登录
    mobx react
    react-native-router-flux 下部导航
    java
    java
    java
  • 原文地址:https://www.cnblogs.com/by-sknight/p/11013905.html
Copyright © 2020-2023  润新知