Reversing Bytes
I came across a problem recently whilst programming sprites on a C64 emulator. Sprites are made up of
patterns of bits, grouped into bytes (one byte = 8 bits). I calculated the byte codes needed to switch on
the bits manually, so it is quite a lot of work to recalculate them if you want the sprite to face the other way
(e.g. to the right instead of to the left). There may be a machine code routine somewhere that does this
on the C64 (I don't know of one) and there may be some simple trick that I missed (in which case please
contact us at botrejectsinc@cronodon.com if you know of it). The solution I came up with was interesting in
its own right, as it introduced me to the bitset template in C++. I include the code here in case it is of
interest to someone else.

    //BitInverter

    #include <iostream>
    #include <stdlib.h>
    #include <bitset>
    #include <fstream>
    #include <vector>
    #include <iomanip>

    using namespace std;

    bool processFile(vector<int>* data);
    bitset<8> invertByte(bitset<8> input);

    int main()
    {
       typedef bitset<8> byte;
       cout << "Bit Inverter!" << endl;

       //Ensure data vector is large enough for the input file
       vector<int>* data = new vector<int>(1000,-1000);
       processFile(data);

       cout << "Press return to continue" << endl;
       string userInput = "";
       getline(cin, userInput);

       return 0;
    }

    bool processFile(vector<int>* data)
    {
       typedef bitset<8> byte;
       byte input = 0;
       byte output = 0;

       fstream inFile;
       ofstream outFile;
       char myByte[5];
       int i=0;

       inFile.open("bytes.txt",ios::in);
       outFile.open("invertedBytes.txt",ios::out);

       if(inFile.is_open() && outFile.is_open())
       {
           while(inFile.peek() != EOF)
           {
               inFile.getline(myByte, 256, ',');
               data->at(i) = atoi(myByte);
               cout << setw(17) << data->at(i) << " = ";
               input = atoi(myByte);
               output = invertByte(input);
               int invertedDecByte = (int)output.to_ulong();
               cout << setw(17) << input << " -> " << invertByte(input);
               cout <<setw(10) << " = " << invertedDecByte << endl;
               outFile << invertedDecByte;
               if((i+1) % 8 == 0)
               {
                   outFile << endl;
               }
               else
               {
                   outFile << ",";
               }
               i++;
           }
           inFile.close();
           outFile.close();
       }
       else
       {
           cout << "Error opening file!" << endl;
           return false;
       }

       return true;
    }

    bitset<8> invertByte(bitset<8> input)
    {
       typedef bitset<8> byte;
       byte result = 0;
       byte temp;
       byte RHS;
       byte unity = 1;
       byte zero = 0;

       for(int i = 0; i <= 7; i++)
       {
          temp = input & (unity << i);
          if(temp != 0)
          {
              RHS.set(7-i);
              //cout << RHS << endl;
              result = result | RHS;
              temp = 0;
          }
       }
       return result;
    }

This short program simply takes a set of decimal bytes, ranging in value from 0 to 255 (though it will work
for negative values) such as the data elements in a C64 program (e.g. DATA 0,254,36,7,8, ...) converts
them into binary format (stored as a bitset template 8 bits long) inverts the order of the bits (e.g. 01100100
become 00100110) and then converts the result back to decimal. The inputs are fed-in as a text file
("bytes.txt") consisting of comma-separated values and outputs the results into another text file
("invertedBytes.txt").

For example, using data from a crawling slime sprite, the input file "bytes.txt" looks like this:
0,0,0,0,0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0,0,0,0,0
1,188,0,7,255,0,21,103,0
21,87,128
21,171,192,7,170,224,1,255,248
0,57,255,0,0,0
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
0,31,128,0,123,208,0,213,96
0,213,96,0,213,192,0,106,192
0,61,192,0,61,112,0,15,240
0,15,252,0,0,0
The output file ("invertedBytes.txt") contains the following:
0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0
0,0,61,0,224,255,0,168
230,0,234,1,213,3,224,85
7,128,255,31,156,255,0,0
0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0
0,0,0,0,0,248,1,0
222,11,0,171,6,171,6,0
171,3,0,86,3,188,3,0
188,14,0,240,15,240,63,0
0,0,
A screenshot of the console output looks like this:
byte inversion part 1
byte inversion part b