C_far

Bits operators

Mar 2nd, 2017
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 4.94 KB | None | 0 0
  1. #include "a_samp"
  2. #define T<%0> a%0
  3.  
  4. enum{a0,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15}
  5.  
  6. main()
  7. {  
  8.     /*
  9.         Bitwise OR
  10.    
  11.         0 OR 0 = 0
  12.         0 OR 1 = 1
  13.         1 OR 0 = 1
  14.         1 OR 1 = 1
  15.    
  16.     */
  17.     func("|", "0011", T<1> | T<2>, false); // 0001 | 0010 = 0011
  18.     func("|", "0101", T<1> | T<4>, false); // 0001 | 0100 = 0101
  19.     func("|", "0111", T<1> | T<2> | T<4>, false); // 0001 | 0010 | 0100 = 0111
  20.     func("|", "1001", T<1> | T<8>, false); // 0001 | 1000 = 1001
  21.     func("|", "1010", T<2> | T<8>, false); // 0010 | 1000 = 1010
  22.     func("|", "1011", T<1> | T<2> | T<8>, false); // 0001 | 0010 | 1000 = 1011
  23.     func("|", "1011", T<1> | T<10>, false); // 0001 | 1010 = 1011
  24.     func("|", "1101", T<8> | T<5>, false); // 1000 | 0101 = 1101
  25.     func("|", "1111", T<10> | T<5>, false); // 1010 | 0101 = 1111
  26.    
  27.     //------------------------------------------------------------------------------------------------------
  28.    
  29.     /*
  30.         Bitwise AND
  31.    
  32.         0 AND 0 = 0
  33.         0 AND 1 = 0
  34.         1 AND 0 = 0
  35.         1 AND 1 = 1
  36.    
  37.     */
  38.     func("&", "0000", T<4> & T<8>, false); // 0100 & 1000 = 0000
  39.     func("&", "0001", T<1> & T<5>, false); // 0001 & 0101 = 0001
  40.     func("&", "0010", T<2> & T<10>, false); // 0010 & 1010 = 0010
  41.     func("&", "0100", T<4> & T<5>, false); // 0100 & 0101 = 0100
  42.     func("&", "1000", T<8> & T<10>, false); // 1000 & 1010 = 1000
  43.     func("&", "1010", T<10> & T<11>, false); // 1010 & 1011 = 1010
  44.     func("&", "0010", (T<10> & T<15>) & T<2>, false); // (1010 & 1111) & 0010 = 1010 & 0010 = 0010
  45.    
  46.     //------------------------------------------------------------------------------------------------------
  47.    
  48.     /*
  49.         Bitwise XOR
  50.    
  51.         0 XOR 0 = 1
  52.         0 XOR 1 = 0
  53.         1 XOR 0 = 0
  54.         1 XOR 1 = 1
  55.    
  56.     */
  57.     func("^", "0010", T<8> ^ T<10>, false); // 1000 ^ 1010 = 0010
  58.     func("^", "0100", T<11> ^ T<15>, false); // 1011 ^ 1111 = 0100
  59.    
  60.     //------------------------------------------------------------------------------------------------------
  61.    
  62.     /*
  63.         Bitwise NOT
  64.    
  65.         NOT 0 = 1
  66.         NOT 1 = 0
  67.    
  68.     */
  69.     func("~", "11111111111111111111111111111010", ~T<5>);
  70.     // ~00000000000000000000000000000101 = 11111111111111111111111111111010
  71.    
  72.     func("~", "11111111111111111111111111110011", ~(T<4> | T<8>));
  73.     // ~(0100 | 1000) = ~00000000000000000000000000001100 = 11111111111111111111111111110011
  74.    
  75.     func("~", "11111111111111111111111111110011", ~(T<5> & T<15>));
  76.     // ~(0101 & 1111) = ~00000000000000000000000000000101 = 11111111111111111111111111110011
  77.    
  78.     //------------------------------------------------------------------------------------------------------
  79.    
  80.     // Bitwise arithmetic shift
  81.     func("<<", "0010", T<1> << 1, false); // 0001 << 1 = 0010
  82.     func("<<", "0100", T<1> << 2, false); // 0001 << 2 = 0100
  83.     func("<<", "1000", T<1> << 3, false); // 0001 << 3 = 1000
  84.     func("<<", "1010", T<5> << 1, false); // 0101 << 1 = 1010
  85.     func(">>", "0100", T<8> >> 1, false); // 1000 >> 1 = 0100
  86.     func(">>", "0010", T<8> >> 2, false); // 0001 >> 2 = 0010
  87.     func(">>", "0001", T<8> >> 3, false); // 0001 >> 3 = 0001
  88.     func(">>", "0101", T<10> >> 1, false); // 1010 >> 1 = 0101
  89.    
  90.     /*
  91.         Attention, if you use the bit right shift and the 32th bit is 1 (so a value negative),
  92.         the bits will not shift, but will be added.
  93.        
  94.         Example, you think that this operation will give this result ? :
  95.         11111111111111111111111111111000 (-8) >> 2 = 00111111111111111111111111111110 (1073741822)
  96.        
  97.         But no, this operation will give this result :
  98.         11111111111111111111111111111000 (-8) >> 2 = 11111111111111111111111111111110 (-2)
  99.        
  100.         Conclusion, when you use the bit right shift on negative value, it remains negative.
  101.     */
  102.    
  103.     func(">>", "11111111111111111111111111111110", 0b11111111111111111111111111111000 >> 2);
  104.    
  105.     //------------------------------------------------------------------------------------------------------
  106.    
  107.     /*
  108.         Bitwise logical shift
  109.    
  110.         If you understood the explanations before for the bit right shift, there is solution
  111.         to shift to the right on a negative value with the bit logical shift operator.
  112.        
  113.         Let us take the example of before :
  114.         11111111111111111111111111111000 (-8) >> 2 = 00111111111111111111111111111110 (1073741822)
  115.        
  116.         It is false, but if I use the bit logical shift operator :
  117.         11111111111111111111111111111000 (-8) >>> 2 = 00111111111111111111111111111110 (1073741822)
  118.        
  119.         This is true.
  120.        
  121.         You are wondering if there is the bit logical shift "left" ? In Pawn : no.
  122.         That's why when we say "bit logical shift", we don't mention the "direction",
  123.         because there only the right.
  124.        
  125.     */
  126.    
  127.     func(">>>", "00111111111111111111111111111110", 0b11111111111111111111111111111000 >>> 2);
  128. }
  129.  
  130. func(const o[], const str[], value, bool:b32 = true)
  131. {
  132.     static
  133.         n = 1;
  134.  
  135.     if(b32)
  136.     {
  137.         printf("________________________________%03d (%s)", n, o);
  138.         print(str);
  139.         printf("%032b", value);
  140.         print("¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯");
  141.     }
  142.        
  143.     else
  144.     {
  145.         printf("____%03d (%s)", n, o);
  146.         print(str);
  147.         printf("%04b", value);
  148.         print("¯¯¯¯");
  149.     }
  150.    
  151.     n++;
  152. }
Advertisement