whiplk

[INCLUDE] - ibits(beta)

Oct 13th, 2013
335
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 3.12 KB | None | 0 0
  1. /*
  2. * --- iBits ---
  3. * Por Willian Luigi
  4. * Atualizado - [15/10/2013]
  5. * Atualizado - [04/11/2013]
  6. * [iPs]TeaM
  7. * mixmusicas.com.br ipsbr.net
  8. *
  9. */
  10. #if defined _ibits_included
  11. #endinput
  12. #endif
  13. #define _ibits_included
  14. #pragma library ibits
  15.  
  16. enum IPSI_BITS (<<=1)
  17. {
  18.     bit16 = 2,
  19.     bit8,
  20.     bit4,
  21.     bit2,
  22.     bit1
  23. };
  24.  
  25. stock partBits(n,inicio,fim) {
  26.     return (n >> inicio) & ~(~0 << (fim - inicio + 1));
  27. }
  28.  
  29. // 1 bit
  30. #define array_bit1:%0<%1,%2> \
  31.                     %0[(%1 + cellbits) / cellbits][(%1 + cellbits) / cellbits]
  32. #define bit1:%0<%1> \
  33.                     %0[(%1 + cellbits) / cellbits]
  34. #define getBit1(%0,%1) \
  35.                     ( ( %0[%1 / cellbits] >> %1 ) & 0x01 )
  36. stock setBit1(arr[], slot, val)
  37. {
  38.     return val ? (arr[slot / cellbits] ^= ((_:val % 0x02) << slot)) : (arr[slot/cellbits] &= ~(0x01 << slot));
  39. }
  40.  
  41. // 2 bits
  42. #define bit2:%0<%1> \
  43.                     %0[(%1 + _:bit2) / _:bit2]
  44. #define getBit2(%0,%1) \
  45.                     ( %0[%1 / _:bit2] >> ( %1 << 1 ) ) % 0x04
  46. stock setBit2(arr[], slot, val)
  47. {
  48.     return arr[slot/_:bit2] =  ((arr[slot/_:bit2] & ~(0x03 << (slot << 1)))) | ((_:val % 0x04) << (slot << 1));
  49. }
  50.  
  51. // 4 bits
  52. #define bit4:%0<%1> \
  53.                     %0[(%1 + _:bit4) / _:bit4]
  54. #define getBit4(%0,%1) \
  55.                     ( %0[%1 / _:bit4] >> ( %1 << 2 ) ) % 0x10
  56. stock setBit4(arr[], slot, val)
  57. {
  58.     return arr[slot/_:bit4] =  ((arr[slot/_:bit4] & ~(0xF << (slot << 2)))) | ((_:val % 0x10) << (slot << 2));
  59. }
  60.  
  61. // 16 bits
  62. #define bit16:%0<%1> \
  63.                     %0[(%1 + _:bit16) / _:bit16]
  64. #define getBit16(%0,%1) \
  65.                     ( %0[%1 / _:bit16] >> ( %1 << 4 ) ) % (65536)
  66. stock setBit16(arr[], slot, val)
  67. {
  68.     return arr[slot/_:bit16] =  ((arr[slot/_:bit16] & ~(65535 << (slot << 4)))) | ((_:val % 65536) << (slot << 4));
  69. }
  70.  
  71.  
  72. //geral bits
  73. #define bit_var%0<%1@%2> \
  74.                     new %0[(%1 + _:%2) / _:%2]
  75. #define bit_var_set:%3(%0,%1,%2) \
  76.                     setBit%3(%0,%1,%2)
  77. #define bit_var_get:%2(%0,%1) \
  78.                     getBit%2(%0,%1)
  79.  
  80. #define bit_array%0<%1,%2@%3> \
  81.                     new %0[(_:%1)][(_:%2 + _:%3) / _:%3]
  82. #define bit_array_set:%4(%0,%1,%2,%3) \
  83.                     setBit%4(%0[%1],_:%2,%3)
  84.  
  85. #define bit_array_get:%3(%0,%1,%2) \
  86.                     getBit%3(%0[%1],_:%2)
  87.  
  88.  
  89. // funcionalidades
  90.  
  91. #define size(%0,%1) \
  92.                 (sizeof(%0) * _:%1) //Retorna o tamanho da estrutura
  93.  
  94.  
  95. stock bool:any(arr[], slot, IPSI_BITS: type) //Verifica se algum bit da sequência está ativo(1)
  96. {
  97.     switch(_:type)
  98.     {
  99.         case bit16: return bool:(getBit16(arr, slot) ^ 0b0) && 1;
  100.         case bit4: return bool:(getBit4(arr, slot) ^ 0b0) && 1;
  101.         case bit2: return bool:(getBit2(arr, slot) ^ 0b0) && 1;
  102.         case bit1: return bool:(getBit1(arr, slot) ^ 0b0) && 1;
  103.         default: return false;
  104.     }
  105.     return false;
  106. }
  107. stock bool:none(arr[], slot, IPSI_BITS: type) //Verifica se nenhum bit da sequência está ativo(1)
  108. {
  109.     switch(_:type)
  110.     {
  111.         case bit16: return bool:(!(getBit16(arr, slot) ^ 0b0)) && 1;
  112.         case bit4: return bool:(!(getBit4(arr, slot) ^ 0b0)) && 1;
  113.         case bit2: return bool:(!(getBit2(arr, slot) ^ 0b0)) && 1;
  114.         case bit1: return bool:(!(getBit1(arr, slot) ^ 0b0)) && 1;
  115.         default: return false;
  116.     }
  117.     return false;
  118. }
Advertisement
Add Comment
Please, Sign In to add comment