// lab3compress.cpp
// Lab 3: Program written by Lucia Moura

#include <iostream>
using namespace std;

// This is an auxiliary procedure that allows you to see the
// bits of a byte stored as an unsigned char 

void printByte(unsigned char c) {
   cout << "Byte decimal value:"<<(int)c<<"\n";

   cout <<"Byte binary representation:";

   unsigned char mask=128; // mask has byte 10000000
   for (int i=7; i>=0; i--) {
      unsigned char bit=(c&mask)>>i; // what is this doing? ask your Ta in the lab.
      cout<<(int)bit; 
      mask=mask>>1; //the bit set in mask moves right
      } 
   cout<<"\n\n";
}

// Main program: first shows some "playing with bits", then
// provides the template for getting a sequence of 4 characters entered 
// by the user and showing its coverted/encoded byte.
// The code to do the encoding via bit manipulation needs to be completed by you

int main() { 

  char ans;
  char letters[4]; // array to store 4 letters
  unsigned char mybyte; // a 1-byte integer can be stored as an unsigned char

  // playing with bits: 

  mybyte=255; // all bits are set to 1

  cout<<"\nDisplay original contents of mybyte:\n";
  printByte(mybyte);

  cout<<"After `mybyte= mybyte << 3':\n";
  mybyte = mybyte << 3; // this does 3 shifts left, with insertion of 3 0's 
  printByte(mybyte);

  cout<<"Display `mybyte|12'\n";   printByte(mybyte|12);
  cout<<"Display `mybyte&12'\n";   printByte(mybyte&12);
  cout<<"Display `mybyte^12'\n";   printByte(mybyte^12);
  cout<<"Display `mybyte>>2'\n";  printByte(mybyte>>2);

  while (1) // infinite loop, until a 'break' is executed
  {
   cout<<"\nEnter 4 characters and press enter: ";
   for (int j=0;j<4;j++) cin>>letters[j];
   

   unsigned char encode=0;

   /****** complete code to encode this info into "encoded" *****/

   /***********************/

   cout<<"Letters are: "
       <<letters[0]<<letters[1]<<letters[2]<<letters[3]<<"\n";
   cout<<"The encoded byte is:\n"; printByte(encode);

   cout<<"Continue? (Y/N)> "; cin>>ans;
   if (ans=='N'||ans=='n') break; // if user doesn't want to continue, break loop.
  }
  return 0; 
}


        


