fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <stdbool.h>
  5. #include <math.h>
  6.  
  7. #define CHECK_BIT(var,pos) ((var) & (1<<(pos)))
  8.  
  9. typedef unsigned int NType; // you may change int to char, short or long
  10.  
  11. // ------------------------------------------------------------------------------------
  12. // Return the maximum representable value by the data type NType
  13. //
  14. NType bits_maxval( void )
  15. {
  16. NType ret=1;
  17. register int ndigs = sizeof(NType) * 8;
  18. while ( ndigs ) {
  19. ret += abs( pow(2, ndigs--) );
  20. }
  21.  
  22. return ret;
  23. }
  24. // ------------------------------------------------------------------------------------
  25. // Return the size of n in bits
  26. //
  27. unsigned int bits_count( NType n )
  28. {
  29. return (unsigned int) (8 * sizeof( n ) );
  30. }
  31. // ---------------------------------------------------------------------------------
  32. // Return the minimum number of bits needed to represent n in binary form
  33. //
  34. unsigned int bits_mincount( NType n )
  35. {
  36. register int i=0;
  37. for (; n; i++)
  38. n >>= 1;
  39.  
  40. return i;
  41. }
  42. // ---------------------------------------------------------------------------------
  43. // Return the minimum number of bits needed to represent n in binary form
  44. // ( same as bits_mincount, but this one is calculated recursively )
  45. //
  46. unsigned int bits_mincount2( NType n, unsigned int bitcount )
  47. {
  48. if (n == 0)
  49. return bitcount;
  50.  
  51. return bits_mincount2( n >> 1, ++bitcount );
  52. }
  53. // ------------------------------------------------------------------------------------
  54. // Return the number of bits that are set to On in the binary representation of n
  55. //
  56. // ---------------------------------------------------------------------------------
  57. // >> is the right bit-shift operator. It drops the right-most bit from the binary
  58. // representation of an integer. So, 0x1001 >> 1 is equal to 0x0100.
  59. //
  60. unsigned int bits_1count( NType n )
  61. {
  62. unsigned int count = 0;
  63. while (n) {
  64. count += (n & 1);
  65. n >>= 1;
  66. }
  67.  
  68. return count;
  69. }
  70. // ------------------------------------------------------------------------------------
  71. // Print n in binary form, using all the bits it ocupies in the memory. If splitbytes
  72. // is true, then a space is used every 8 bits, to separate bytes. suffix is appended
  73. // at the end
  74. //
  75. void bits_print( const NType n, const _Bool splitbytes, char *suffix )
  76. {
  77. unsigned int i=0, ndigs = sizeof(n) * 8;
  78.  
  79. while ( i < ndigs ) {
  80. if ( splitbytes && i != 0 && i % 8 == 0)
  81. putchar(' ');
  82. printf("%c", n & (1 << (ndigs - ++i)) ? '1' : '0');
  83. }
  84. printf( suffix );
  85.  
  86. return;
  87. }
  88. //-----------------------------------------------------------------------------------
  89. // Print n in binary form, using the minimum bits needed. If splitbytes is true, then
  90. // a space is used every 8 bits, to separate bytes. bitcount must always be 0 (due to
  91. // the recusrive implementation of the function).
  92. //
  93. void bits_printmin( NType n, int bitcount, const _Bool splitbytes )
  94. {
  95. if (n == 0) {
  96. return;
  97. }
  98.  
  99. bits_printmin( n >> 1, ++bitcount, splitbytes );
  100. if (splitbytes && bitcount != 0 && bitcount % 8 == 0)
  101. putchar(' ');
  102. printf("%c", (n & 1) ? '1' : '0');
  103.  
  104. }
  105. // ------------------------------------------------------------------------------------
  106. void showbinary( NType n )
  107. {
  108. while ( n ) {
  109. printf("%c", (n & 1) ? '1' : '0');
  110. n >>= 1;
  111. }
  112.  
  113. return;
  114. }
  115. // ------------------------------------------------------------------------------------
  116. char *s_reverse( char *s )
  117. {
  118. if ( !s ) // early exit
  119. return NULL;
  120.  
  121. char *cp1, *cp2, dummy;
  122.  
  123. cp2 = s + strlen(s) - 1; // set cp2 at end of s
  124. for (cp1=s; cp1 < cp2; cp1++, cp2--) {
  125. dummy = *cp1;
  126. *cp1 = *cp2;
  127. *cp2 = dummy;
  128. }
  129.  
  130. return s;
  131. }
  132. // ------------------------------------------------------------------------------------
  133. char *bits_min2str( char *s, const size_t maxlen, NType n )
  134. {
  135. if ( !s )
  136. return NULL;
  137.  
  138. register char *cp=s;
  139. while ( n && cp-s < maxlen ) {
  140. *cp++ = ( n & 1) ? '1' : '0';
  141. n >>= 1;
  142. }
  143. *cp = '\0';
  144. s_reverse( s );
  145.  
  146. return s;
  147. }
  148. // ------------------------------------------------------------------------------------
  149. char *bits_all2str( char *s, const size_t maxlen, NType n )
  150. {
  151. if ( !s )
  152. return NULL;
  153.  
  154. memset(s, '0', maxlen);
  155. register int i = maxlen-1;
  156. s[ i--] = '\0';
  157. while ( n && i > -1) {
  158. s[i--] = ( n & 1) ? '1' : '0';
  159. n >>= 1;
  160. }
  161.  
  162. return s;
  163. }
  164. // ------------------------------------------------------------------------------------
  165. void bits_printstr( const char *s, _Bool splitbytes, const char *suffix )
  166. {
  167. if ( !s )
  168. return;
  169.  
  170. register int i = 0;
  171. while ( s[i] )
  172. {
  173. if ( splitbytes && i != 0 && i % 8 == 0)
  174. putchar(' ');
  175. putchar( s[i++] );
  176. }
  177. printf(suffix);
  178.  
  179. return;
  180. }
  181. // ------------------------------------------------------------------------------------
  182. char *s_path2fname( const char *path, const char delim )
  183. {
  184. if ( !path )
  185. return NULL;
  186. if ( !*path )
  187. return (char *)path;
  188.  
  189. char *ret = (char *) (path + strlen( path )-1);
  190. while ( ret > path && *--ret != delim )
  191. ;
  192.  
  193. return ret == path ? ret : ++ret;
  194. }
  195. // ------------------------------------------------------------------------------------
  196. int main( int argc, char **argv )
  197. {
  198. NType n = 130; // default value for n
  199. NType maxval = bits_maxval(); // maximum possible value for n
  200. const _Bool SPLITBYTES = true;
  201.  
  202. int smaxlen = 8 * sizeof(NType) + 1;
  203. char s[ smaxlen ];
  204.  
  205. if (argc > 1)
  206. n = (n = abs( atoi( argv[1] )) ) == 0 ? 1 : n;
  207. else
  208. printf("\nusage:\t%s num ( 0 < num < %llu )\n\t*** assuming num = %llu\n",
  209. s_path2fname( argv[0], '\\'),
  210. (unsigned long long) maxval+1,
  211. (unsigned long long) n );
  212. putchar('\n');
  213.  
  214. printf( "%llu occupies %u bits in memory, it uses %u bits, it has %u bit(s) set to On\n( the maximum representable decimal positive value is %llu )\n",
  215. (unsigned long long) n,
  216. bits_count(n),
  217. bits_mincount(n),
  218. bits_1count(n),
  219. (unsigned long long) maxval+1 );
  220.  
  221. puts("\nBinary Representation\n---------------------");
  222.  
  223. printf("Used bits: ");
  224. bits_printmin(n, 0, SPLITBYTES);
  225.  
  226. printf("\nAll bits: ");
  227. bits_print(n, SPLITBYTES, "\n");
  228.  
  229. puts("\nOther Representations\n---------------------");
  230. printf("OCTal : %o\nDECimal : %llu\nHEXadecimal : %x\n",
  231. n, (unsigned long long) n, n );
  232.  
  233. putchar('\n');
  234. return 0;
  235. }
  236.  
stdin
Standard input is empty
compilation info
prog.c: In function ‘bits_print’:
prog.c:84: warning: format not a string literal and no format arguments
prog.c: In function ‘bits_printstr’:
prog.c:177: warning: format not a string literal and no format arguments
prog.c: In function ‘main’:
prog.c:203: warning: unused variable ‘s’
stdout
usage:	./prog num ( 0 < num < 2147483648 )
	*** assuming num = 130

130 occupies 32 bits in memory, it uses 8 bits, it has 2 bit(s) set to On
( the maximum representable decimal positive value is 2147483648 )

Binary Representation
---------------------
Used bits:  10000010
All  bits: 00000000 00000000 00000000 10000010

Other Representations
---------------------
OCTal       : 202
DECimal     : 130
HEXadecimal : 82