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

code needs to be written in c language. A useful way to examine binary numbers i

ID: 3780221 • Letter: C

Question

code needs to be written in c language.

A useful way to examine binary numbers is to check if particular bits are on or off in a binary number. For instance: The binary number (10011) has on bits in positions: 0, 1, 4. Write a program that reads in from stdin two values: octal value (that is the binary number which is guaranteed to fit in an unsigned integer), an int (which denotes the position we are interested in). The program should print "on" if the bit in that position is on in the provided binary number, otherwise print "off".

Explanation / Answer

#include<iostream>

using namespace std;

#define MAX 1000

int main()
{
char octNum[MAX];
long int a[MAX];
long int i =0;
int index;

cout<<"Enter the octal number"<<endl;
cin>>octNum;

cout<<"Please enter the index for which you want to the bits is on or off"<<endl;
cin>>index;

while(octNum[i])
{
switch(octNum[i])
{
case '0':
           a[i] = 000;
break;
case '1':
           a[i]= 001;
break;
case '2':
           a[i]= 010;
break;
case '3':
            a[i]= 011;
               break;
case '4':
           a[i]= 100;
break;
case '5':
           a[i]= 101;
break;
case '6':
a[i]= 110;
break;
case '7':
           a[i]= 111;
break;
default: cout<<"nInvalid octal digit "<<octalNumber[i];
return 0;
}
i++;
}
cout<<"The status of bit at given index is"<<endl;
   if(a[index]==1)
   {cout<<"On";
   }
   else
   {cout<<"Off";}
return 0;
}