Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
EigenVecta opened this issue Jun 17, 2022 · 0 comments

Comments

@EigenVecta
Copy link

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 (*).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant