bit shift vs multiplication

Viewed 2876

I am trying convert a byte array to a number and for large numbers, I see that the bit shift is giving -ve results. Can one of you please why we see this issue? Do you see any drawbacks using "multiplication" instead of "bit-shift"?

For example,

<script language="JavaScript">
    var myVar = 1000000;
    document.write("Bit shift Result: " + (myVar << 8));
    document.write("<br>");
    document.write("Multiplication Result: " + parseInt(myVar *256));
</script>

Output:

Bit shift Result : 256000000

Multiplication Result: 256000000

Upon adding one more zero to myVar, you see the issue I am talking about

<script language="JavaScript">
    var myVar = 10000000;
    document.write("Bit shift Result: " + (myVar << 8));
    document.write("<br>");
    document.write("Multiplication Result: " + parseInt(myVar *256));
</script>

Output:
Bit shift Result: -1734967296

Multiplication Result: 2560000000

3 Answers
Related