Skip to the editor
JSGroundwork
JSGroundwork handwritten · web dev
←Back to Bit manipulation

Ready to read

JSJavaScript⑂Git◎Interview prepΣDSA in JSSDSystem Design
More topics15›
</>HTML{ }CSS⚛ReactNNext.jsNeNest.jsTSTypeScriptNoNode.js🐳DockerDBSQL & Databases✓Testing🔒Web Security☁Cloud & DevOps◈GraphQL◆Redis☸Kubernetes
☰‹›
Description
All topics›JavaScript›Bit manipulation

Sum of Two Integers

advancedlayer I11 · Bit manipulation5 tests

Return a + b without using the + or - operators anywhere in your solution (that rules out ++, --, += and unary minus too).

  • Both inputs fit in a signed 32-bit integer, and so does the result
  • Either input may be negative — negatives are stored in two's complement, and the same add-with-carry loop handles them with no special casing
  • Why it terminates: JS bitwise operators wrap to 32 bits, so the carry keeps marching left and eventually falls off the top edge, reaching 0. Without that wrap a negative operand would loop forever.

Stuck?

All exercisesPlayground
1234567891011121314
loading the editor…

Nothing yet — hit Run and whatever you log shows up here.

Submit to see how you did.

⌘/Ctrl + Enter run⌘/Ctrl + S save⌘/Ctrl + / comment⌘/Ctrl + F find/replace⌘/Ctrl + D select next matchAlt + click multi-cursorTab indent · ⇧Tab outdentEsc leave fullscreen