Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9055613
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T13:59:52+00:00 2026-06-16T13:59:52+00:00

int hash (const string &key, int tableSize) { int hashVal = 0; for (int

  • 0
int hash (const string &key, int tableSize) {
   int hashVal = 0; 

   for (int i = 0; i < key.length(); i++)
        hashVal = 37*hashVal + key[i]; 
   hashVal %= tableSize; 
   if (hashVal < 0)   /* in case overflows occurs */
        hashVal += tableSize; 

   return hashVal;      
};

Why do we control if hashVal is smaller than zero? How is this possible?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T13:59:53+00:00Added an answer on June 16, 2026 at 1:59 pm

    If the string is long enough, the code:

    for (int i = 0; i < key.length(); i++)
        hashVal = 37*hashVal + key[i]; 
    

    might cause the value of hashVal to exceed the maximum value of an int (typically something like 231 − 1) and become negative. This is known as integer overflow.

    The C++ standard does not specify whether the value of the % operator for negative operands should be positive or negative; thus, depending on your compiler and CPU architecture (and possibly compile-time switches), an expression like -47 % 37 may evaluate to either -10 or 27. Thus, the code you’ve quoted guards against the former possibility by adding the modulus to the result if it’s negative.

    By the way, an easier way to avoid this issue would have been to define hashVal as unsigned.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following class class hash_key { public: int get_hash_value(std::string &inStr, int inSize) const
Can someone please explain to me the static HashMap#hash(int) method? What's the justification behind
int a = 10; int* pA = &a; long long b = 200; long
I have: const unsigned int hash_1 = 0xaf019b0c; const unsigned int hash_2 = 0xf864e55c;
I want to hash a char array in to an int or a long.
I have following code: #include <cstring> #include <boost/functional/hash.hpp> #include <iostream> int main(int argc, char
I'm building a custom hash where I sum all the letters in string according
int main(void) { char testStr[50] = Hello, world!; char revS[50] = testStr; } I
int* p_bob = new int; *p_bob = 78; The above code makes sense to
int x; if (Q()) x = 123; if (R()) Console.WriteLine(x); // illegal int x;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.