- #include <cerrno> 
- #include <cstdlib> 
- #include <iostream> 
- #include <cstring> 
- #include <sstream> 
- #include <cmath> 
-   
- #define BINARY_STRING_LEN 0x435 
-   
- typedef long double float124_t; 
-   
- bool isValidBinary(const char* numStr) 
- { 
-     const char *validBinary = "01"; 
-     return (!numStr[strspn(numStr,validBinary)]) ? true : false; 
- } 
-   
- char *cstrrev(char *str) 
- { 
-       char *p1, *p2; 
-   
-       if (! str || ! *str) 
-             return str; 
-       for (p1 = str, p2 = str + strlen(str) - 1; p2 > p1; ++p1, --p2) 
-       {*p1 ^= *p2; *p2 ^= *p1; *p1 ^= *p2;} 
-   
- 	  return str; 
- } 
-   
- void showBitDiff(const char *binaryStr1,const char *binaryStr2) 
- { 
-   
-     size_t index = 0,matchedBits = 0,notMatchedBits; 
-     bool isValidLen = (strlen(binaryStr1) == strlen(binaryStr2)) ? true : false; 
-   
-     if(isValidLen) 
-     { 
-         size_t len =  strlen(binaryStr1); 
-   
-         printf("showBitDiff info Legend : # Matched, ~ Not Matched\n"); 
-         printf("Following bits at locations  matched/not-matched : \n"); 
-   
-         if(isValidBinary(binaryStr1) && isValidBinary(binaryStr2)) 
-         { 
-   
-             for(index = 0; index < len; index++) 
-             { 
-   
-                 if(binaryStr1[index] == binaryStr2[index]) 
-                 { 
-                     matchedBits++; 
-                     printf("# %d\t",index + 1); 
-                 } 
-   
-                 else 
-                     printf("~ %d\t",index + 1); 
-             } 
-   
-         } 
-   
-         else 
-         { 
-             fprintf(stderr,"Invalid-binary digit encountered\n"); 
-             exit(EXIT_FAILURE); 
-         } 
-   
-         notMatchedBits = len - matchedBits; 
-   
-         if(notMatchedBits == 0) 
-             printf("Both binary-strings are equal!\n"); 
-   
-         else 
-             printf("\n\nshowBitDiff statistics : \nTotal Bits %d Bits\n%d Bits matched\n%d Bits not matched\n",len,matchedBits,notMatchedBits); 
-     } 
-   
- 	else{ 
- 	printf("binaryStr1 len = %d and binaryStr2 len = %d\n",strlen(binaryStr1),strlen(binaryStr2)); 
- 	fputs("binary-strings must have same length\n",stderr); 
- 	} 
- } 
-   
- static const char* double2Binary(float124_t db_num) 
- { 
-   
-     int i,bitCount = 0; 
-     static char binString[BINARY_STRING_LEN] = {'\0'}; 
-   
-     do 
-     { 
-         binString[bitCount++] = '0' + (int)fmodl(db_num,2.0); 
-         db_num = floorl(db_num / 2); 
-   
-     } 
-     while (db_num > 0); 
-   
-     std::cout<<"Bit-count = "<<bitCount<<std::endl; 
-   
-     binString[bitCount] = '\0'; 
-     return cstrrev(binString); 
- } 
-   
-   
- int main() 
- { 
-     const char *numStr = "998446744073709551615"; 
-     const char *binStr = "1101100010000000111011011001111011110011001010110011111111111111111111"; 
-   
-     char *e; 
-     errno = 0; 
-     float124_t val = strtold(numStr,&e); 
-   
-     if (*e != '\0' || errno != 0) 
-     { 
-         std::cout<<"Invalid character --> "<<*e<<std::endl; 
-         exit(EXIT_FAILURE); 
-     } 
-   
- 	/*Converting String to Long Double in C-Style*/ 
-     const char *cBinary = double2Binary(val); 
-   
-     std::cout << "Successfully parsed strtold (C-Style): " <<val <<std::endl; 
-     std::cout<< "Binary : "<<cBinary<<std::endl; 
-  	showBitDiff(cBinary,binStr); 
-   
- 	/*Converting String to Long Double in C++-Style*/ 
-   
- 	std::string cppString(numStr); 
-     std::stringstream ss(cppString); 
-     float124_t dblNum; 
-     ss >> dblNum; 
-   
-     const char *cppBinary = double2Binary(dblNum); 
-     std::cout << "stringstream parsed stringstream (C++ Style): " <<dblNum <<std::endl; 
-     std::cout<< "Binary : "<<cppBinary<<"\n\n"; 
-     showBitDiff(cppBinary,binStr); 
-   
-     return 0; 
- }