Thread: return(result>>1)

  1. #1
    Registered User
    Join Date
    Jun 2016
    Posts
    2

    return(result>>1)

    Hi guys, i have a big dubt, i have been doing project for one of my subjects on university and i cant understand why i have to have this return, return(result>>1) for it to get the return rigth

    this is the function where i use it:
    Code:
    uint16_t getPulseWidth1()
    {
    
    
        uint16_t i,resultado1;
    
    
        for(i=0;i<60000;i++)
        {
            if(!(PIN_sd & (1<<ECHO1)))
            continue;
            else
            break;
        }
    
    
        if(i==60000)
        return Time_out_sd1;
    
    
    
    
        TCCR1A=0x00;
        TCCR1B=(1<<CS10);
        TCNT1=0x00;
        
        
        for(i=0;i<60000;i++)
        {
            if(PIN_sd & (1<<ECHO1))
            {
                if(TCNT1>60000) break; else continue;
            }
            else
            break;
        }
    
    
        if(i==1900)
        return sem_obst_sd1;
    
    
        resultado1=TCNT1;
    
    
        TCCR1B=0x00;
    
    
        if(resultado1>1900)
        return sem_obst_sd1;
        else
        return (resultado1>>1);
    }
    if any one could explain it to me, thaks a lot, sry for the bad ing :/

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I have no idea what this is function is for in the first place, but I can tell you that a right shift by 1 is equivalent to division by 2, so obviously without that you have a bug that requires you to divide by 2 to fix it.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2016
    Posts
    2
    this code is for the HC-SR004 ultrasond sensor, this function returns a number
    then
    (number*2)/58 to get the distence in cm but the original formula is number/58, so thats why i must multiply for 2 thaks a lot!
    now i wil try to fix the bug!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 09-24-2013, 10:33 AM
  2. It doesn't return a correct result...
    By pantera in forum C++ Programming
    Replies: 5
    Last Post: 01-16-2010, 05:23 PM
  3. How do I return a temporary result object?
    By matsp in forum C++ Programming
    Replies: 4
    Last Post: 07-24-2007, 04:41 PM
  4. Return value: Error or result?
    By Cactus_Hugger in forum C Programming
    Replies: 3
    Last Post: 05-03-2007, 12:23 AM
  5. Pass values to C++ dll and return result
    By joeyzt in forum C++ Programming
    Replies: 2
    Last Post: 06-21-2004, 11:26 PM

Tags for this Thread