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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T20:04:38+00:00 2026-05-15T20:04:38+00:00

var1=anyInteger var2=anyInteger (Math.round(var1/var2)*var2) What would be the syntax for JavaScripts bitshift alternative for the

  • 0
var1=anyInteger
var2=anyInteger

(Math.round(var1/var2)*var2)

What would be the syntax for JavaScripts bitshift alternative for the above?

Using integer not floating

Thank you

  • 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-15T20:04:39+00:00Added an answer on May 15, 2026 at 8:04 pm

    [UPDATED]
    The quick answer:

    var intResult = ((((var1 / var2) + 0.5) << 1) >> 1) * var2;

    It’s faster than the Math.round() method provided in the question and provides the exact same values.

    Bit-shifting is between 10 and 20% faster from my tests. Below is some updated code that compares the two methods.

    The code below has four parts: first, it creates 10,000 sets of two random integers; second, it does the round in the OP’s question, stores the value for later comparison and logs the total time of execution; third, it does an equivalent bit-shift, stored the value for later comparison, and logs the execution time; fourth, it compares the Round and Bit-shift values to find any differences. It should report no anomalies.

    Note that this should work for all positive, non-zero values. If the code encounters a zero for the denominator, it will raise and error, and I’m pretty sure that negative values will not bit-shift correctly, though I’ve not tested.

    var arr1 = [],
        arr2 = [],
        arrFloorValues = [],
        arrShiftValues = [],
        intFloorTime = 0,
        intShiftTime = 0,
        mathround = Math.round, // @trinithis's excellent suggestion
        i;
    
    // Step one: create random values to compare
    for (i = 0; i < 100000; i++) {
        arr1.push(Math.round(Math.random() * 1000) + 1);
        arr2.push(Math.round(Math.random() * 1000) + 1);
    }
    
    // Step two: test speed of Math.round()
    var intStartTime = new Date().getTime();
    for (i = 0; i < arr1.length; i++) {
        arrFloorValues.push(mathround(arr1[i] / arr2[i]) * arr2[i]);
    }
    console.log("Math.floor(): " + (new Date().getTime() - intStartTime));
    
    // Step three: test speed of bit shift
    var intStartTime = new Date().getTime();
    for (i = 0; i < arr1.length; i++) {
        arrShiftValues.push( ( ( ( (arr1[i] / arr2[i]) + 0.5) << 1 ) >> 1 ) * arr2[i]);
    
    }
    console.log("Shifting: " + (new Date().getTime() - intStartTime));
    
    // Step four: confirm that Math.round() and bit-shift produce same values
    intMaxAsserts = 100;
    for (i = 0; i < arr1.length; i++) {
        if (arrShiftValues[i] !== arrFloorValues[i]) {
            console.log("failed on",arr1[i],arr2[i],arrFloorValues[i],arrShiftValues[i])
            if (intMaxAsserts-- < 0) break;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 513k
  • Answers 513k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can do: $input = 'dd-mm-yy'; $a = explode('-',$input); $result… May 16, 2026 at 6:04 pm
  • Editorial Team
    Editorial Team added an answer Well, That is funny that no one replied on this… May 16, 2026 at 6:04 pm
  • Editorial Team
    Editorial Team added an answer Make use of jQuery Grid Plugin May 16, 2026 at 6:04 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

var1 = 'abc' var2 = 'xyz' print('literal' + var1 + var2) # literalabcxyz print('literal',
var var1 = 1, var2 = 1, var3 = 1; This is equivalent to
When debugging the following code NSString *var1 = @blaa; NSString *var2 = @blaaaaa; NSString*
Say, I have var1 and var2 both with a string value. Var2 is a
>> example: make object! [ [ var1: 10 [ var2: var1 + 10 [
Consider the following Lua code: local var1, var2; Is var2 a local variable here?
Though .htaccess, I want to redirect /page/var1/var2 to ./page.php?var1=var1&var2=var2. This is very easy BUT
SELECT var1,var2,var3,table_name FROM table1 LEFT JOIN table_name on var3=table_name.id Meaning I want to dynamically
i have two variables in a SSIS package Var1 and Var2. Both of these
<input type=checkbox id=var1 name=s_k > <input type=checkbox id=var2 name=s_k> <input type=checkbox id=var3 name=s_k> At

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.