fork download
  1. #include <cerrno>
  2. #include <cstdlib>
  3. #include <iostream>
  4. #include <cstring>
  5. #include <sstream>
  6. #include <cmath>
  7.  
  8. #define BINARY_STRING_LEN 0x435
  9.  
  10. typedef long double float124_t;
  11.  
  12. bool isValidBinary(const char* numStr)
  13. {
  14. const char *validBinary = "01";
  15. return (!numStr[strspn(numStr,validBinary)]) ? true : false;
  16. }
  17.  
  18. char *cstrrev(char *str)
  19. {
  20. char *p1, *p2;
  21.  
  22. if (! str || ! *str)
  23. return str;
  24. for (p1 = str, p2 = str + strlen(str) - 1; p2 > p1; ++p1, --p2)
  25. {*p1 ^= *p2; *p2 ^= *p1; *p1 ^= *p2;}
  26.  
  27. return str;
  28. }
  29.  
  30. void showBitDiff(const char *binaryStr1,const char *binaryStr2)
  31. {
  32.  
  33. size_t index = 0,matchedBits = 0,notMatchedBits;
  34. bool isValidLen = (strlen(binaryStr1) == strlen(binaryStr2)) ? true : false;
  35.  
  36. if(isValidLen)
  37. {
  38. size_t len = strlen(binaryStr1);
  39.  
  40. printf("showBitDiff info Legend : # Matched, ~ Not Matched\n");
  41. printf("Following bits at locations matched/not-matched : \n");
  42.  
  43. if(isValidBinary(binaryStr1) && isValidBinary(binaryStr2))
  44. {
  45.  
  46. for(index = 0; index < len; index++)
  47. {
  48.  
  49. if(binaryStr1[index] == binaryStr2[index])
  50. {
  51. matchedBits++;
  52. printf("# %d\t",index + 1);
  53. }
  54.  
  55. else
  56. printf("~ %d\t",index + 1);
  57. }
  58.  
  59. }
  60.  
  61. else
  62. {
  63. fprintf(stderr,"Invalid-binary digit encountered\n");
  64. exit(EXIT_FAILURE);
  65. }
  66.  
  67. notMatchedBits = len - matchedBits;
  68.  
  69. if(notMatchedBits == 0)
  70. printf("Both binary-strings are equal!\n");
  71.  
  72. else
  73. printf("\n\nshowBitDiff statistics : \nTotal Bits %d Bits\n%d Bits matched\n%d Bits not matched\n",len,matchedBits,notMatchedBits);
  74. }
  75.  
  76. else{
  77. printf("binaryStr1 len = %d and binaryStr2 len = %d\n",strlen(binaryStr1),strlen(binaryStr2));
  78. fputs("binary-strings must have same length\n",stderr);
  79. }
  80. }
  81.  
  82. static const char* double2Binary(float124_t db_num)
  83. {
  84.  
  85. int i,bitCount = 0;
  86. static char binString[BINARY_STRING_LEN] = {'\0'};
  87.  
  88. do
  89. {
  90. binString[bitCount++] = '0' + (int)fmodl(db_num,2.0);
  91. db_num = floorl(db_num / 2);
  92.  
  93. }
  94. while (db_num > 0);
  95.  
  96. std::cout<<"Bit-count = "<<bitCount<<std::endl;
  97.  
  98. binString[bitCount] = '\0';
  99. return cstrrev(binString);
  100. }
  101.  
  102.  
  103. int main()
  104. {
  105. const char *numStr = "998446744073709551615";
  106. const char *binStr = "1101100010000000111011011001111011110011001010110011111111111111111111";
  107.  
  108. char *e;
  109. errno = 0;
  110. float124_t val = strtold(numStr,&e);
  111.  
  112. if (*e != '\0' || errno != 0)
  113. {
  114. std::cout<<"Invalid character --> "<<*e<<std::endl;
  115. exit(EXIT_FAILURE);
  116. }
  117.  
  118. /*Converting String to Long Double in C-Style*/
  119. const char *cBinary = double2Binary(val);
  120.  
  121. std::cout << "Successfully parsed strtold (C-Style): " <<val <<std::endl;
  122. std::cout<< "Binary : "<<cBinary<<std::endl;
  123. showBitDiff(cBinary,binStr);
  124.  
  125. /*Converting String to Long Double in C++-Style*/
  126.  
  127. std::string cppString(numStr);
  128. std::stringstream ss(cppString);
  129. float124_t dblNum;
  130. ss >> dblNum;
  131.  
  132. const char *cppBinary = double2Binary(dblNum);
  133. std::cout << "stringstream parsed stringstream (C++ Style): " <<dblNum <<std::endl;
  134. std::cout<< "Binary : "<<cppBinary<<"\n\n";
  135. showBitDiff(cppBinary,binStr);
  136.  
  137. return 0;
  138. }
Success #stdin #stdout 0s 4524KB
stdin
Standard input is empty
stdout
Bit-count = 70
Successfully parsed strtold (C-Style): 9.98447e+20
Binary : 1101100010000000111011011001111011110011001010110100000000000000000000
showBitDiff info Legend : # Matched, ~ Not Matched
Following bits at locations  matched/not-matched : 
# 1	# 2	# 3	# 4	# 5	# 6	# 7	# 8	# 9	# 10	# 11	# 12	# 13	# 14	# 15	# 16	# 17	# 18	# 19	# 20	# 21	# 22	# 23	# 24	# 25	# 26	# 27	# 28	# 29	# 30	# 31	# 32	# 33	# 34	# 35	# 36	# 37	# 38	# 39	# 40	# 41	# 42	# 43	# 44	# 45	# 46	# 47	# 48	# 49	~ 50	~ 51	~ 52	~ 53	~ 54	~ 55	~ 56	~ 57	~ 58	~ 59	~ 60	~ 61	~ 62	~ 63	~ 64	~ 65	~ 66	~ 67	~ 68	~ 69	~ 70	

showBitDiff statistics : 
Total Bits 70 Bits
49 Bits matched
21 Bits not matched
Bit-count = 70
stringstream parsed stringstream (C++ Style): 9.98447e+20
Binary : 1101100010000000111011011001111011110011001010110100000000000000000000

showBitDiff info Legend : # Matched, ~ Not Matched
Following bits at locations  matched/not-matched : 
# 1	# 2	# 3	# 4	# 5	# 6	# 7	# 8	# 9	# 10	# 11	# 12	# 13	# 14	# 15	# 16	# 17	# 18	# 19	# 20	# 21	# 22	# 23	# 24	# 25	# 26	# 27	# 28	# 29	# 30	# 31	# 32	# 33	# 34	# 35	# 36	# 37	# 38	# 39	# 40	# 41	# 42	# 43	# 44	# 45	# 46	# 47	# 48	# 49	~ 50	~ 51	~ 52	~ 53	~ 54	~ 55	~ 56	~ 57	~ 58	~ 59	~ 60	~ 61	~ 62	~ 63	~ 64	~ 65	~ 66	~ 67	~ 68	~ 69	~ 70	

showBitDiff statistics : 
Total Bits 70 Bits
49 Bits matched
21 Bits not matched