Skip to content

Integer Overflow (?) on implementation pp. 127, in section 5.3.3 Hash Codes for Arrays and Strings, C++ edition #99

@EigenVecta

Description

@EigenVecta

I suspect the line

long p = (1L<<32)-5; // prime: 2ˆ32 - 5

causes overflow. long on my machine is 32-bit (printf("%ld\n", LONG_MAX); prints 2147483647), and compiler would give a warning -Wshift-count-overflow on that line. Indeed long type cannot hold that prime number.

I suspect it could be corrected as

unsigned long p = (1L<<32) - 5;

with printf("%lu\n", p); prints 4294967291, which is desired.

However the -Wshift-count-overflow warning: left shift count >= width of type still persists.

With

unsigned long p = (~0L) - 4;

(*)
the warning vanishes.

In summary, I think that line should be (*).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions