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, using implicit expansion between A and K.
Syntax
C = bitshift(A, K)Inputs
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
A | NumericArray | Yes | — | Integer-valued input. |
K | NumericArray | Yes | — | Shift count; positive shifts left and negative shifts right. |
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 are not compatible for implicit expansion. | bitwise operation: array sizes are not compatible |
RunMat:idivide:DivideByZero | The divisor contains zero. | idivide: division by zero |
RunMat:idivide:Overflow | A rounded quotient cannot be represented in the dividend integer class. | idivide: quotient overflows output class |
How bitshift works
- Accepts double integer-valued inputs, logical inputs, integer scalar values, integer-class tensors, and gatherable gpuArray tensors.
- Positive shifts move bits left; negative shifts move bits right.
- Shift magnitudes of 32 or more produce zero for the uint32 execution path.
- Preserves uint8, uint16, and uint32 output class from the input being shifted.
- Rejects fractional, infinite, NaN, complex, string, cell, and struct inputs.
Does RunMat run bitshift on the GPU?
RunMat currently executes integer bitwise functions on the host.
GPU memory and residency
GPU inputs are gathered for host execution.
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 are implicitly expanded against each other when their shapes are compatible.
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.