After some major modifications to my code:
Code:
#ifdef DBG_BIT
#include <stdio.h>
#define DBG_BIT_POS
#endif
zuv_t* _zuvShl( zuv_t *num, zul_t bytes, zul_t bits ) {
if ( !_zuvValidNum( num ) ) return NULL;
#ifdef DBG_BIT
printf("\n_vuvShl(): Moving %lu of %u bytes", bytes, num->size );
printf("\n_vuvShl(): Moving %lu of %lu bits", bits, num->bits );
#endif
// Skip oversized moving
if ( bits > num->bits || bytes > num->size ) {
memset( num->buff, 0, num->size );
return num;
}
// We don't need lbs so skip it
zul_t rbs = num->bits - bits;
zus_t lps = num->size, rps = lps - bytes;
zuc_t lbt = _zuvEndBit( num->bits ), rbt = _zuvEndBit( rbs );
#ifdef DBG_BIT
printf("\n_vuvShl(): rbs = %lu", rbs);
printf("\n_vuvShl(): lps = %u; rps = %u", lps, rps);
printf("\n_vuvShl(): lbt = %02x; rbt = %02x", lbt, rbt);
#endif
// Make sure we start with correct bytes
--lps;
while ( rps > 0 ) {
--rps;
#ifdef DBG_BIT_POS
printf("\n_vuvShl(): while()");
printf("\n_vuvShl(): rbs = %lu", rbs);
printf("\n_vuvShl(): lps = %u; rps = %u", lps, rps);
printf("\n_vuvShl(): lbt = %02x; rbt = %02x", lbt, rbt);
#endif
for ( ; rbt & rbs > 0; --rbs, lbt >>= 1, rbt >>= 1 ) {
#ifdef DBG_BIT_POS
printf("\n_vuvShl(): for()");
printf("\n_vuvShl(): rbs = %lu", rbs);
printf("\n_vuvShl(): lps = %u; rps = %u", lps, rps);
printf("\n_vuvShl(): lbt = %02x; rbt = %02x", lbt, rbt);
#endif
if ( !lbt ) {
lbt = SCHAR_MIN;
// This will never be lower than rps so don't check it
--lps;
}
// Set the left bit
if ( num->buff[rps] & rbt ) {
num->buff[lps] |= lbt;
// We're moving bits left so make sure to clear as we go right
num->buff[rps] ^= rbt;
}
// Clear left bit
else if ( num->buff[lps] & lbt )
num->buff[lps] ^= lbt;
}
// Make sure we're not retrieving 0
rbt = SCHAR_MIN;
}
return num;
}
To produce this:
Code:
...
(vul2 << vul3) = 1110000000000000u
(vuv2 << vuv3) = 111u
vuvShl(): Valid Numbers!
vuvShl(): Moving 13 bits
_vuvShl(): Moving 1 of 4 bytes
_vuvShl(): Moving 5 of 32 bits
_vuvShl(): rbs = 27
_vuvShl(): lps = 4; rps = 3
_vuvShl(): lbt = 80; rbt = 04
_vuvShl(): while()
_vuvShl(): rbs = 27
_vuvShl(): lps = 3; rps = 2
_vuvShl(): lbt = 80; rbt = 04
_vuvShl(): while()
_vuvShl(): rbs = 27
_vuvShl(): lps = 3; rps = 1
_vuvShl(): lbt = 80; rbt = 80
_vuvShl(): while()
_vuvShl(): rbs = 27
_vuvShl(): lps = 3; rps = 0
_vuvShl(): lbt = 80; rbt = 80
(vuv2 << vuv3) = 111u
I narrowed down the problem area to my for loop, I am somehow not entering that loop in the first place (BTW I am using GDB online Debugger | Compiler - Code, Compile, Run, Debug online C, C++ to write and test this code)
Edit: Never mind I just noticed the reason, should've been