// This program finds the octal equivalent of a series of hexadecimal numbers from m to n
// Code: Yuvraj Singla
#include
#include
#include
using namespace std;
int add=0; //This variables serves as a counter to print total additions that took place in the program at the end
unsigned int MSB_SET(unsigned int v) //finding MSB SET of an N-bit integer in O(log(N)) operations with multiply and lookup
{ // Matrix lookup Given by Eric Cole
short int r; // result goes here
static const unsigned short MultiplynLookupBitPosition[32] =
{
0, 9, 1, 10, 13, 21, 2, 29, 11, 14, 16, 18, 22, 25, 3, 30,
8, 12, 20, 28, 15, 17, 24, 7, 19, 27, 23, 6, 26, 5, 4, 31
}; //look-up stable
r = MultiplynLookupBitPosition[(unsigned int)(v * 0x07C4ACDDU) >> 27];
return r+1; //Result is the index of msb w.r.t. 0th index, but our index starts from 1 so add 1
}
unsigned long Hex2Dec(char *input) //Function to convert Hexadecimal code to decimal code
{
unsigned long dec = 0; //This will hold the result value in decimal
while (*input) { //While a cahracter is fetched from the input file
char c = *input++; //copying that character to c, and incrementing the pointer
if ( c >= 'a') //If the Hex code provide is in LOWERCASE, convert to upper
{
c -= 32;
add++;
}
c = ( c >= 'A' && c < 'G' ) ? (add++),c - 'A' + 10 : c - '0'; //converting the hex character to number
add++;
dec = (dec << 4) | c; //appending the number to the last 4 bits of our result
}
return dec;
}
unsigned long Dec2Oct(unsigned long dec) //converting from decimel to octal
{
unsigned int r = 0; //r stores the position of MST that is set w.r.t. LSB
r=MSB_SET(dec);
r-=(r%3); add++; //store the next number to r that is a multiple of 3
unsigned long oct=0;
while(r+3) //as r is multiple of 3, this loop will work upto r=0
{
unsigned int x=7; //3 LSB set
x=(dec>>(r))&x; //Fetching 3 bits starting from the one with the MSB set
oct=((oct<<3) + (oct<<1)); //*10 operation using bitshifting
oct+=x; //Thus adding the next digit to the number
r-=3;
add+=3;
}
return oct;
}
int main()
{
clock_t beg_time; //To hold the beginning clock time
beg_time=clock();
freopen("in_sec5_3.txt","r",stdin); //Change the standard input to file
freopen("out_101083013_sec5_3.txt","w",stdout); //Change the standard output to file
char b[10],c[10]; //Hold the 2 hexadecimal numbers as string
short int diff; //To store the diff betweent he two input numbers
cin.getline(b,100,','); //Get first number limited by a comma as a string
cin.getline(c,1,' '); //Fetch the comma and discard
cin.getline(c,100); //Get the second number as string
beg_time=clock();
unsigned long oct1,oct2; //to store the octal value of the inputs
oct1=Hex2Dec(b); //Convert to decimel
oct2=Hex2Dec(c);
diff=oct2-oct1; //Find difference between the 2 decimel values
oct1=Dec2Oct(oct1); //Convert 1st number to octal
while(diff) //While diff is not 0
{
cout<7) //if pct%(div*10)/div is >7, the number is invalid
{
oct1+=(div<<1); //Make the number valid
add+=1;
}
div=((div<<3) + (div<<1)); ; //multiply div with 10;
add+=1;
}
diff--; //Decrement diff
}
cout<