#45009: c++ 中的函式 bitset 可以轉二進制


caspar9202166422@gmail.com (陳柏宇)

學校 : 不指定學校
編號 : 231799
來源 : [27.242.70.239]
最後登入時間 :
2025-03-25 16:36:10

#include <iostream>
#include <bitset>
using namespace std;

int main()
{
    int n;
    while(cin >> n)
    {
        if(n == 0)
        {
            break;
        }
        bitset<32> b(n);
        string bstr = b.to_string();
        bstr = bstr.substr(bstr.find('1'));

        int parity = b.count();
        cout << "The parity of " << bstr << " is " << parity << " (mod 2)." << endl;
    }
}