Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ there were only 16 foreground and background colors; each numbered 0 to 15.

ID: 3564744 • Letter: C

Question

C++

there were only 16 foreground and background colors; each numbered 0 to 15. White was represented by 15 and black by 0.

Each pixel on the screen had a single byte that stored the background and foreground color. The upper four bits of the byte were the foreground color and the background color was the lower four bytes. (Remember, a single byte can only hold the numbers 0 to 255 in binary).

""

Write a program that will ask the user for the value that the byte contains then it will tell the user what the foreground and background colors are.

Then, write another program that when given the foreground and background colors it will then display the value that the byte will contain.

Error checking on input for these program is required!!!!!

Explanation / Answer

Write a program that will ask the user for the value that the byte contains then it will tell the user what the foreground and background colors are.

#include <iostream>
using namespace std;
int main()
{
   int m;
   int a[8]={0};
   cin>>m;
   int i=0;
   if(m<0 || m>255)
   {
       cout<<"invalid input"<<endl;
       return 0;
   }
   while(m>0)
   {
       a[i]=m%2;
       m=m/2;
       i++;
   }
   int fore = 8*a[7]+4*a[6]+2*a[5]+a[4];
   int back = 8*a[3]+4*a[2]+2*a[1]+a[0];
   cout<<"foreground color is "<<fore<<endl;
   cout<<"background color is "<<back<<endl;
   return 0;
}

write another program that when given the foreground and background colors it will then display the value that the byte will contain.

#include <iostream>
using namespace std;

int main()
{
   int a[4]={0},b[4]={0};
   int m,n; //m =foreground n=background
   cin>>m>>n;
   if((m<0 || m>15) || (n<0 || n>15))
   {
       cout<<"invalid input"<<endl;
       return 0;
   }
   int i=0;
   while(m>0)
   {
       a[i]=m%2;
       m=m/2;
       i++;
   }
   int j=0;
   while(n>0)
   {
       b[j]=n%2;
       n=n/2;
       j++;
   }
   int color = 128*a[3]+64*a[2]+32*a[1]+16*a[0]+8*b[3]+4*b[2]+2*b[1]+b[0];
   cout<<"color is "<<color<<endl;
   return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote