zoukankan      html  css  js  c++  java
  • [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;
    }
    

    总结

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

  • 相关阅读:
    VisualSVN 服务不能自动启动的问题
    "There is already an open DataReader associated with this Command which must be closed first"错误
    LINQ 中调用存储过程自动绑定列名
    vs2008中调试iis7.0托管的程序
    SQL SERVER 2005的一个怪问题: 在查询结果面板中编辑失败.
    在VS2003下把一个DataTable Update 到数据库
    帮人解决一个小问题:QQ空间登录时脚本错误造成无法登入
    关于Sys未定义错误
    多层母版页嵌套中, 内层母版页的事件默认不触发
    线程同步
  • 原文地址:https://www.cnblogs.com/by-sknight/p/11013905.html
Copyright © 2011-2022 走看看