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

  • Home
  • SEARCH
  • 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 4575190
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T20:01:42+00:00 2026-05-21T20:01:42+00:00

I have the following piece of code: public void AddHash( int val ) {

  • 0

I have the following piece of code:

    public void AddHash( int val )
    {
        m_Hash ^= (val & 0x3FFFFFF);
        m_Hash ^= (val >> 26) & 0x3F;
    }

I would like very much to know what the heck it does, but I would be satisfied to know how to build a bool HasHash( int val ) that tells me whether m_Hash has that number or not…

something like this?

    public bool HasHash( int val )
    {
        return (m_Hash & val) == 0;
    }
  • 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-05-21T20:01:43+00:00Added an answer on May 21, 2026 at 8:01 pm

    EDIT to fix the bug that @schnaader found: What it does? This code probably wants to rotate val leftwards (clockwise) by 6 bits and form the ones-complement sum (edit: not the product, as I’d said before) — the xor — of that rotated value and the current value of m_Hash to yield a new m_Hash. That new m_Hash will be used the next time AddHash( ) is called.

    However, the code as written has a bug: it rotates only the high-order 6 bits of val leftwards, leaving in place the low-order 26 bits of val. The code then xors together three values:

    1. the new low-order (old high-order) 6 bits of val;
    2. the original, unshifted low-order 26 bits of val; and
    3. the current value of m_Hash

    leaving the result in m_Hash.

    How does it do it? You can just map it out and emulate it:

    1. val & 0x3FFFFFF means extract the low-order 26 bits of val.
    2. xor those 26 bits with the current value of m_Hash

    3. now shift val rightwards such that the low-order 26 bits drop off the low-order end, leaving what used to be the high-order 6 bits of val in the low-order 6 bits of val.

    4. Mask with 0x3f to extract only those low-order 6 bits (in case some extraneous bits got shifted into the high order part of val).
    5. xor those low-order 6 bits with the current value of m_Hash to give the new m_Hash.

    You know that rotating and exclusive-oring are common operations in calculating a hash.

    EDIT: @schnaader pointed out the bug in the original code: that code forgot to do the other leg of the rotate: shifting the low-order 26 bits left by 6. To fix that, the code should read something like:

    public void AddHash( int val )
    {
      m_Hash ^= ((val & 0x3FFFFFF) << 6);
      m_Hash ^= (val >> 26) & 0x3F;
    }
    

    As to your HasHash( ) function: you should know that saying

    return (m_Hash & val) == 0;

    will return TRUE under many conditions, including some you might not want. For example, the function will return TRUE if m_Hash == 0xC0 and val == 0x03.

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

Sidebar

Related Questions

I have the following piece of code that I would like help with... public
Within one Activity I have th following piece of code: public void onStartMonitoringToggleClicked(View v)
I have a piece of code like follows public class Test{ public static void
I have the following piece of code: class ICookable { public: virtual void CookMe
I have following piece of code: public void ProcessRequest (HttpContext context) { context.Response.ContentType =
I have the following piece of code: class Student { public: Student(){} void display()
Suppose I have the following piece of code public synchronized void method() { if(something
I have a piece of code like the following: public class ActivityHelper { public
I have the following piece of code. I would like to know what sort
I have the following piece of code public static void main(String[] args) { ArrayList<Integer>

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.