Question: C++ I need to reverse a given output in bits I need to add a loop after // reverse the bits Please help #include #include

 C++ I need to reverse a given output in bits I C++ I need to reverse a given output in bits I need to add a loop after "// reverse the bits" Please help

#include #include /* e.g. a character encoded like this: 3f 66 66 66 3e 06 06 will be converted to: fc 66 66 66 67 60 60 3f = 00111111, fc = 11111100 i.e. move bit to the other end Could use a loop that iterates 8 times: (use index as power of 2) if lsb (least significant bit) (bit 0) is 1, then that means add 247 =128 to result if bit is position 2 (from the right) (bit 1) -> add 2*6 =64 etc. One way to "move" through bits is to shift the byte in each iteration ch >> 1 (moves one bit to right) the lsb is 1 if (number is odd) */ int main() { // open files in binary mode for input/output FILE* fin; FILE* fout; char ch; //(in C and asm it is actually an 8-bit number) fin fopen("Plm", "rb"); // open in mode read/binary // check if files were opened without error if(fin == NULL) {printf("Open failed "); exit(1);} //exit with error code 1 fout = fopen("P2m", "wb"); = = // open in mode write/binary // loop to read each character ch fgetc(fin); // priming read while ch != EOF) // EF is special value that indicates end of file // In this system EF is Oxff which is 11111111 as signed number is -1 { printf("%.2x\t",ch); // reverse the bits //write char to output file fputc(ch, fout); ch = fgetc(fin); } fputc(0x00, fout); // close files fclose(fin); fclose(fout); return 0; #include #include /* e.g. a character encoded like this: 3f 66 66 66 3e 06 06 will be converted to: fc 66 66 66 67 60 60 3f = 00111111, fc = 11111100 i.e. move bit to the other end Could use a loop that iterates 8 times: (use index as power of 2) if lsb (least significant bit) (bit 0) is 1, then that means add 247 =128 to result if bit is position 2 (from the right) (bit 1) -> add 2*6 =64 etc. One way to "move" through bits is to shift the byte in each iteration ch >> 1 (moves one bit to right) the lsb is 1 if (number is odd) */ int main() { // open files in binary mode for input/output FILE* fin; FILE* fout; char ch; //(in C and asm it is actually an 8-bit number) fin fopen("Plm", "rb"); // open in mode read/binary // check if files were opened without error if(fin == NULL) {printf("Open failed "); exit(1);} //exit with error code 1 fout = fopen("P2m", "wb"); = = // open in mode write/binary // loop to read each character ch fgetc(fin); // priming read while ch != EOF) // EF is special value that indicates end of file // In this system EF is Oxff which is 11111111 as signed number is -1 { printf("%.2x\t",ch); // reverse the bits //write char to output file fputc(ch, fout); ch = fgetc(fin); } fputc(0x00, fout); // close files fclose(fin); fclose(fout); return 0

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!