/************************************************************************* $Archive: /PACS/OnBoard/u_bits.h $ $Revision: 1.11 $ $Date: 2009/04/23 13:51:12 $ $Author: amazy $ $Log: u_bits.h,v $ Revision 1.11 2009/04/23 13:51:12 amazy 6.029 * * 10 3/14/06 4:32p Pacs Egse * Version 6.001 * Cleaned and commented *************************************************************************/ #define U_BITS_H /***************************************************************** SUMMARY ****************************************************************** SET_BIT(var, bits) ((var |= (bits)) CLEAR_BIT(var, bits) (var &=~(bits)) TEST_ONE_BIT(var, bits) ((var & (bits)) != 0) TEST_NOT_ONE(var, bits) ((~var & (bits)) != 0) TEST_ALL_BITS(var, bits) ((var & (bits)) == (bits)) TEST_NOT_ALL(var, bits) ((~var & (bits)) == (bits)) GET_BIT_POS(var, bitPos) ((var & (1 << bitPos)) >> bitPos) IS(var, mask, bits) ((var & (mask)) == (bits)) SET_BITS(var, mask, bits) {var = (var & ~(mask)) | (bits) ;} *****************************************************************/ /* MACRO : SET_BIT(var, bits) ************************** AUTHOR : AMazy USE : Sets one or more bits in a word. (Sets the bits to 1) PARAMETERS : var : the word in which bits will be set bits : the bits to set EXAMPLE : enum { REAL_DATA = 0x01, BLUE_DETECTOR = 0x02, } DWORD info = 0x00; SET_BIT(info, REAL_DATA); // now info == 0x01 SET_BIT(info, BLUE_DETECTOR); // now info == 0x03 SET_BIT(info, REAL_DATA | BLUE_DETECTOR); // now info == 0x03 */ #define SET_BIT(var, bits) ((var) |= (bits)) /* MACRO : CLEAR_BIT(var, bits) **************************** AUTHOR : AMazy USE : Clear one or more bits in a word. (Sets the bits to 0) PARAMETERS : var : the word in which bits will be cleard bits : the bits to clear EXAMPLE : enum { REAL_DATA = 0x01, BLUE_DETECTOR = 0x02, } DWORD info = 0x00; SET_BIT(info, REAL_DATA | BLUE_DETECTOR); // now info = 0x03 CLEAR_BIT(info, REAL_DATA); // now info = 0x02 CLEAR_BIT(info, BLUE_DETECTOR); // now info = 0x00 */ #define CLEAR_BIT(var, bits) ((var) &=~(bits)) /* MACRO : TEST_ONE_BIT(var, bits) ******************************* AUTHOR : AMazy USE : Tests if one of the bits is set in var PARAMETERS : var : the word in which bits will be tested bits : the bits to test WARNING : The macro actually test if AT LEAST ONE of the bits of 'bits' is set to one. EXAMPLE : enum { REAL_DATA = 0x01, BLUE_DETECTOR = 0x02, } DWORD info = 0x00; SET_BIT(info, REAL_DATA | BLUE_DETECTOR); // now info = 0x03 TEST_ONE_BIT(info, REAL_DATA) // returns TRUE CLEAR_BIT(info, BLUE_DETECTOR); // now info = 0x00 TEST_ONE_BIT(info, BLUE_DETECTOR) // returns FALSE TEST_ONE_BIT(info, REAL_DATA | BLUE_DETECTOR) // returns TRUE */ #define TEST_ONE_BIT(var, bits) (((var) & (bits)) != 0) /* MACRO : IS(var, mask, bits) *************************** AUTHOR : AMazy USE : Tests if all the bits of the mask are equal to the bits specified. PARAMETERS : var : the word in which bits will be tested mask : the bits to test bits : the target bits WARNING : The macro actually test if ALL of the bits of 'mask' are equal to bits EXAMPLE : enum { CRE_MODE = 1 << 0, // CRE_MODE is simply a mask CRE_MODE_SLOW = 0 << 0, CRE_MODE_FAST = 1 << 0, } DWORD info = 0x00; SET_BIT(info, CRE_MODE_FAST); // now info = 0x01 TEST_ONE_BIT(info, CRE_MODE_FAST) // returns TRUE TEST_ONE_BIT(info, CRE_MODE_SLOW) // since the value of CRE_MODE_SLOW is 0, this test // will alway return FALSE // but, if you write IS(info, CRE_MODE, CRE_MODE_SLOW) // you test if the CRE_MODE is CRE_MODE_SLOW // and, no matter the real value of CRE_MODE_SLOW, the test will return the good value. IS(info, CRE_MODE, CRE_MODE_SLOW) // returns FALSE SET_BIT(info, CRE_MODE_SLOW) // now info = 0x00 IS(info, CRE_MODE, CRE_MODE_SLOW) // returns TRUE */ #define IS(info, mask, bits) (((info) & (mask)) == (bits)) #define SET_BITS(var, mask, bits) {var = ((var) & ~(mask)) | ((bits) & (mask)) ;} /* MACRO : TEST_ALL_BITS(var, bits) ******************************* AUTHOR : AMazy USE : Tests if all the bits are set in var. PARAMETERS : var : the word in which bits will be tested bits : the bits to test WARNING : The macro actually test if ALL of the bits of 'bits' are set to one. EXAMPLE : enum { REAL_DATA = 0x01, BLUE_DETECTOR = 0x02, } DWORD info = 0x00; SET_BIT(info, REAL_DATA | BLUE_DETECTOR); // now info = 0x03 TEST_ALL_BITS(info, REAL_DATA | BLUE_DETECTOR) // returns TRUE TEST_ONE_BIT(info, REAL_DATA) // returns TRUE CLEAR_BIT(info, BLUE_DETECTOR); // now info = 0x01 TEST_ONE_BIT(info, BLUE_DETECTOR) // returns FALSE TEST_ONE_BIT(info, REAL_DATA | BLUE_DETECTOR) // returns TRUE TEST_ALL_BITS(info, REAL_DATA | BLUE_DETECTOR) // returns FALSE */ #define TEST_ALL_BITS(var, bits) (((var) & (bits)) == (bits)) /* MACRO : TEST_NOT_ONE(var, bits) ******************************* AUTHOR : AMazy USE : Tests if one of the bit is not set in var PARAMETERS : var : the word in which bits will be tested bits : the bits to test WARNING : The macro actually test if AT LEAST ONE of the bits of 'bits' is not set in var. EXAMPLE : enum { REAL_DATA = 0x01, BLUE_DETECTOR = 0x02, } DWORD info = 0x00; SET_BIT(info, REAL_DATA | BLUE_DETECTOR); // now info = 0x03 TEST_NOT_ONE(info, REAL_DATA | BLUE_DETECTOR) // returns FALSE TEST_ONE_BIT(info, REAL_DATA) // returns TRUE CLEAR_BIT(info, BLUE_DETECTOR); // now info = 0x01 TEST_NOT_ONE(info, REAL_DATA | BLUE_DETECTOR) // returns TRUE CLEAR_BIT(info, REAL_DATA); // now info = 0x00 TEST_NOT_ONE(info, REAL_DATA | BLUE_DETECTOR) // returns TRUE */ #define TEST_NOT_ONE(var, bits) ((~(var) & (bits)) != 0) /* MACRO : TEST_NOT_ALL(var, bits) ******************************* AUTHOR : AMazy USE : Tests if all of the bits are not set in var PARAMETERS : var : the word in which bits will be tested bits : the bits to test WARNING : The macro actually test if ALL of the bits of 'bits' are not set in var. EXAMPLE : enum { REAL_DATA = 0x01, BLUE_DETECTOR = 0x02, } DWORD info = 0x00; SET_BIT(info, REAL_DATA | BLUE_DETECTOR); // now info = 0x03 CLEAR_BIT(info, BLUE_DETECTOR); // now info = 0x01 TEST_NOT_ONE(info, REAL_DATA | BLUE_DETECTOR) // returns TRUE TEST_NOT_ALL(info, REAL_DATA | BLUE_DETECTOR) // returns FALSE CLEAR_BIT(info, REAL_DATA); // now info = 0x00 TEST_NOT_ONE(info, REAL_DATA | BLUE_DETECTOR) // returns TRUE TEST_NOT_ALL(info, REAL_DATA | BLUE_DETECTOR) // returns TRUE */ #define TEST_NOT_ALL(var, bits) ((~(var) & (bits)) == (bits)) /* MACRO : GET_BIT_POS(var, bitPos) ******************************** AUTHOR : AMazy USE : Tests if the bit at bitPos is set to 1 PARAMETERS : var : the word in which bits will be tested bitPos : the position of the bit to be tested EXAMPLE : DWORD info = 0x02; GET_BIT_POS(info, 0); //returns 0 GET_BIT_POS(info, 1); //returns 1 */ #define GET_BIT_POS(var, bitPos) (((var & (1 << bitPos)) >> bitPos) & 1)