Example #1
0
/***************************************************************************
*   Function   : BitArrayShiftLeft
*   Description: This function shifts the bits in a bit array to the left
*                by the amount of positions specified.
*   Parameters : ba - pointer to the bit array 0 is the msb of the first
*                     unsigned char in the bit array.
*                shifts - number of bits to shift by.
*   Effects    : The bit array data pointed to by ba is shifted to the left.
*                New bits shifted in will be set to 0.
*   Returned   : None
***************************************************************************/
void BitArrayShiftLeft(bit_array_t *ba, unsigned int shifts)
{
    int i, j;
    int chars = shifts / CHAR_BIT;  /* number of whole byte shifts */
    shifts = shifts % CHAR_BIT;     /* number of bit shifts remaining */

    if (ba == NULL)
    {
        return;         /* nothing to shift */
    }

    if (shifts >= ba->numBits)
    {
        /* all bits have been shifted off */
        BitArrayClearAll(ba);
        return;
    }

    /* first handle big jumps of bytes */
    if (chars > 0)
    {
        for (i = 0; (i + chars) <  BITS_TO_CHARS(ba->numBits); i++)
        {
            ba->array[i] = ba->array[i + chars];
        }

        /* now zero out new bytes on the right */
        for (i = BITS_TO_CHARS(ba->numBits); chars > 0; chars--)
        {
            ba->array[i - chars] = 0;
        }
    }

    /* now we have at most CHAR_BIT - 1 bit shifts across the whole array */
    for (i = 0; i < shifts; i++)
    {
        for (j = 0; j < BIT_CHAR(ba->numBits - 1); j++)
        {
            ba->array[j] <<= 1;

            /* handle shifts across byte bounds */
            if (ba->array[j + 1] & MS_BIT)
            {
                ba->array[j] |= 0x01;
            }
        }

        ba->array[BIT_CHAR(ba->numBits - 1)] <<= 1;
    }
}
Example #2
0
/***************************************************************************
*   Function   : BitArrayNot
*   Description: This function performs a bitwise NOT between two bit
*                arrays, storing the results in a third bit array.  If the
*                arrays are NULL or different in size, no operation will
*                will occur.
*   Parameters : dest - pointer to destination bit array
*                src1 - pointer to first source bit array
*                src2 - pointer to second source bit array
*   Effects    : dest will contain the results of a bitwise XOR of src1 and
*                src2 unless one array pointer is NULL or arrays are
*                different in size.
*   Returned   : NONE
***************************************************************************/
void BitArrayNot(bit_array_t *dest,
            const bit_array_t *src)
{
    int i, bits;
    unsigned char mask;

    if (src == NULL)
    {
        return;         /* no source array */
    }

    if (dest == NULL)
    {
        return;         /* no destination array */
    }

    if (src->numBits != dest->numBits)
    {
        return;         /* size mismatch */
    }

    /* NOT array one unsigned char at a time */
    for(i = 0; i < BITS_TO_CHARS(dest->numBits); i++)
    {
        dest->array[i] = ~(src->array[i]);
    }

    /* zero any spare bits so increment and decrement are consistent */
    bits = dest->numBits % CHAR_BIT;
    if (bits != 0)
    {
        mask = UCHAR_MAX << (CHAR_BIT - bits);
        dest->array[BIT_CHAR(dest->numBits - 1)] &= mask;
    }
}
Example #3
0
//---------------------------------------------------------------------------
void kbitarray_clean_bit(t_kbitarray *self, off_t bit_index) {
	unsigned char mask;

	/* create a mask to zero out desired bit */
	mask = BIT_IN_CHAR(bit_index);
	mask = ~mask;

	self->bitarray[BIT_CHAR(bit_index)] &= mask;
}
Example #4
0
/***************************************************************************
*   Function   : BitArraySetBit
*   Description: This function sets the specified bit to 1 in the bit array
*                passed as a parameter.
*   Parameters : ba - pointer to bit array
*                bit - bit to set
*   Effects    : The specified bit in the bit array is set to 1.
*   Returned   : NONE
***************************************************************************/
void BitArraySetBit(bit_array_t *ba, unsigned int bit)
{
    if (ba == NULL)
    {
        return;         /* no bit to set */
    }

    if (ba->numBits <= bit)
    {
        return;         /* bit out of range */
    }

    ba->array[BIT_CHAR(bit)] |= BIT_IN_CHAR(bit);
}
Example #5
0
/***************************************************************************
*   Function   : BitArrayDecrement
*   Description: This function decrements a bit array as if it is an
*                unsigned value of the specified number of bits.
*   Parameters : ba - pointer bit array to be decremented
*   Effects    : ba will contain the results of a decrement operation
*                performed on itself.  Any spare bits in the array of
*                unsigned characters containing the bits will be set to 0.
*   Returned   : None
***************************************************************************/
void BitArrayDecrement(bit_array_t *ba)
{
    int i;
    unsigned char maxValue;     /* maximum value for current char */
    unsigned char one;          /* least significant bit in current char */

    if (ba == NULL)
    {
        return;         /* nothing to decrement */
    }

    /* handle arrays that don't use every bit in the last character */
    i = (ba->numBits % CHAR_BIT);
    if (i != 0)
    {
        maxValue = UCHAR_MAX << (CHAR_BIT - i);
        one = 1 << (CHAR_BIT - i);
    }
    else
    {
        maxValue = UCHAR_MAX;
        one = 1;
    }

    for (i = BIT_CHAR(ba->numBits - 1); i >= 0; i--)
    {
        if (ba->array[i] >= one)
        {
            ba->array[i] = ba->array[i] - one;
            return;
        }
        else
        {
            /* need to borrow from the next byte */
            ba->array[i] = maxValue;

            /* remaining characters must use all bits */
            maxValue = UCHAR_MAX;
            one = 1;
        }
    }
}
Example #6
0
/***************************************************************************
*   Function   : BitArrayClearBit
*   Description: This function sets the specified bit to 0 in the bit array
*                passed as a parameter.
*   Parameters : ba - pointer to bit array
*                bit - bit to clear
*   Effects    : The specified bit in the bit array is set to 0.
*   Returned   : NONE
***************************************************************************/
void BitArrayClearBit(bit_array_t *ba, unsigned int bit)
{
    unsigned char mask;

    if (ba == NULL)
    {
        return;         /* no bit to clear */
    }

    if (ba->numBits <= bit)
    {
        return;         /* bit out of range */
    }

    /* create a mask to zero out desired bit */
    mask =  BIT_IN_CHAR(bit);
    mask = ~mask;

    ba->array[BIT_CHAR(bit)] &= mask;
}
Example #7
0
/***************************************************************************
*   Function   : BitArraySetAll
*   Description: This function sets every bit to 1 in the bit array passed
*                as a parameter.  This is function uses UCHAR_MAX, so it is
*                crucial that the machine implementation of unsigned char
*                utilizes all the bits in the memory allocated for an
*                unsigned char.
*   Parameters : ba - pointer to bit array
*   Effects    : Each of the bits used in the bit array are set to 1.
*                Unused (spare) bits are set to 0.
*   Returned   : NONE
***************************************************************************/
void BitArraySetAll(bit_array_t *ba)
{
    int bits;
    unsigned char mask;

    if (ba == NULL)
    {
        return;         /* nothing to set */
    }

    /* set bits in all bytes to 1 */
    memset((void *)(ba->array), UCHAR_MAX, BITS_TO_CHARS(ba->numBits));

    /* zero any spare bits so increment and decrement are consistent */
    bits = ba->numBits % CHAR_BIT;
    if (bits != 0)
    {
        mask = UCHAR_MAX << (CHAR_BIT - bits);
        ba->array[BIT_CHAR(ba->numBits - 1)] = mask;
    }
}
Example #8
0
int GetBitCount (BYTE *pTicketBet, WORD bits_in_universe)
{
    int  bits_enabled;
    int  bit;
    BOOL status;

    status = FALSE;
    bits_enabled = 0;

    // How many bits are enabled in an universe of universe_size elements?
    for ( bit = 0; bit < bits_in_universe; bit++ )
    {
        status = (pTicketBet[BIT_CHAR(bit)] & BIT_IN_CHAR(bit)) != 0;

        if ( status )
        {
            bits_enabled++;
        }
    }

    return bits_enabled;
}
Example #9
0
/***************************************************************************
*   Function   : BitArrayCompare
*   Description: This function compares two bit arrays.
*   Parameters : ba1 - pointer to bit array
*                ba2 - pointer to bit array
*   Effects    : None
*   Returned   : < 0 if ba1 < ba2 or ba1 is shorter than ba2
*                0 if ba1 == ba2
*                > 0 if ba1 > ba2 or ba1 is longer than ba2
***************************************************************************/
int BitArrayCompare(const bit_array_t *ba1, const bit_array_t *ba2)
{
    int i;

    if (ba1 == NULL)
    {
        if (ba2 == NULL)
        {
            return 0;                   /* both are NULL */
        }
        else
        {
            return -(ba2->numBits);     /* ba2 is the only Non-NULL*/
        }
    }

    if (ba2 == NULL)
    {
        return (ba1->numBits);          /* ba1 is the only Non-NULL*/
    }

    if (ba1->numBits != ba2->numBits)
    {
        /* arrays are different sizes */
        return(ba1->numBits - ba2->numBits);
    }

    for(i = 0; i <= BIT_CHAR(ba1->numBits - 1); i++)
    {
        if (ba1->array[i] != ba2->array[i])
        {
            /* found a non-matching unsigned char */
            return(ba1->array[i] - ba2->array[i]);
        }
    }

    return 0;
}
Example #10
0
//---------------------------------------------------------------------------
void kbitarray_set_bit(t_kbitarray *self, off_t bit_index) {
	self->bitarray[BIT_CHAR(bit_index)] |= BIT_IN_CHAR(bit_index);
}
Example #11
0
//---------------------------------------------------------------------------
bool kbitarray_test_bit(t_kbitarray *self, off_t bit_index) {
	return ((self->bitarray[BIT_CHAR(bit_index)] & BIT_IN_CHAR(bit_index)) != 0);
}
Example #12
0
/***************************************************************************
*   Function   : BitArrayShiftRight
*   Description: This function shifts the bits in a bit array to the right
*                by the amount of positions specified.
*   Parameters : ba - pointer to the bit array 0 is the msb of the first
*                     unsigned char in the bit array.
*                shifts - number of bits to shift by.
*   Effects    : The bit array data pointed to by ba is shifted to the
*                right.  New bits shifted in will be set to 0.
*   Returned   : None
***************************************************************************/
void BitArrayShiftRight(bit_array_t *ba, unsigned int shifts)
{
    int i, j;
    unsigned char mask;
    int chars = shifts / CHAR_BIT;  /* number of whole byte shifts */
    shifts = shifts % CHAR_BIT;     /* number of bit shifts remaining */

    if (ba == NULL)
    {
        return;         /* nothing to shift */
    }

    if (shifts >= ba->numBits)
    {
        /* all bits have been shifted off */
        BitArrayClearAll(ba);
        return;
    }

    /* first handle big jumps of bytes */
    if (chars > 0)
    {
        for (i = BIT_CHAR(ba->numBits - 1); (i - chars) >= 0; i--)
        {
            ba->array[i] = ba->array[i - chars];
        }

        /* now zero out new bytes on the right */
        for (; chars > 0; chars--)
        {
            ba->array[chars - 1] = 0;
        }
    }

    /* now we have at most CHAR_BIT - 1 bit shifts across the whole array */
    for (i = 0; i < shifts; i++)
    {
        for (j = BIT_CHAR(ba->numBits - 1); j > 0; j--)
        {
            ba->array[j] >>= 1;

            /* handle shifts across byte bounds */
            if (ba->array[j - 1] & 0x01)
            {
                ba->array[j] |= MS_BIT;
            }
        }

        ba->array[0] >>= 1;
    }

    /***********************************************************************
    * zero any spare bits that are beyond the end of the bit array so
    * increment and decrement are consistent.
    ***********************************************************************/
    i = ba->numBits % CHAR_BIT;
    if (i != 0)
    {
        mask = UCHAR_MAX << (CHAR_BIT - i);
        ba->array[BIT_CHAR(ba->numBits - 1)] &= mask;
    }
}
Example #13
0
/***************************************************************************
*   Function   : BitArrayTestBit
*   Description: This function tests the specified bit in the bit array
*                passed as a parameter.  A non-zero will be returned if the
*                tested bit is set.
*   Parameters : ba - pointer to bit array
*                bit - bit to test
*   Effects    : None
*   Returned   : Non-zero if bit is set, otherwise 0.  This function does
*                not check the input.  Tests on invalid input will produce
*                unknown results.
***************************************************************************/
int BitArrayTestBit(bit_array_t *ba, unsigned int bit)
{
    return((ba->array[BIT_CHAR(bit)] & BIT_IN_CHAR(bit)) != 0);
}