Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Binary math operations (and, or, etc.)



On Jun 8, 2005, at 09:19, Doug McNutt wrote:

In C, the [program]

  - 2^2 returns -4

while in AppleScript

  - 2^2 returns +4

Without worrying about AppleScript's problems previously discussed, why does the C function work at all?

Because unary minus has higher precedence than exclusive-or.

First, for those who don't already know, in C, "^" is the exclusive-or operator, not the "power" operator.

"2^2" on its own produces zero. However, the above calculation is "(-2) xor 2". Here's a C program that demonstrates. Put this code in a file named "xortest.c":

  #include <stdio.h>

  int main ()
  {
    printf("-2^2   = %d\n", -2^2  );
    printf("-(2^2) = %d\n", -(2^2));
    printf("(-2)^2 = %d\n", (-2)^2);
    printf("-2^3   = %d\n", -2^3);
  }

Then compile and run it:

% gcc xortest.c
% ./a.out
-2^2   = -4
-(2^2) = 0
(-2)^2 = -4
-2^3   = -3

The last line shows that it is only a deceptive coincidence that "-2^2 = -4" looks like "-(2 to the power of 2)". If you change either of the two numbers the result no longer looks confusingly like "^" is the "power" operator.

--
Chris Page - Software Wrangler - Dylan Pundit

 That’s “Chris” with a silent *and* invisible “3”.

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/applescript-users/email@hidden

This email sent to email@hidden
References: 
 >Binary math operations (and, or, etc.) (From: Stephen Jonke <email@hidden>)
 >Re: Binary math operations (and, or, etc.) (From: "Mark J. Reed" <email@hidden>)
 >Re: Binary math operations (and, or, etc.) (From: Stephen Jonke <email@hidden>)
 >Re: Binary math operations (and, or, etc.) (From: Doug McNutt <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.