Επειδή μου άρεσε η ιδέα με τα bits, έφτιαξα ένα πιο ολοκληρωμένο πρόγραμμα.
Δέχεται έναν θετικό ακέραιο μεγαλύτερο του 0 από τη γραμμή εντολών (αν του δώσετε αρνητικό το μετατρέπει σε θετικό, αν του δώσετε 0 το μετατρέπει σε 1, αν δεν του δώσετε τίποτε χρησιμοποιεί μια default τιμή ).
Ενημερώνει πόσα είναι τα ελάχιστα δυαδικά ψηφία που χρειάζονται για να απεικονιστεί σε δυαδική μορφή o ακέραιος, πόσα είναι τα συνολικά bits που καταλαμβάνει στη μνήμη, καθώς και πόσα από τα bits του είναι αναμμένα. Επίσης ενημερώνει για την μέγιστη δυνατή τιμή που μπορεί να δεχτεί για ακέραιο, ενώ τέλος τυπώνει τον ακέραιο σε δυαδική, οκταδική, δεκαδική & δεκεξαδική μορφή.
Του έχω βάλει σχόλια, αλλά στα Αγγλικά (ελπίζω να μην είναι πρόβλημα). Το παραθέτω σε περίπτωση που φανεί χρήσιμο σε κάποια παιδιά...
- Κώδικας: Επιλογή όλων
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <stdbool.h>
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 number of bits that are set to On in the binary representation of n
//
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)
putchar(' ');
printf("%c", n & (1 << (ndigs - ++i)) ? '1' : '0');
}
printf( suffix );
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 start with 0
// (due to the recursive 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)
putchar(' ');
printf("%c", (n & 1) ? '1' : '0');
}
// ------------------------------------------------------------------------------------
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;
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",
argv[0],
(unsigned long long) maxval+1,
(unsigned long long) n );
putchar('\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---------------------");
printf("Used bits: ");
bits_printmin(n, 0, SPLITBYTES);
printf("\nAll bits: ");
bits_print(n, SPLITBYTES, "\n");
puts("\nOther Representations\n---------------------");
printf("OCTal : %o\nDECimal : %llu\nHEXadecimal : %x\n",
n, (unsigned long long) n, n );
putchar('\n');
return 0;
}

