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

    总结

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

  • 相关阅读:
    16-高级指针
    15-C语言结构体
    14-C语言宏
    13-C语言字符串函数库
    12-C语言字符串
    11-C语言指针
    10-C语言函数
    POJ 1001 高精度乘法
    POJ 1060 多项式乘法和除法取余
    POJ 1318 字典排序
  • 原文地址:https://www.cnblogs.com/by-sknight/p/11013905.html
Copyright © 2011-2022 走看看