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

NameTypeRequiredDefaultDescription
ANumericArrayYesInteger-valued input.
KNumericArrayYesShift count; positive shifts left and negative shifts right.

Returns

NameTypeDescription
CNumericArrayBitwise numeric result.

Errors

IdentifierWhenMessage
RunMat:bitwise:InvalidInputInputs are not finite integer-valued numeric, logical, or gatherable gpuArray values.bitwise operation: invalid input
RunMat:bitwise:SizeMismatchInput shapes are not compatible for implicit expansion.bitwise operation: array sizes are not compatible
RunMat:idivide:DivideByZeroThe divisor contains zero.idivide: division by zero
RunMat:idivide:OverflowA 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
  4

Using 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.

Bit

and · bitand · bitor · not · or · xor

Rel

eq · ge · gt · isequal · isequaln · le · lt · ne

Logical

logical

Open-source implementation

Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how bitshift is executed, line by line, in Rust.

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.

Getting started · Benchmarks · Pricing

Download RunMat

Download RunMat for full performance, or use RunMat in your browser for zero setup.