Post Reply 
Summation on HP 42S
09-30-2018, 05:28 PM (This post was last modified: 09-30-2018 05:34 PM by ijabbott.)
Post: #52
RE: Summation on HP 42S
(09-30-2018 03:03 PM)Albert Chan Wrote:  
(09-29-2018 09:10 PM)ijabbott Wrote:  Curiously, replacing `i += i;` with `i <<= 1` in the code ... it just returns the value 31.

Perhaps << operator assumed unsigned integer (bits operation) ?
To confirm, this unsigned version also work: i += (unsigned) i

I've no idea why the i <<= 1; version works. By the rules of C, it would be at liberty to turn it into an infinite loop as it did for i += i; (although that is not a terribly useful thing to do!).

The i += (unsigned)i case works because there is no undefined behaviour. The result of the addition will be of type unsigned int and that needs to be converted back to a signed int. If the value does not fit (i.e. it's greater than INT_MAX) then the conversion is implementation-defined or an implementation-defined signal is raised. In the case of GCC, it will be defined as mapping onto a negative value by 2's complement. The key point is that it isn't undefined behaviour and that GCC will define (unsigned int)0x80000000 as being converted to (int)-0x80000000, and so the loop will terminate after 31 iterations (in the abstract machine). (In practice, the optimiser can get rid of the loop by working out the result at compile time.)

[EDIT - removed unfinished bits of reply I didn't mean to post!]

[EDIT - it's almost as if I'm having a "Senior" moment! (Post count: 299)]

— Ian Abbott
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
Summation on HP 42S - lrdheat - 09-23-2018, 06:14 PM
RE: Summation on HP 42S - Didier Lachieze - 09-23-2018, 06:28 PM
RE: Summation on HP 42S - lrdheat - 09-23-2018, 06:41 PM
RE: Summation on HP 42S - lrdheat - 09-23-2018, 06:51 PM
RE: Summation on HP 42S - ijabbott - 09-23-2018, 07:40 PM
RE: Summation on HP 42S - burkhard - 09-25-2018, 12:58 PM
RE: Summation on HP 42S - John Keith - 09-25-2018, 04:15 PM
RE: Summation on HP 42S - Thomas Klemm - 09-23-2018, 08:29 PM
RE: Summation on HP 42S - lrdheat - 09-23-2018, 09:25 PM
RE: Summation on HP 42S - Didier Lachieze - 09-23-2018, 10:03 PM
RE: Summation on HP 42S - lrdheat - 09-23-2018, 10:02 PM
RE: Summation on HP 42S - lrdheat - 09-23-2018, 10:37 PM
RE: Summation on HP 42S - Thomas Klemm - 09-24-2018, 12:56 AM
RE: Summation on HP 42S - Albert Chan - 09-24-2018, 11:56 AM
RE: Summation on HP 42S - Thomas Klemm - 09-24-2018, 01:17 AM
RE: Summation on HP 42S - Thomas Klemm - 09-24-2018, 01:52 PM
RE: Summation on HP 42S - Albert Chan - 09-24-2018, 04:20 PM
RE: Summation on HP 42S - pier4r - 09-24-2018, 01:55 PM
RE: Summation on HP 42S - Frido Bohn - 09-25-2018, 01:59 PM
RE: Summation on HP 42S - Dieter - 09-25-2018, 04:47 PM
RE: Summation on HP 42S - Frido Bohn - 09-26-2018, 10:10 AM
RE: Summation on HP 42S - Albert Chan - 09-26-2018, 03:13 PM
RE: Summation on HP 42S - Albert Chan - 09-25-2018, 05:28 PM
RE: Summation on HP 42S - lrdheat - 09-25-2018, 05:17 PM
RE: Summation on HP 42S - lrdheat - 09-25-2018, 05:28 PM
RE: Summation on HP 42S - Albert Chan - 09-25-2018, 07:17 PM
RE: Summation on HP 42S - Valentin Albillo - 09-25-2018, 08:01 PM
RE: Summation on HP 42S - Albert Chan - 09-25-2018, 08:59 PM
RE: Summation on HP 42S - Albert Chan - 09-26-2018, 01:23 PM
RE: Summation on HP 42S - pier4r - 09-26-2018, 04:09 PM
RE: Summation on HP 42S - Thomas Klemm - 09-27-2018, 03:47 AM
RE: Summation on HP 42S - Ángel Martin - 09-27-2018, 05:27 AM
RE: Summation on HP 42S - pier4r - 09-27-2018, 10:37 PM
RE: Summation on HP 42S - Thomas Okken - 09-28-2018, 12:33 AM
RE: Summation on HP 42S - pier4r - 09-28-2018, 01:40 PM
RE: Summation on HP 42S - ijabbott - 09-28-2018, 11:20 PM
RE: Summation on HP 42S - Albert Chan - 09-29-2018, 12:51 AM
RE: Summation on HP 42S - Valentin Albillo - 09-29-2018, 01:15 AM
RE: Summation on HP 42S - Thomas Okken - 09-29-2018, 01:24 AM
RE: Summation on HP 42S - ijabbott - 09-29-2018, 11:58 AM
RE: Summation on HP 42S - Albert Chan - 09-29-2018, 12:49 PM
RE: Summation on HP 42S - Thomas Okken - 09-29-2018, 01:50 PM
RE: Summation on HP 42S - ijabbott - 09-29-2018, 04:06 PM
RE: Summation on HP 42S - Thomas Okken - 09-29-2018, 06:15 PM
RE: Summation on HP 42S - ijabbott - 09-29-2018, 09:10 PM
RE: Summation on HP 42S - Thomas Okken - 09-29-2018, 09:33 PM
RE: Summation on HP 42S - Albert Chan - 09-30-2018, 03:03 PM
RE: Summation on HP 42S - ijabbott - 09-30-2018 05:28 PM
RE: Summation on HP 42S - brickviking - 10-01-2018, 08:40 AM
RE: Summation on HP 42S - pier4r - 09-29-2018, 05:59 PM
RE: Summation on HP 42S - Thomas Okken - 09-29-2018, 08:59 PM



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