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 9241491
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:22:51+00:00 2026-06-18T08:22:51+00:00

I am creating a bitmask in javascript. It works fine for bit 0 through

  • 0

I am creating a bitmask in javascript. It works fine for bit 0 through 14. When I set only bit fifteen to 1. It yields the integer value of “-2147483648” instead of “2147483648“. I can do a special case hack here by returning hardcoded “2147483648” for bit fifteen but I would like to know the correct way of doing it.

Sample code:

function join_bitmap(hex_lower_word, hex_upper_word)
{
    var lower_word = parseInt(hex_lower_word, 16);
    var upper_word = parseInt(hex_upper_word, 16);
    return (0x00000000ffffffff & ((upper_word<<16) | lower_word));
}

Above code returns -2147483648 when hex_lower_word is “0x0” and hex_upper_word is “0x8000” instead of 2147483648

  • 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-18T08:22:52+00:00Added an answer on June 18, 2026 at 8:22 am

    As previous answers explained, the bitwise operators are 32 bit signed. Thus, if at any point along the way you set bit 31, things will go badly wrong.

    In your code, the expression

    (upper_word<<16) | lower_word)
    

    is evaluated first because of the parentheses, and since upper_word has the top bit set, you will now have a negative number (0x80000000 = -2147483648)

    The solution is to make sure that you do not shift a 1into bit 31 – so you have to set bit 15 of the upper word to zero before shifting:

    mask15 = 0x7fff;
    ((upper_word&mask15)<<16|lower_word)
    

    This will take care of “numbers that are too big become negative”, but it won’t solve the problem completely – it will just give the wrong answer! To get back to the right answer, you need to set bit 31 in the answer, iff bit 15 was set in upper_word:

    bit15 = 0x8000;
    bit31 = 0x80000000;
    answer = answer + (upper_word & bit15)?bit31:0; 
    

    The rewritten function then becomes:

    function join_bitmap(hex_lower_word, hex_upper_word)
        {
            var lower_word = parseInt(hex_lower_word, 16);
            var upper_word = parseInt(hex_upper_word, 16);
            var mask15 = 0x7fff;
            var bit15 = 0x8000;
            var bit31 = 0x80000000;
            return 0xffffffff & (((upper_word&mask15)<<16) | lower_word) + ((upper_word & bit15)?bit31:0);
        }
    

    There isn’t just a single “hard coded special case” – there are 2 billion or so. This takes care of all of them.

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

Sidebar

Related Questions

Creating a class with variables like this works fine : class Example { public
(creating a separate question after comments on this: Javascript redeclared global variable overrides old
Creating a widget for my site. Check it out at http://bit.ly/w42SC4 . Looks good
Creating a new post in wordpress, I try to set a featured image. The
creating users through mysql admin, but unable to login mysql command line following on-line
I am somewhat curious about creating a macro to generate a bit mask for
Creating a simple public/private keypair, such as through ssh-keygen does not create a file
Creating a document fragment using plain Javascript is dead simple: var docFragment = document.createDocumentFragment();
Creating the permutations of a list or set is simple enough. I need to
Creating an item(Under the key) is easy,but how to add subitems(Value)? listView1.Columns.Add(Key); listView1.Columns.Add(Value); listView1.Items.Add(sdasdasdasd);

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.