/* mostra la rapresentazione a bit di - integer - unsigned - float uso: ./bit_analyser i -10 ./bit_analyser u 127 ./bit_analyser f 0.123 [https://en.wikipedia.org/wiki/Single_precision] */ #include #include #include #include void printByte(char byte); void analizzaInt(int *n); void analizzaUns(unsigned *n); void analizzaFlo(float *n); int main(int argc, char *argv[]) { int i, n; unsigned un; float f; char *ptr; if (argc < 3) { printf("\nUso, ad es.: ./bit_analysier i -2345\n\n"); return 0; } if ( strcmp("i", argv[1]) == 0 ) { printf("Analizzo un integer \n"); n = atoi(argv[2]); analizzaInt(&n); } else if ( strcmp("u", argv[1]) == 0 ) { printf("Analizzo un unsigned \n"); un = (unsigned) strtoul(argv[2], &ptr, 10); //https://www.tutorialspoint.com/c_standard_library/c_function_strtoul.htm analizzaUns(&un); } else if ( strcmp("f", argv[1]) == 0 ) { printf("Analizzo un float \n"); f = atof(argv[2]); analizzaFlo(&f); } else { printf("Type %s non riconosciuto\nAccettati: i, u, f\n", argv[1]); } return 0; } //--------------------------------------------------------- void printByte(char byte) { int j; for(j=0; j<8; j++) { printf("%d", (byte & (128 >> j)) && 1); if(j==3) printf(" "); } printf(" "); } void analizzaInt(int *n) { // max: 2^31 - 1 = 2147483647 // min: -2147483648 int i; char byte; unsigned char *pc = (unsigned char *)n; printf("Rappresentazione di %d\nHex: ", *n); for (i = 0; i < sizeof(int); i++) { printf("%02X ", *(pc - i + sizeof(int) - 1) ); } printf("\nBinary: "); for (i = 0; i < sizeof(int); i++) { printByte( *(pc - i + sizeof(int) - 1 ) ); } puts(""); } void analizzaUns(unsigned *n) { // max: 2^32 - 1 = 4294967295 int i; char byte; unsigned char *pc = (unsigned char *)n; printf("Rappresentazione di %u\nHex: ", *n); for (i = 0; i < sizeof(int); i++) { printf("%02X ", *(pc - i + sizeof(int) - 1) ); } printf("\nBinary: "); for (i = 0; i < sizeof(int); i++) { printByte( *(pc - i + sizeof(int) - 1 ) ); } puts(""); } void analizzaFlo(float *n) { int i,j; unsigned char *pc = (unsigned char *)n; // unsigned char *pc0 = (unsigned char *)n; unsigned char byte; unsigned char bits[sizeof(float)*8]; unsigned char ibit = sizeof(float)*8; char segno; char esponente; float frazione; float ricostruito; printf(" %13e: ", *n); for (i = 0; i < sizeof(float); i++) { printf("%02X ", *(pc - i + sizeof(float) - 1) ); printf(" "); } puts(""); segno=1; printf(" "); for (i = 0; i < sizeof(float); i++) { byte = *(pc -i + sizeof(float) - 1) ; for(j=0; j<8; j++) { bits[--ibit] = (byte & (128 >> j)) && 1; printf("%d", bits[ibit]); // non vale la pena di usare printByte() // visto che dobbiamo riempire bits[] if(j==3) printf(" "); } printf(" "); } if (bits[sizeof(float)*8 - 1]) { segno = -1; printf(" [-]"); } puts(""); esponente = -127; for (ibit=23; ibit<31; ibit++) { esponente += bits[ibit] * (1 << (ibit-23)); } printf("esponente: %d - 127: %d\n", esponente + 127, esponente); frazione = 1; for (i=1; i<23; i++) { frazione += bits[23-i] * pow(2,-i); } printf("frazione : %f\n", frazione); ricostruito = segno * pow(2, esponente) * frazione; printf("valore ricostruito dal contenuto dei bit %e\n\n", ricostruito); }