#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <math.h>
#define CHECK_BIT(var,pos) ((var) & (1<<(pos)))
typedef unsigned int NType; // you may change int to char, short or long
// ------------------------------------------------------------------------------------
// Return the maximum representable value by the data type NType
//
NType bits_maxval( void )
{
NType ret=1;
register int ndigs = sizeof(NType) * 8;
while ( ndigs ) {
ret
+= abs( pow(2, ndigs
--) ); }
return ret;
}
// ------------------------------------------------------------------------------------
// Return the size of n in bits
//
unsigned int bits_count( NType n )
{
return (unsigned int) (8 * sizeof( n ) );
}
// ---------------------------------------------------------------------------------
// Return the minimum number of bits needed to represent n in binary form
//
unsigned int bits_mincount( NType n )
{
register int i=0;
for (; n; i++)
n >>= 1;
return i;
}
// ---------------------------------------------------------------------------------
// Return the minimum number of bits needed to represent n in binary form
// ( same as bits_mincount, but this one is calculated recursively )
//
unsigned int bits_mincount2( NType n, unsigned int bitcount )
{
if (n == 0)
return bitcount;
return bits_mincount2( n >> 1, ++bitcount );
}
// ------------------------------------------------------------------------------------
// Return the number of bits that are set to On in the binary representation of n
//
// ---------------------------------------------------------------------------------
// >> is the right bit-shift operator. It drops the right-most bit from the binary
// representation of an integer. So, 0x1001 >> 1 is equal to 0x0100.
//
unsigned int bits_1count( NType n )
{
unsigned int count = 0;
while (n) {
count += (n & 1);
n >>= 1;
}
return count;
}
// ------------------------------------------------------------------------------------
// Print n in binary form, using all the bits it ocupies in the memory. If splitbytes
// is true, then a space is used every 8 bits, to separate bytes. suffix is appended
// at the end
//
void bits_print( const NType n, const _Bool splitbytes, char *suffix )
{
unsigned int i=0, ndigs = sizeof(n) * 8;
while ( i < ndigs ) {
if ( splitbytes && i != 0 && i % 8 == 0)
printf("%c", n
& (1 << (ndigs
- ++i
)) ? '1' : '0'); }
return;
}
//-----------------------------------------------------------------------------------
// Print n in binary form, using the minimum bits needed. If splitbytes is true, then
// a space is used every 8 bits, to separate bytes. bitcount must always be 0 (due to
// the recusrive implementation of the function).
//
void bits_printmin( NType n, int bitcount, const _Bool splitbytes )
{
if (n == 0) {
return;
}
bits_printmin( n >> 1, ++bitcount, splitbytes );
if (splitbytes && bitcount != 0 && bitcount % 8 == 0)
printf("%c", (n
& 1) ? '1' : '0');
}
// ------------------------------------------------------------------------------------
void showbinary( NType n )
{
while ( n ) {
printf("%c", (n
& 1) ? '1' : '0'); n >>= 1;
}
return;
}
// ------------------------------------------------------------------------------------
char *s_reverse( char *s )
{
if ( !s ) // early exit
return NULL;
char *cp1, *cp2, dummy;
cp2
= s
+ strlen(s
) - 1; // set cp2 at end of s for (cp1=s; cp1 < cp2; cp1++, cp2--) {
dummy = *cp1;
*cp1 = *cp2;
*cp2 = dummy;
}
return s;
}
// ------------------------------------------------------------------------------------
char *bits_min2str( char *s, const size_t maxlen, NType n )
{
if ( !s )
return NULL;
register char *cp=s;
while ( n && cp-s < maxlen ) {
*cp++ = ( n & 1) ? '1' : '0';
n >>= 1;
}
*cp = '\0';
s_reverse( s );
return s;
}
// ------------------------------------------------------------------------------------
char *bits_all2str( char *s, const size_t maxlen, NType n )
{
if ( !s )
return NULL;
register int i = maxlen-1;
s[ i--] = '\0';
while ( n && i > -1) {
s[i--] = ( n & 1) ? '1' : '0';
n >>= 1;
}
return s;
}
// ------------------------------------------------------------------------------------
void bits_printstr( const char *s, _Bool splitbytes, const char *suffix )
{
if ( !s )
return;
register int i = 0;
while ( s[i] )
{
if ( splitbytes && i != 0 && i % 8 == 0)
}
return;
}
// ------------------------------------------------------------------------------------
char *s_path2fname( const char *path, const char delim )
{
if ( !path )
return NULL;
if ( !*path )
return (char *)path;
char *ret
= (char *) (path
+ strlen( path
)-1); while ( ret > path && *--ret != delim )
;
return ret == path ? ret : ++ret;
}
// ------------------------------------------------------------------------------------
int main( int argc, char **argv )
{
NType n = 130; // default value for n
NType maxval = bits_maxval(); // maximum possible value for n
const _Bool SPLITBYTES = true;
int smaxlen = 8 * sizeof(NType) + 1;
char s[ smaxlen ];
if (argc > 1)
n
= (n
= abs( atoi( argv
[1] )) ) == 0 ? 1 : n
; else
printf("\nusage:\t%s num ( 0 < num < %llu )\n\t*** assuming num = %llu\n", s_path2fname( argv[0], '\\'),
(unsigned long long) maxval+1,
(unsigned long long) n );
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", (unsigned long long) n,
bits_count(n),
bits_mincount(n),
bits_1count(n),
(unsigned long long) maxval+1 );
puts("\nBinary Representation\n---------------------");
bits_printmin(n, 0, SPLITBYTES);
bits_print(n, SPLITBYTES, "\n");
puts("\nOther Representations\n---------------------"); printf("OCTal : %o\nDECimal : %llu\nHEXadecimal : %x\n", n, (unsigned long long) n, n );
return 0;
}