Title: Bitwise AND on signed i8/i16 skips sign extension when one operand is wrapped or negative · Issue #3061 · AssemblyScript/assemblyscript · GitHub
Open Graph Title: Bitwise AND on signed i8/i16 skips sign extension when one operand is wrapped or negative · Issue #3061 · AssemblyScript/assemblyscript
X Title: Bitwise AND on signed i8/i16 skips sign extension when one operand is wrapped or negative · Issue #3061 · AssemblyScript/assemblyscript
Description: Bug description Bitwise & on i8 or i16 omits sign extension (i32.extend8_s / i32.extend16_s) when one operand is already wrapped or is a negative constant, leaving upper bits from an unmasked arithmetic expression intact. For example, in...
Open Graph Description: Bug description Bitwise & on i8 or i16 omits sign extension (i32.extend8_s / i32.extend16_s) when one operand is already wrapped or is a negative constant, leaving upper bits from an unmasked arith...
X Description: Bug description Bitwise & on i8 or i16 omits sign extension (i32.extend8_s / i32.extend16_s) when one operand is already wrapped or is a negative constant, leaving upper bits from an unmasked a...
Opengraph URL: https://github.com/AssemblyScript/assemblyscript/issues/3061
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Bitwise AND on signed i8/i16 skips sign extension when one operand is wrapped or negative","articleBody":"### Bug description\n\nBitwise `&` on `i8` or `i16` omits sign extension (`i32.extend8_s` / `i32.extend16_s`) when one operand is already wrapped or is a negative constant, leaving upper bits from an unmasked arithmetic expression intact.\n\nFor example, in the reproduction below:\n- In `f()`, `a + a` wraps to `-2` in `i16`, and `-2 & -1` should evaluate to `-2`, so `f()` should return `-2n`, but it returns `65534n`.\n- In `g(100)`, `a + a` wraps to `-56` in `i8`, and `-56 & -128` should evaluate to `-128`, so `g(100)` should return `true` (`1`), but it returns `false` (`0`).\n\n`Flow.canOverflow` for `BinaryOp.AndI32` treats `x & y` as non-overflowing if either operand cannot overflow or is a constant `\u003c= computeSmallIntegerMask(type)`, which only holds for non-negative values since negative small integers have `1`s in their upper 16/24 bits.\n\n### Steps to reproduce\n\n```ts\nexport function f(): i64 {\n let a: i16 = 32767;\n let m: i16 = -1;\n let b: i16 = (a + a) & m; // a + a wraps to -2 in i16; -2 & -1 is -2\n return \u003ci64>b;\n}\n\nexport function g(a: i8): bool {\n let b: i8 = (a + a) & \u003ci8>-128; // for a = 100: a + a wraps to -56; -56 & -128 is -128\n return b == -128;\n}\n```\n\nCompile with `asc repro.ts -O0` (or `-O3`) and call `f()` and `g(100)`.\n\nCredit: https://ucsbplse.github.io/\n\n### AssemblyScript version\n\nv0.28.20\n\n### Playground\n\nhttps://www.assemblyscript.org/editor.html#IyFvcHRpbWl6ZT1zcGVlZCZydW50aW1lPWluY3JlbWVudGFsCi8vIENhc2UgMTogPGJvb2w+IGNhc3Qgb2YgdTggZG9lcyBub3QgdHJ1bmNhdGUKZXhwb3J0IGZ1bmN0aW9uIGYoYTogdTgpOiBib29sIHsKICBsZXQgYzogdTggPSBhICsgMTsKICByZXR1cm4gPGJvb2w+YzsKfQpleHBvcnQgZnVuY3Rpb24gZyhhOiB1OCk6IGJvb2wgewogIGxldCBjOiB1OCA9IGEgKyAxOwogIHJldHVybiBjID8gdHJ1ZSA6IGZhbHNlOwp9Ci8vIENhc2UgMjogaTE2L2k4ICYgc2tpcHMgc2lnbiBleHRlbnNpb24KZXhwb3J0IGZ1bmN0aW9uIGgoKTogaTY0IHsKICBsZXQgYTogaTE2ID0gMzI3Njc7CiAgbGV0IG06IGkxNiA9IC0xOwogIGxldCBiOiBpMTYgPSAoYSArIGEpICYgbTsKICByZXR1cm4gPGk2ND5iOwp9CmV4cG9ydCBmdW5jdGlvbiBrKGE6IGk4KTogYm9vbCB7CiAgbGV0IGI6IGk4ID0gKGEgKyBhKSAmIDxpOD4tMTI4OwogIHJldHVybiBiID09IC0xMjg7Cn0KIyFodG1sCjxwcmUgaWQ9Im91dCI+PC9wcmU+CjxzY3JpcHQgdHlwZT0ibW9kdWxlIj4KY29uc3QgZSA9IGF3YWl0IGluc3RhbnRpYXRlKGF3YWl0IGNvbXBpbGUoKSwge30pOwpkb2N1bWVudC5nZXRFbGVtZW50QnlJZCgib3V0IikudGV4dENvbnRlbnQgPSBbCiAgImYoMjU1KSA9ICIgKyBlLmYoMjU1KSArICIgIChleHBlY3RlZCBmYWxzZSkiLAogICJnKDI1NSkgPSAiICsgZS5nKDI1NSkgKyAiICAoZXhwZWN0ZWQgZmFsc2UpIiwKICAiaCgpID0gIiArIGUuaCgpICsgIiAgKGV4cGVjdGVkIC0yKSIsCiAgImsoMTAwKSA9ICIgKyBlLmsoMTAwKSArICIgIChleHBlY3RlZCB0cnVlKSIKXS5qb2luKCJcbiIpOwo8L3NjcmlwdD4=\n\nClick Run: `f(255)` prints `true` (case 1, expected `false`), `h()` prints `65534` (expected `-2`) and `k(100)` prints `false` (case 2, expected `true`).\n","author":{"url":"https://github.com/trackoor","@type":"Person","name":"trackoor"},"datePublished":"2026-11-07T03:45:31Z","url":"https://github.com/AssemblyScript/assemblyscript/issues/3061"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:3d0cc483-7281-a2ac-75cc-e326b8611d35 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C0A2:07D0:214DEF7:2C233DB:6B0051F7 |
| html-safe-nonce | d167205965ac17db593832a555800754dfdd7826c5a337cb7b7509f14f0525f0 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDMEEyOjA3RDA6MjE0REVGNzoyQzIzM0RCOjZCMDA1MUY3IiwidmlzaXRvcl9pZCI6IjU1Nzg1Mzg4NTU2ODgwNjU1MjciLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 37fc612f9e4cac4129d6867511f574dc39430ca5f1f6de99cfb82801405fe551 |
| hovercard-subject-tag | issue:6254800406 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/AssemblyScript/assemblyscript/3061/issue_layout |
| twitter:image | https://opengraph.githubassets.com/eabae5c08c3be91c9b250f0b11392eb11f33be5e6ea2c729b0fcc30b5da92415/AssemblyScript/assemblyscript/issues/3061 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/eabae5c08c3be91c9b250f0b11392eb11f33be5e6ea2c729b0fcc30b5da92415/AssemblyScript/assemblyscript/issues/3061 |
| og:image:alt | Bug description Bitwise & on i8 or i16 omits sign extension (i32.extend8_s / i32.extend16_s) when one operand is already wrapped or is a negative constant, leaving upper bits from an unmasked arith... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | trackoor |
| hostname | github.com |
| expected-hostname | github.com |
| None | f420f4996e6b1ff1ccd6d7683f8134f4b5f0f8a50124c210dbfe7490e5644abb |
| turbo-cache-control | no-preview |
| go-import | github.com/AssemblyScript/assemblyscript git https://github.com/AssemblyScript/assemblyscript.git |
| octolytics-dimension-user_id | 28916798 |
| octolytics-dimension-user_login | AssemblyScript |
| octolytics-dimension-repository_id | 105138356 |
| octolytics-dimension-repository_nwo | AssemblyScript/assemblyscript |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 105138356 |
| octolytics-dimension-repository_network_root_nwo | AssemblyScript/assemblyscript |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 26e00df917aba0f128135dc073ea0c5e33a70d40 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width