int main() { int bit_counts[8]; for (int i = 0; i < 8; i++) { bit_counts[i] = 0; } fstream infile; string filename; cin >> filename;
Count how often each bit is set in all the bytes of the given binary file.
Open the file with the given name as a binary file. Count how often each bit is set in all the bytes of the file. A byte value returned by infile.get outside the range from 0 to 255 indicates the end of the file. Once you have a byte, you can get the bits like this:
for (int i = 0; i < 8; i++) { if (byte % 2 == 1) { // The i-th bit is set } byte = byte / 2; }
CODE SHOWN BELOW:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
int bit_counts[8];
for (int i = 0; i < 8; i++) { bit_counts[i] = 0; }
fstream infile;
string filename;
cin >> filename;
/* Your code goes here */
int largest = 0;
for (int i = 0; i < 8; i++)
{
cout << i << ": " << bit_counts[i] << endl;
}
return 0;
}
Step by step
Solved in 2 steps