Advertisement
juaniisuar

Untitled

Aug 22nd, 2017
390
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <gmp.h>
  4. typedef struct { short int n[16]; }nro;
  5.  
  6. void print(nro n)
  7. {
  8. int i;
  9. char str[1024];
  10. short sign[16], num[16];
  11. mpz_t n1, n2;
  12. for (i=0; i<15; i++) {
  13. num[i] = n.n[i];
  14. sign[i] = 0;
  15. }
  16. num[15] = n.n[15] & 0x7FFF;
  17. sign[15] = n.n[15] & 0x8000;
  18. mpz_init(n1);
  19. mpz_init(n2);
  20. mpz_import(n1, 16, -1, 2, 0, 0, num);
  21. mpz_import(n2, 16, -1, 2, 0, 0, sign);
  22. mpz_neg(n2, n2);
  23. mpz_add(n2, n1, n2);
  24. mpz_get_str(str, 10, n2);
  25. printf("Número: %s\n", str);
  26. mpz_clear(n1);
  27. mpz_clear(n2);
  28. }
  29.  
  30.  
  31. int igual_0(nro num){
  32. int i;
  33. for (i = 0; i < 16; ++i)
  34. if(num.n[i] != 0)
  35. return 0;
  36. return 1;
  37. }
  38. int igual_1(nro num){
  39. int i;
  40. if(num.n[0] != 1)
  41. return 0;
  42. for (i = 1; i < 16; ++i)
  43. if(num.n[i] != 0)
  44. return 0;
  45. return 1;
  46. }
  47. int par(nro num){
  48. return !(num.n[0] & 1);
  49. }
  50. nro shift_i(nro num){
  51. int i,final_b,b1 = 0,b2;
  52. final_b = 1 << (sizeof(short int) * 8 - 1);
  53. for (i = 0; i < 16; ++i){
  54. b2 = num.n[i] & final_b ? 1 : 0;
  55. num.n[i] = num.n[i] << 1;
  56. num.n[i] |= b1;
  57. b1 = b2;
  58. }
  59. return num;
  60. }
  61. int main(){
  62. int i;
  63. nro n;
  64. for(i = 1; i < 16; ++i)
  65. n.n[i] = 0;
  66. n.n[0] = 1 << 15;
  67. print(n);
  68. n = shift_i(n);
  69. print(n);
  70.  
  71. return 0;
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement