C_far icon

Bits operators

C_far | PRO | 03/02/17 04:55:54 PM UTC | 0 ⭐ | 7671 👁️ | Never ⏰ | []
Pawn |

4.94 KB

|

None

|

0 👍

/

0 👎

#include "a_samp"
#define T<%0> a%0
 
enum{a0,a1,a2,a3,a4,a5,a6,a7,a8,a9,a10,a11,a12,a13,a14,a15}
 
main()
{   
    /* 
        Bitwise OR
    
        0 OR 0 = 0
        0 OR 1 = 1
        1 OR 0 = 1
        1 OR 1 = 1
    
    */
    func("|", "0011", T<1> | T<2>, false); // 0001 | 0010 = 0011
    func("|", "0101", T<1> | T<4>, false); // 0001 | 0100 = 0101
    func("|", "0111", T<1> | T<2> | T<4>, false); // 0001 | 0010 | 0100 = 0111
    func("|", "1001", T<1> | T<8>, false); // 0001 | 1000 = 1001
    func("|", "1010", T<2> | T<8>, false); // 0010 | 1000 = 1010
    func("|", "1011", T<1> | T<2> | T<8>, false); // 0001 | 0010 | 1000 = 1011
    func("|", "1011", T<1> | T<10>, false); // 0001 | 1010 = 1011
    func("|", "1101", T<8> | T<5>, false); // 1000 | 0101 = 1101
    func("|", "1111", T<10> | T<5>, false); // 1010 | 0101 = 1111
    
    //------------------------------------------------------------------------------------------------------
    
    /* 
        Bitwise AND
    
        0 AND 0 = 0
        0 AND 1 = 0
        1 AND 0 = 0
        1 AND 1 = 1
    
    */
    func("&", "0000", T<4> & T<8>, false); // 0100 & 1000 = 0000
    func("&", "0001", T<1> & T<5>, false); // 0001 & 0101 = 0001
    func("&", "0010", T<2> & T<10>, false); // 0010 & 1010 = 0010
    func("&", "0100", T<4> & T<5>, false); // 0100 & 0101 = 0100
    func("&", "1000", T<8> & T<10>, false); // 1000 & 1010 = 1000
    func("&", "1010", T<10> & T<11>, false); // 1010 & 1011 = 1010
    func("&", "0010", (T<10> & T<15>) & T<2>, false); // (1010 & 1111) & 0010 = 1010 & 0010 = 0010
    
    //------------------------------------------------------------------------------------------------------
    
    /* 
        Bitwise XOR
    
        0 XOR 0 = 1
        0 XOR 1 = 0
        1 XOR 0 = 0
        1 XOR 1 = 1
    
    */
    func("^", "0010", T<8> ^ T<10>, false); // 1000 ^ 1010 = 0010
    func("^", "0100", T<11> ^ T<15>, false); // 1011 ^ 1111 = 0100
    
    //------------------------------------------------------------------------------------------------------
    
    /* 
        Bitwise NOT
    
        NOT 0 = 1
        NOT 1 = 0
    
    */
    func("~", "11111111111111111111111111111010", ~T<5>); 
    // ~00000000000000000000000000000101 = 11111111111111111111111111111010
    
    func("~", "11111111111111111111111111110011", ~(T<4> | T<8>)); 
    // ~(0100 | 1000) = ~00000000000000000000000000001100 = 11111111111111111111111111110011
    
    func("~", "11111111111111111111111111110011", ~(T<5> & T<15>)); 
    // ~(0101 & 1111) = ~00000000000000000000000000000101 = 11111111111111111111111111110011
    
    //------------------------------------------------------------------------------------------------------
    
    // Bitwise arithmetic shift
    func("<<", "0010", T<1> << 1, false); // 0001 << 1 = 0010
    func("<<", "0100", T<1> << 2, false); // 0001 << 2 = 0100
    func("<<", "1000", T<1> << 3, false); // 0001 << 3 = 1000
    func("<<", "1010", T<5> << 1, false); // 0101 << 1 = 1010
    func(">>", "0100", T<8> >> 1, false); // 1000 >> 1 = 0100
    func(">>", "0010", T<8> >> 2, false); // 0001 >> 2 = 0010
    func(">>", "0001", T<8> >> 3, false); // 0001 >> 3 = 0001
    func(">>", "0101", T<10> >> 1, false); // 1010 >> 1 = 0101
    
    /*
        Attention, if you use the bit right shift and the 32th bit is 1 (so a value negative), 
        the bits will not shift, but will be added.
        
        Example, you think that this operation will give this result ? :
        11111111111111111111111111111000 (-8) >> 2 = 00111111111111111111111111111110 (1073741822)
        
        But no, this operation will give this result :
        11111111111111111111111111111000 (-8) >> 2 = 11111111111111111111111111111110 (-2)
        
        Conclusion, when you use the bit right shift on negative value, it remains negative.
    */
    
    func(">>", "11111111111111111111111111111110", 0b11111111111111111111111111111000 >> 2);
    
    //------------------------------------------------------------------------------------------------------
    
    /*
        Bitwise logical shift
    
        If you understood the explanations before for the bit right shift, there is solution
        to shift to the right on a negative value with the bit logical shift operator.
        
        Let us take the example of before :
        11111111111111111111111111111000 (-8) >> 2 = 00111111111111111111111111111110 (1073741822)
        
        It is false, but if I use the bit logical shift operator :
        11111111111111111111111111111000 (-8) >>> 2 = 00111111111111111111111111111110 (1073741822)
        
        This is true.
        
        You are wondering if there is the bit logical shift "left" ? In Pawn : no. 
        That's why when we say "bit logical shift", we don't mention the "direction",
        because there only the right.
        
    */
    
    func(">>>", "00111111111111111111111111111110", 0b11111111111111111111111111111000 >>> 2);
}
 
func(const o[], const str[], value, bool:b32 = true)
{
    static 
        n = 1;
 
    if(b32)
    {
        printf("________________________________%03d (%s)", n, o);
        print(str);
        printf("%032b", value);
        print("¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯");
    }
        
    else
    {
        printf("____%03d (%s)", n, o);
        print(str);
        printf("%04b", value);
        print("¯¯¯¯");
    }
    
    n++;
}

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎

    
        
  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎

    
        
  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎