Simplest question on here? adding multiple ints

Discussion in 'Mac Programming' started by timoog, Apr 28, 2010.

  1. timoog macrumors newbie

    Joined:
    Jan 22, 2008
    #1
    This has got to be the simplest question on here but it's driving me nuts, I'm making slow progress with cocoa and objective c and Im making a simple timer I want to add the hours, minutes and seconds together to get the total number of seconds

    So I have 3 Ints, Hours, Min, Sec and want to do this:

    int totalSec = (((Hours x 60) x 60) + (min x 60) + sec);

    but keep getting errors no matter how I try to reformat it, is there no way to do this without splitting each sum somehow

    thanks in advance!
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    Assuming all the variables there are declared then that will work. Please post the exact error (copy and paste it). Without that we don't know what's going wrong.

    Basically anything you can do in C you can do in Objective-C: this is just C.
     
  3. miles01110 macrumors Core

    miles01110

    Joined:
    Jul 24, 2006
    Location:
    The Ivory Tower (I'm not coming down)
    #3
    Assuming in your code you have defined Hours, Min, Sec, you need to be aware that your identifiers are case sensitive. Also, * is the multiplication operator, not "x" ...

    Along with your error, post your code.
     
  4. timoog thread starter macrumors newbie

    Joined:
    Jan 22, 2008
    #4
    Code & Error detail

    Ha, thanks, Im a moron, of course it's * not X

    thanks for the quick response
     
  5. JoshDC macrumors regular

    Joined:
    Apr 8, 2009
    #5
    As the previous poster said, it's *, not x to multiply.
     
  6. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #6
    As was stated above, * is the binary multiplication operator. It does other things, too (unary dereference, etc.), but it is what you want here. You can't just paste some math and hope for the best. Something like ^ may be how you write exponentiation, but you will be in for a surprise if you try to use it in C.

    You should probably get a book, or at least review the wikipedia article on C operators and their precedence.

    -Lee
     
  7. Bill McEnaney macrumors 6502

    Joined:
    Apr 29, 2010
    #7
    In C, the "^ "means "bitwise exclusive or." Here's a way to use it:

    static void swap(int *first, int *second)
    {
    *first ^= *second;
    *second ^= *first;
    *first ^= *second;
    }

    Swaps 'em without a temporary variable. Pretty clever, eh?

    Hmm, I wonder how to make the server keep my indentation in my function.

    Bill
     
  8. jared_kipe macrumors 68030

    jared_kipe

    Joined:
    Dec 8, 2003
    Location:
    Seattle
    #8
    Wrap it in [ c o d e ] [ / c o d e ] blocks (without spaces)
    Code:
    static void swap(int *first, int *second)
    {
       *first ^= *second;
       *second ^= *first;
       *first ^= *second;
    }
    
     
  9. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #9
    look out for first == second. It's a doozy. No more value for you.

    -Lee
     
  10. gnasher729 macrumors P6

    gnasher729

    Joined:
    Nov 25, 2005
    #10
    No, actually. Swapping with a temporary variable is faster, easier to understand, less error prone, and it works with objects of any type. In C++, calling the swap() method is often _substantially_ faster than any other method and sometimes the only correct way to swap objects.

    In this case specifically: Exchanging *first and *second using a temporary variable takes two loads and two stores. Your code takes four loads, three xors, and three stores assuming a good compiler. And swap (&x, &x); will set x to zero with your method, which may come unexpected to the caller.
     
  11. jared_kipe macrumors 68030

    jared_kipe

    Joined:
    Dec 8, 2003
    Location:
    Seattle
    #11
    Not an error, it swaps your original numbers with zeroes.
     
  12. Bill McEnaney macrumors 6502

    Joined:
    Apr 29, 2010
    #12
    Thanks jared_kipe. I'll remember that.
     
  13. plinden macrumors 68040

    plinden

    Joined:
    Apr 8, 2004
    #13
    There's only one reason to remember this. This is a fairly common interview question ("Can you swap two integers without using a temporary variable?") and you can then use Gnasher's reasoning to show how clever you are by explaining why you wouldn't actually want do this.
     
  14. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #14
    Compression and encryption!
     
  15. Bill McEnaney macrumors 6502

    Joined:
    Apr 29, 2010
    #15
    Thank for the details, gnasher729. I know that slow code would be more readable with a temporary variable in it. It would be more useful then, too, because, as you say, it would work with data of any type. Since I almost obsess about writing ultra-readable programs, I wouldn't put that function into any program. I posted the function to give an example of what you can do with C's exclusive-or operator and to show that the function would work without a temporary variable. It does work. I've tried it.

    Besides, I usually write in Lisp, Prolog, and Python. So you won't need to read my entry for the next obfuscated C contest. :)
     

Share This Page