Post Reply 
negative number raised to even power
05-05-2015, 06:12 PM (This post was last modified: 05-05-2015 06:24 PM by John Colvin.)
Post: #46
RE: negative number raised to even power
(05-05-2015 05:17 AM)debrouxl Wrote:  In C/C++, ^ is exclusive or (xor). In C (but not in C++), from C99 onwards, when using the alternative spellings iso646.h header, xor is expanded to ^.

With 2's complement representation of negative numbers:
* the binary representation of -1 is the pattern of 1 (0b11...111 in GCC and Clang for years, C++14 made the 0b syntax standard);
* the binary representation of -2 is 0b11...110;
* the binary representation of 2 is 0b00...010;
* the binary representation of (-2)^2 is therefore 0b11...100, i.e. -4.
That explains it. I knew ^ meant xor in the context of boolean operations, but that symbol is used so frequently to represent exponentiation, I just forgot about that quirk C. My bad.
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
negative number raised to even power - DrD - 04-25-2015, 11:24 AM
RE: negative number raised to even power - John Colvin - 05-05-2015 06:12 PM



User(s) browsing this thread: 20 Guest(s)