int main() { int bit_counts[8]; for (int i = 0; i < 8; i++) { bit_counts[i] = 0; } fstream infile; string filename; cin >> filename;

EBK JAVA PROGRAMMING
9th Edition
ISBN:9781337671385
Author:FARRELL
Publisher:FARRELL
Chapter13: File Input And Output
Section: Chapter Questions
Problem 6PE
icon
Related questions
Question

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

Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Array
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT