Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <gmp.h>
- typedef struct { short int n[16]; }nro;
- void print(nro n)
- {
- int i;
- char str[1024];
- short sign[16], num[16];
- mpz_t n1, n2;
- for (i=0; i<15; i++) {
- num[i] = n.n[i];
- sign[i] = 0;
- }
- num[15] = n.n[15] & 0x7FFF;
- sign[15] = n.n[15] & 0x8000;
- mpz_init(n1);
- mpz_init(n2);
- mpz_import(n1, 16, -1, 2, 0, 0, num);
- mpz_import(n2, 16, -1, 2, 0, 0, sign);
- mpz_neg(n2, n2);
- mpz_add(n2, n1, n2);
- mpz_get_str(str, 10, n2);
- printf("Número: %s\n", str);
- mpz_clear(n1);
- mpz_clear(n2);
- }
- int igual_0(nro num){
- int i;
- for (i = 0; i < 16; ++i)
- if(num.n[i] != 0)
- return 0;
- return 1;
- }
- int igual_1(nro num){
- int i;
- if(num.n[0] != 1)
- return 0;
- for (i = 1; i < 16; ++i)
- if(num.n[i] != 0)
- return 0;
- return 1;
- }
- int par(nro num){
- return !(num.n[0] & 1);
- }
- nro shift_i(nro num){
- int i,final_b,b1 = 0,b2;
- final_b = 1 << (sizeof(short int) * 8 - 1);
- for (i = 0; i < 16; ++i){
- b2 = num.n[i] & final_b ? 1 : 0;
- num.n[i] = num.n[i] << 1;
- num.n[i] |= b1;
- b1 = b2;
- }
- return num;
- }
- int main(){
- int i;
- nro n;
- for(i = 1; i < 16; ++i)
- n.n[i] = 0;
- n.n[0] = 1 << 15;
- print(n);
- n = shift_i(n);
- print(n);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement