Integer arithmetic in WebAssembly
No operators, only instructions — and division declares its sign.
(module
;; Integer division and remainder come signed and unsigned; the
;; suffix picks. There is no operator syntax, only instructions.
(func (export "split_coins") (param $coins i32) (param $ways i32)
(result i32)
(i32.div_s (local.get $coins) (local.get $ways)))
(func (export "left_over") (param $coins i32) (param $ways i32)
(result i32)
(i32.rem_s (local.get $coins) (local.get $ways)))
(func (export "double_less_one") (param $n i32) (result i32)
(i32.sub (i32.mul (local.get $n) (i32.const 2)) (i32.const 1))))
How it works
i32.div_sdivides as signed;_uwould read the bits unsigned.i32.rem_sis the matching remainder.- Compound math nests folded calls: multiply, then subtract.
Keywords and builtins used here
exportfunci32moduleparamresult
The run, in numbers
- Lines
- 13
- Characters to type
- 520
- Tokens
- 109
- Three-star pace
- 65 tpm
At the three-star pace of 65 tokens a minute, this run takes about 101 seconds.
Step 1 of 3 in Numbers, step 4 of 28 in Language basics.