如何在数组中存储位串?

bal*_*aji 1 c c++

如何计算8位字符串中出现次数1.例如10110001.位字符串取自用户.像10110001那样应该使用什么类型的数组来存储c中的这个位串?

Pra*_*rav 5

简短而简单.使用std::bitset(C++)

#include <iostream>
#include <bitset>

int main()
{
  std::bitset<8> mybitstring;
  std::cin >> mybitstring;
  std::cout << mybitstring.count(); // returns the number of set bits
}
Run Code Online (Sandbox Code Playgroud)

Ideone的在线测试