Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

timoog

macrumors newbie
Original poster
Jan 22, 2008
2
0
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!
 
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.
 
Code & Error detail

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

thanks for the quick response
 
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
 
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
 
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
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;
}
 
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

look out for first == second. It's a doozy. No more value for you.

-Lee
 
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?

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.
 
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?

Bill
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.
 
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. :)
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.