2015-02-08 14:08:26 +00:00
|
|
|
/*
|
|
|
|
* bitmaskros.h
|
|
|
|
*
|
|
|
|
* Helper macros for large bit masks management
|
|
|
|
*
|
|
|
|
* Copyright (C) 2008 Jean-Philippe Meuret
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Number of bits for 1 unsigned char */
|
2016-09-04 12:16:34 +00:00
|
|
|
#define nBitsPerUchar (sizeof(unsigned char) * 8)
|
2015-02-08 14:08:26 +00:00
|
|
|
|
|
|
|
/* Number of unsigned chars to contain a given number of bits */
|
2016-09-04 12:16:34 +00:00
|
|
|
#define nUcharsForNBits(nBits) ((((nBits)-1) / nBitsPerUchar) + 1)
|
2015-02-08 14:08:26 +00:00
|
|
|
|
|
|
|
/* Index=Offset of given bit in 1 unsigned char */
|
2016-09-04 12:16:34 +00:00
|
|
|
#define bitOffsetInUchar(bit) ((bit) % nBitsPerUchar)
|
2015-02-08 14:08:26 +00:00
|
|
|
|
|
|
|
/* Index=Offset of the unsigned char associated to the bit
|
|
|
|
at the given index=offset */
|
2016-09-04 12:16:34 +00:00
|
|
|
#define ucharIndexForBit(bit) ((bit) / nBitsPerUchar)
|
2015-02-08 14:08:26 +00:00
|
|
|
|
|
|
|
/* Value of an unsigned char with bit set at given index=offset */
|
2016-09-04 12:16:34 +00:00
|
|
|
#define ucharValueForBit(bit) (((unsigned char)(1)) << bitOffsetInUchar(bit))
|
2015-02-08 14:08:26 +00:00
|
|
|
|
|
|
|
/* Test the bit with given index=offset in an unsigned char array */
|
2016-09-04 12:16:34 +00:00
|
|
|
#define testBit(bit, array) ((array[ucharIndexForBit(bit)] >> bitOffsetInUchar(bit)) & 1)
|