예제 #1
0
void sumBitArray(bitArray *r , const bitArray * a , const bitArray * b){
		
		int l = (a->length > b->length) ? a->length : b->length;
		
		bitArray * tmp = newBitArray(l);
		
		if(a->length > b->length)
			memcpy(tmp->array , a->array , sizeof(bit) * l);
		else
			memcpy(tmp->array , b->array , sizeof(bit) * l);
		
		int i , e = (a->length < b->length) ? a->length : b->length;
		
		for(i = 0; i < e; i++)
			tmp->array[i] = a->array[i] ^ b->array[i];

		copyBitArray(r , tmp);
		
		deleteBitArray(tmp);
}
예제 #2
0
int BitStream::read(uint8_t* bytes, const unsigned bitlen)
{
    uint8_t tmp[MaxBytesPerRW + 1];

    const unsigned bytelen = bitlenToBytelen(bitlen + (bit_offset_ % 8));
    UAVCAN_ASSERT(MaxBytesPerRW >= bytelen);

    const int read_res = buf_.read(bit_offset_ / 8, tmp, bytelen);
    if (read_res < 0)
    {
        return read_res;
    }
    if (static_cast<unsigned>(read_res) < bytelen)
    {
        return ResultOutOfBuffer;
    }

    fill(bytes, bytes + bitlenToBytelen(bitlen), uint8_t(0));
    copyBitArray(tmp, bit_offset_ % 8, bitlen, bytes, 0);
    bit_offset_ += bitlen;
    return ResultOk;
}
예제 #3
0
int BitStream::write(const uint8_t* bytes, const unsigned bitlen)
{
    // Temporary buffer is needed to merge new bits with cached unaligned bits from the last write() (see byte_cache_)
    uint8_t tmp[MaxBytesPerRW + 1];

    // Tmp space must be large enough to accomodate new bits AND unaligned bits from the last write()
    const unsigned bytelen = bitlenToBytelen(bitlen + (bit_offset_ % 8));
    UAVCAN_ASSERT(MaxBytesPerRW >= bytelen);
    tmp[0] = tmp[bytelen - 1] = 0;

    fill(tmp, tmp + bytelen, uint8_t(0));
    copyBitArray(bytes, 0, bitlen, tmp, bit_offset_ % 8);

    const unsigned new_bit_offset = bit_offset_ + bitlen;

    // Bitcopy algorithm resets skipped bits in the first byte. Restore them back.
    tmp[0] |= byte_cache_;

    // (new_bit_offset % 8 == 0) means that this write was perfectly aligned.
    byte_cache_ = uint8_t((new_bit_offset % 8) ? tmp[bytelen - 1] : 0);

    /*
     * Dump the data into the destination buffer.
     * Note that if this write was unaligned, last written byte in the buffer will be rewritten with updated value
     * within the next write() operation.
     */
    const int write_res = buf_.write(bit_offset_ / 8, tmp, bytelen);
    if (write_res < 0)
    {
        return write_res;
    }
    if (static_cast<unsigned>(write_res) < bytelen)
    {
        return ResultOutOfBuffer;
    }

    bit_offset_ = new_bit_offset;
    return ResultOk;
}
예제 #4
0
void mulBitArray(bitArray *r , const bitArray * a , const bitArray * b){
	
	int aG = getGradeBitArray(a);
	int bG = getGradeBitArray(b);

	int l = aG + bG + 1;	//length of the result
	
	bitArray * tmp = newBitArray(l);
		
	int i;
	for(i = 0; i <= aG; i++)	//for each bit of a
	{
		if(a->array[i] == 1)
		{
			int j , c = i;	//current bit of tmp
			for(j = 0; j <= bG; j++) //for each bit of b
				tmp->array[c++] ^= b->array[j];
		} 		
	}
	
	copyBitArray(r , tmp);	
	deleteBitArray(tmp);		
}