8.17.3: Count how often each bit is set in all the bytes of the given binary file....
Question:
8.17.3: 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
#include
#include
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;
}
Fundamentals of Corporate Finance
ISBN: 978-0077861629
8th Edition
Authors: Stephen A. Ross, Randolph W. Westerfield, Bradford D.Jordan