I am trying to do some bit-swapping operations and handle binary numbers in JavaScript.
Here is what I am trying to do. The user enters a value, and I do the following with him:
var squaredInput = (inputVal * inputVal) % 65536;
var bits = Math.floor(Math.log(squaredInput) / Math.log(2)) + 1;
var squaredShifted = squaredInput >>> (16 - bits);
As long as the number is more than 46, it works. When it is less than 46, it does not work. I know the problem is bitrate. Now, based on the C-background, I know that this will be done differently, since all numbers will be stored in 32-bit format (provided that it is int). Does JavaScript do the same (since it vardoesn't type)?
If so, is it possible to store a 16-bit number? If not, can I consider it as 32-bit and perform the required calculations, assuming it is 16-bit?
Note. I am trying to extract the middle 4-bit of a 16-bit value in squaredInput.
Another note: when printing var, it just prints the value without filling, so I can not figure it out. Tried to use parseIntand toString.
thank
source
share