Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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


Step by Step Solution

3.35 Rating (164 Votes )

There are 3 Steps involved in it

Step: 1

ANSWER In this exercise we will be reading a binary file one byte at a time and counting how many ti... blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Fundamentals of Corporate Finance

Authors: Stephen A. Ross, Randolph W. Westerfield, Bradford D.Jordan

8th Edition

978-0073530628, 978-0077861629

More Books

Students also viewed these Finance questions