nht_cntt

data lab

Oct 13th, 2016
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.52 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. int bitAnd(int, int);
  4. int getByte(int, int);
  5. int logicalShift(int, int);
  6. int bitCount(int);
  7. int bang(int);
  8. int tmin();
  9. int divpwr2(int, int);
  10. int negate(int);
  11. int isPositive(int);
  12. int isLessOrEqual(int, int);
  13. int ilog2(int x);
  14.    
  15. int main()
  16. {
  17.     return 0;
  18. }
  19.  
  20. int bitAnd(int x, int y)
  21. {
  22.     return ~(~x|~y);
  23. }
  24.  
  25. int getByte(int x, int n)
  26. {
  27.     return (x >> (n << 3)) & 0xff;
  28. }
  29.  
  30. int logicalShift(int x, int n)
  31. {
  32.     return (int)((unsigned)x >> n);
  33. }
  34.  
  35. int bitCount(int x)
  36. {
  37.     x = (x & 0x55555555) + ((x >> 1) & 0x55555555);
  38.     x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
  39.     x = (x & 0x0F0F0F0F) + ((x >> 4) & 0x0F0F0F0F);
  40.     x = (x & 0x00FF00FF) + ((x >> 8) & 0x00FF00FF);
  41.     x = (x & 0x0000FFFF) + ((x >> 16) & 0x0000FFFF);
  42.     return x;
  43. }
  44.  
  45. int bang(int x)
  46. {
  47.     int complement_2 = ~x + 1;
  48.     return ((~(complement_2 | x)) >> 31) & 0x1;
  49. }
  50.  
  51. int tmin()
  52. {
  53.     return (1 << 31);
  54. }
  55.  
  56. int divpwr2(int x, int n)
  57. {
  58.     return (int)(x >> n);
  59. }
  60.  
  61. int negate(int x)
  62. {
  63.     return ~x + 1;
  64. }
  65.  
  66. int isPositive(int x)
  67. {
  68.     return (!( x >> 31)) ^ (!x);
  69. }
  70.  
  71. int isLessOrEqual(int x, int y)
  72. {
  73.     return  (!(x^y)) | (((x+((~y)+1)) >> 31) & 1);
  74. }
  75.  
  76. int ilog2(int x)
  77. {
  78.     int i, j, k, l, m;
  79.     x = x | (x >> 1);
  80.     x = x | (x >> 2);
  81.     x = x | (x >> 4);
  82.     x = x | (x >> 8);
  83.     x = x | (x >> 16);
  84.     x = (x & 0x55555555) + ((x >> 1) & 0x55555555);
  85.     x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
  86.     x = (x & 0x0F0F0F0F) + ((x >> 4) & 0x0F0F0F0F);
  87.     x = (x & 0x0000FFFF) + ((x >> 16) & 0x0000FFFF);
  88.     x = x + ~0;
  89.     return x;
  90. }
Advertisement
Add Comment
Please, Sign In to add comment