3

I am trying to perform some bitshift operations and dealing with binary numbers in JavaScript.

Here's what I'm trying to do. A user inputs a value and I do the following with it:

// Square Input and mod with 65536 to keep it below that value
var squaredInput = (inputVal * inputVal) % 65536;
// Figure out how many bits is the squared input number
var bits = Math.floor(Math.log(squaredInput) / Math.log(2)) + 1;
// Convert that number to a 16-bit number using bitshift.
var squaredShifted = squaredInput >>> (16 - bits);

As long as the number is larger than 46, it works. Once it is less than 46, it does not work. I know the problem is the in bitshift. Now coming from a C background, I know this would be done differently, since all numbers will be stored in 32-bit format (given it is an int). Does JavaScript do the same (since it vars are not typed)?

If so, is it possible to store a 16-bit number? If not, can I treat it as 32-bits and do the required calculations to assume it is 16-bits?

Note: I am trying to extract the middle 4-bits of the 16-bit value in squaredInput.

Another note: When printing out the var, it just prints out the value without the padding so I couldn't figure it out. Tried using parseInt and toString.

Thanks

11
  • 1
    Try (squaredInput >> 16) & 0xffff Commented Apr 17, 2012 at 20:19
  • I was going to suggest a bitmask as well, I've found those useful. Commented Apr 17, 2012 at 20:20
  • 1
    In JavaScript, all number are 64bit-floats. Only for dealing with bitwise operators they will be converted to 32-bit signed integers. Commented Apr 17, 2012 at 20:20
  • @Pointy still produces the same problem. Anything under 46 does not work. Commented Apr 17, 2012 at 20:26
  • How can I print out squaredInput WITH the padding? So the whole stored number. Commented Apr 17, 2012 at 20:27

2 Answers 2

1

Are you looking for this?

function get16bitnumber( inputVal ){
   return ("0000000000000000"+(inputVal * inputVal).toString(2)).substr(-16);
}

This function returns last 16 bits of (inputVal*inputVal) value.By having binary string you could work with any range of bits.

Sign up to request clarification or add additional context in comments.

3 Comments

Why do you put 16 0's in the beginning?
for padding . get16bitnumber will always return 16-length binary string, with leading zeros
@Nayefc I have updated my answer, that might be useful for you.
0

Don't use bitshifting in JS if you don't absolutely have to. The specs mention at least four number formats

  • IEEE 754
  • Int32
  • UInt32
  • UInt16

It's really confusing to know which is used when.

For example, ~ applies a bitwise inversion while converting to Int32. UInt16 seems to be used only in String.fromCharCode. Using bitshift operators converts the operands to either UInt32 or to Int32.

In your case, the right shift operator >>> forces conversion to UInt32. When you type

a >>> b

this is what you get:

ToUInt32(a) >>> (ToUInt32(b) & 0x1f)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.