Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Scrivere una function C che, fissato il numero n di bit, calcoli la rappresentazione di un intero:
- • per complemento a 2;
- • eccesso B (B-biased).*/
- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- #include<math.h>
- #define max_bit 16
- void complemento_a_2(unsigned short numero_bit, unsigned short bit[max_bit], short x);
- void eccesso_B(unsigned short numero_bit, unsigned short bit[max_bit], short x);
- int main()
- {
- unsigned short numero_bit, bit[max_bit];
- short x;
- printf("Inserisci il numero di bit = ");
- scanf("%hu", &numero_bit);
- printf("Inserisci il numero da rappresentare = ");
- scanf("%hd", &x);
- complemento_a_2(numero_bit, bit, x);
- eccesso_B(numero_bit, bit, x);
- return 0;
- }
- void complemento_a_2(unsigned short numero_bit, unsigned short bit[max_bit], short x)
- ///I numeri vengono già memorizzati dalla macchina in complemento a 2
- {
- char i;
- for (i = max_bit - numero_bit - 1; i < max_bit; i++)
- {
- bit[i] = x & 1;
- x = x >> 1;
- }
- printf("Rappresentazione in complemento a 2 = ");
- for (i = max_bit - numero_bit - 1; i < max_bit; i++)
- (i % 4 == 0) ? printf(" %hu", bit[i]) : printf("%hu", bit[i]);
- printf("\n");
- /* char i;
- for (i = 0; i < max_bit; i++) bit[i] = '0';
- i = max_bit - 1;
- do
- {
- bit[i] = (x % 2) + 48;
- x = x / 2;
- i--;
- } while (x != 0 && i >= 0);
- for (i = 0; i < max_bit; i++)
- {
- if (bit[i] == '0')
- bit[i] = '1';
- else
- bit[i] = '0';
- }
- if (bit[max_bit-1] == '0')
- bit[max_bit - 1] = '1';
- else
- bit[max_bit - 1] = '0';
- printf("In complemento a 2 = ");
- for (i = max_bit - numero_bit - 1; i < max_bit; i++)
- (i % 4 == 0) ? printf(" %c", bit[i]) : printf("%c", bit[i]);
- printf("\n");*/
- }
- void eccesso_B(unsigned short numero_bit, unsigned short bit[max_bit], short x)
- {
- char i;
- x = x + (pow(2, numero_bit - 1) - 1);
- i = max_bit - 1;
- while (x > 0)
- {
- bit[i--] = x % 2;
- x = x / 2;
- }
- printf("Rappresentazione in Biased = ");
- for (i = max_bit - numero_bit - 1; i < max_bit; i++)
- (i % 4 == 0) ? printf(" %hu", bit[i]) : printf("%hu", bit[i]);
- printf("\n");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement