• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag
 

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Binary math operations (and, or, etc.)
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


  • Subject: Re: Binary math operations (and, or, etc.)
  • From: Chris Page <email@hidden>
  • Date: Mon, 13 Jun 2005 12:55:26 -0700

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:
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>)

  • Prev by Date: Re: Printing a window
  • Next by Date: Bug with "missing value"
  • Previous by thread: Re: Binary math operations (and, or, etc.)
  • Next by thread: Re: Binary math operations (and, or, etc.)
  • Index(es):
    • Date
    • Thread