20.04.2013, 06:58
Some people does not understand the idea of manipulating and checking bits inside 32 bit variables.
You can use the macros below to manipulate and/or check for bits inside 32 bit variables.
You can use the macros below to manipulate and/or check for bits inside 32 bit variables.
pawn Code:
// Sets a single bit at a special position (0-31)
// Usage: set_bit(variable, position)
#define set_bit(%0,%1) %0 |= 1<<(%1)
// Sets multiple bits related to the second argument
// Usage: set_bits(variable, value)
#define set_bits(%0,%1) %0 |= (%1)
// Unsets a single bit at a special position (0-31)
// Usage: unset_bit(variable, position)
#define unset_bit(%0,%1) %0 &= ~(1<<(%1))
// Unsets multiple bits related to the second argument
// Usage: unset_bits(variable, value)
#define unset_bits(%0,%1) %0 &= ~(%1)
// Toggles a single bit at a special position (0-31)
// Usage: toggle_bit(variable, position)
#define toggle_bit(%0,%1) %0 ^= 1<<(%1)
// Toggles multiple bits related to the second argument
// Usage: toggle_bits(variable, value)
#define toggle_bits(%0,%1) %0 ^= (%1)
// Returns true, if the bit at a special position (0-31) is enabled otherwise false
// Usage: is_bit(value, position)
#define is_bit(%0,%1) (!!((%0)&(1<<(%1))))
// Returns true, if ONLY the bit at a special position (0-31) is enabled otherwise false
// Usage: is_only_bit(value, position)
#define is_only_bit(%0,%1) ((%0) == (1<<(%1)))
// Returns true, if the bits related to the second argument are enabled otherwise false
// Usage: are_bits(value, value_2)
#define are_bits(%0,%1) (((%0)&(%1)) == (%1))
// Returns true, if ONLY the bits related to the second argument are enabled otherwise false
// Usage: are_only_bits(value, value_2)
#define are_only_bits(%0,%1) ((%0) == (%1))
// Returns true, if the bit at a special position (0-31) is disabled otherwise false
// Usage: isnot_bit(value, position)
#define isnot_bit(%0,%1) (!((%0)&(1<<(%1))))
// Returns true, if the bits related to the second argument are disabled otherwise false
// Usage: arenot_bits(value, value_2)
#define arenot_bits(%0,%1) (((%0)&(%1)) == 0)
// Returns true, if ONLY the bits related to the second argument are disabled otherwise false
// Usage: arenot_only_bits(value, value_2)
#define arenot_only_bits(%0,%1) ((~(%0)) == (%1))