bitshift — Shift integer-valued scalars and arrays left or right by bit counts.
bitshift(A, k) shifts integer-valued input A left for positive k and right for negative k. A and k must be scalar or exactly the same size; bitshift(A, k, assumedtype) selects the signed or unsigned interpretation width for double A.
Syntax
C = bitshift(A, K)
C = bitshift(A, K, assumedtype)Inputs
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
A | NumericArray | Yes | — | Integer-valued input. |
K | NumericArray | Yes | — | Shift count; positive shifts left and negative shifts right. |
assumedtype | StringScalar | No | — | Integer class used to interpret double input A. |
Returns
| Name | Type | Description |
|---|---|---|
C | NumericArray | Bitwise numeric result. |
Errors
| Identifier | When | Message |
|---|---|---|
RunMat:bitwise:InvalidInput | Inputs are not finite integer-valued numeric, logical, or gatherable gpuArray values. | bitwise operation: invalid input |
RunMat:bitwise:SizeMismatch | Input shapes violate the operation's expansion rule: binary bitwise functions use compatible-size expansion, while bit positions/counts require scalar expansion or exactly matching nonscalar sizes. | bitwise operation: array sizes are not compatible |
How bitshift works
- A accepts double and all eight built-in integer classes; k independently accepts double and all eight integer classes.
- Positive shifts move bits left and truncate overflow bits; negative shifts move bits right and preserve the sign bit for signed A.
- Shift magnitudes at least the width of A produce zero, except a negative shift of negative signed A produces all one-bits.
- Output preserves the data type of A; the class of k does not affect output class.
- Rejects fractional, infinite, NaN, complex, logical, single, string, cell, struct, and nonscalar unequal-size inputs in MATLAB-compatible mode.
Does RunMat run bitshift on the GPU?
At least one input must be an integer array, A cannot be signed, 64-bit integers are unsupported in either input, and assumedtype is unsupported.
RunMat mode separately gates single A, single k, logical A, logical k, broader resident input combinations, and resident assumedtype calls as extensions.
GPU memory and residency
Documented GPU calls gather exactly for host execution and restore the output class of A to the owning provider.
Example
Left and right shifts
bitshift(uint32(3), 2)
bitshift(uint32(8), -1)Expected output:
ans = uint32
12
ans = uint32
4Using bitshift with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how bitshift changes the result.
Run a small bitshift example, explain the result, then change one input and compare the output.
FAQ
Can shift counts be arrays?⌄
Yes. A and k can each be scalar, or they can be arrays with exactly the same size; general singleton expansion is not supported.
Related Logical functions
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how bitshift is executed, line by line, in Rust.
- View the source for bitshift in Rust on GitHub
- Learn how the RunMat runtime works
- Found a bug? Open an issue with a minimal reproduction.
About RunMat
RunMat is an open-source runtime that executes MATLAB-syntax code blazing on any GPU. It is licensed under the Apache 2.0 license.
- RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed. Simulations that took hours now take minutes.
- Start running code in seconds. RunMat runs in the browser, on the desktop, or from the CLI. No license server, no IT ticket.