简体   繁体   中英

Why does bitwise left shift from numpy give different results on different systems?

I use bitwise operations from numpy on two different system and get different / inconsistent results. After a bit of investigation I found out that left_shift causes the problem. If I do the following:

xs = [i for i in range(100)]
np.left_shift(xs, xs)

On the first system I get:

array([                   0,                    2,                    8,
                         24,                   64,                  160,
                        384,                  896,                 2048,
                       4608,                10240,                22528,
                      49152,               106496,               229376,
                     491520,              1048576,              2228224,
                    4718592,              9961472,             20971520,
                   44040192,             92274688,            192937984,
                  402653184,            838860800,           1744830464,
                 3623878656,           7516192768,          15569256448,
                32212254720,          66571993088,         137438953472,
               283467841536,         584115552256,        1202590842880,
              2473901162496,        5085241278464,       10445360463872,
             21440476741632,       43980465111040,       90159953477632,
            184717953466368,      378231999954944,      774056185954304,
           1583296743997440,     3236962232172544,     6614661952700416,
          13510798882111488,    27584547717644288,    56294995342131200,
         114841790497947648,   234187180623265792,   477381560501272576,
         972777519512027136,  1981583836043018240,  4035225266123964416,
        8214565720323784704, -1729382256910270464, -2882303761517117440,
       -4611686018427387904, -6917529027641081856, -9223372036854775808,
       -9223372036854775808,                   64,                  130,
                        264,                  536,                 1088,
                       2208,                 4480,                 9088,
                      18432,                37376,                75776,
                     153600,               311296,               630784,
                    1277952,              2588672,              5242880,
                   10616832,             21495808,             43515904,
                   88080384,            178257920,            360710144,
                  729808896,           1476395008,           2986344448,
                 6039797760,          12213813248,          24696061952,
                49928994816,         100931731456,         204010946560,
               412316860416,         833223655424,        1683627180032,
              3401614098432])

And on the second system I get:

array([                   0,                    2,                    8,
                         24,                   64,                  160,
                        384,                  896,                 2048,
                       4608,                10240,                22528,
                      49152,               106496,               229376,
                     491520,              1048576,              2228224,
                    4718592,              9961472,             20971520,
                   44040192,             92274688,            192937984,
                  402653184,            838860800,           1744830464,
                 3623878656,           7516192768,          15569256448,
                32212254720,          66571993088,         137438953472,
               283467841536,         584115552256,        1202590842880,
              2473901162496,        5085241278464,       10445360463872,
             21440476741632,       43980465111040,       90159953477632,
            184717953466368,      378231999954944,      774056185954304,
           1583296743997440,     3236962232172544,     6614661952700416,
          13510798882111488,    27584547717644288,    56294995342131200,
         114841790497947648,   234187180623265792,   477381560501272576,
         972777519512027136,  1981583836043018240,  4035225266123964416,
        8214565720323784704, -1729382256910270464, -2882303761517117440,
       -4611686018427387904, -6917529027641081856, -9223372036854775808,
       -9223372036854775808,                    0,                    0,
                          0,                    0,                    0,
                          0,                    0,                    0,
                          0,                    0,                    0,
                          0,                    0,                    0,
                          0,                    0,                    0,
                          0,                    0,                    0,
                          0,                    0,                    0,
                          0,                    0,                    0,
                          0,                    0,                    0,
                          0,                    0,                    0,
                          0,                    0,                    0,
                          0])

So, as you can see, up to a certain points the results become inconsistent. Does any one know what could cause it?

ADDED

By "different systems" I mean different computers. The first computer has numpy 1.15.1 and the second one 1.15.2.

Old versions of NumPy don't define what shift operators do if you try to shift by an amount >= the width of a type. They basically just delegate the semantics to the C shift operators, for which this is undefined behavior . Depending on hardware and compiler details, pretty much anything could happen (theoretically up to and including crashes, security issues, and arbitrary other misbehavior).

This got changed in 1.18, but your NumPy versions are older than that.

If you want defined behavior, don't try to do shifts like that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM