0

I am trying to bit-shift a bigint like so:

let foo = BigInt(420) << 32;

but I am getting the JavaScript Error: Operator '<<' cannot be applied to types 'bigint' and 'number'.

How do I bit-shift bigints in JavaScript?

1
  • 2
    Try time_now << 16n Commented Oct 4, 2023 at 16:52

1 Answer 1

6

As James have suggested as a comment and per the MDN documentation of the left shift operator (<<), both sides of the operation must be the same, either both are numbers or both are BigInts. So you should add a n at the end of the number literal (eg. 16n and 32n) to declare them as BigInts or explicitily create them as such.

Don't know if this is really your problem since the exception thrown is different compared to when I tried on the console, but I hope it helps.

Also, do not mark this as answered if it helped since I'd feel like stealing credit from James hahaha. I'm just trying to expand a little on the answer.

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

1 Comment

I think you explained it well!

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.