Thread: Can anybody spot where I'm going wrong with this?

  1. #1
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733

    Post Can anybody spot where I'm going wrong with this?

    Having trouble identifying why my results are coming out like this:
    Code:
    (vul1 << vul2) = ade68a80u                                                                                             
    
    (vuv1 << vuv2) = 865bcd15u                                                                                             
    
    (vul1 << vul3) = 79a2a000u                                                                                             
    
    (vuv1 << vuv3) = 75bcd15u                                                                                              
    (vul2 << vul3) = e000u                                                                                                 
    
    (vuv2 << vuv3) = 7u
    Here's the relevant code:
    Code:
    #define _zuvEndBit( BITS ) (1 << (((BITS)-1) % CHAR_BIT))
    // ...
    zuv_t* _zuvShl( zuv_t *num, zul_t bytes, zul_t bits ) {
        if ( !_zuvValidNum( num ) ) return NULL;
        // 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 );
        // Make sure we start with correct bytes
        --lps;
        while ( rps > 0 ) {
            --rps;
            for ( ; rbt & rbs > 0; --rbs, lbt >>= 1, rbt >>= 1 ) {
                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;
    }
    // ...
    int main( void ) {
        zu_t vul1 = 123456789, vuc1 = vul1;
        zu_t vul2 = 7, vuc2 = vul2;
        zu_t vul3 = 13, vuc3 = vul3;
        zuv_t vuv1 = zuvVal( &vuc1, sizeof(int), 0, NULL );
        zuv_t vuv2 = zuvVal( &vuc2, sizeof(int), 0, NULL );
        zuv_t vuv3 = zuvVal( &vuc3, sizeof(int), 0, NULL );
        //...
        val_print( Shl, u );
        //...
        return 0;
    }

  2. #2
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733
    Decided that hex just wasn't clear enough so I modified some macros val_print relies on to produce this:
    Code:
    (vul1 << vul2) = 10101101111001101000101010000000u                                                                       
    (vuv1 << vuv2) =      111010110111100110100010101u                                                                       
    (vuv1 << vuv2) = 10000110010110111100110100010101u                                                                       
    (vul1 << vul3) =  1111001101000101010000000000000u                                                                       
    (vuv1 << vuv3) =      111010110111100110100010101u                                                                       
    (vuv1 << vuv3) =      111010110111100110100010101u                                                                       
    (vul2 << vul3) =                 1110000000000000u                                                                       
    (vuv2 << vuv3) =                              111u                                                                       
    (vuv2 << vuv3) =                              111u
    Edit: You'll notice that each set now has 2 pairs of the vuv* values, the 1st is the pre-shift state the 2nd is the post-shift state
    Last edited by awsdert; 01-07-2018 at 07:55 AM.

  3. #3
    Registered User awsdert's Avatar
    Join Date
    Jan 2015
    Posts
    1,733
    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,
    Code:
    rbt & rbs >0
    should've been
    Code:
    rbt && rbs > 0
    Last edited by awsdert; 01-07-2018 at 08:45 AM.

  4. #4
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    I liked your acting in The Critter Chronicles

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Who can spot what I did wrong?
    By jturner38 in forum C Programming
    Replies: 13
    Last Post: 03-25-2009, 01:09 AM
  2. spot the error
    By razza in forum C++ Programming
    Replies: 5
    Last Post: 08-01-2002, 09:21 AM

Tags for this Thread