de2bi — Convert nonnegative decimal integers to binary or base-p digit rows in MATLAB and RunMat.
de2bi(D) converts decimal integers to rows of binary digits. Matrix inputs are converted in column-major D(:) order. Omitted width uses the minimum number of columns needed for the largest input. de2bi(D, N, P, FLG) selects the width, base, and digit ordering.
Syntax
de2bi(D)How de2bi works
Dmay be a scalar, vector, logical array, or numeric matrix; matrix inputs are flattened in column-major order.Dvalues must be finite nonnegative integers in RunMat's exact integer range.Nspecifies the number of output digit columns. IfNis omitted or[], RunMat chooses the minimum width needed for the maximum input value.- If a supplied
Nis too small to represent an input value in baseP, RunMat raises an error. Pmust be an integer greater than one and defaults to 2.'right-msb'is the default and stores the least significant digit in column 1.'left-msb'stores the most significant digit in column 1.
Examples
Minimum-width binary expansion
b = de2bi([0; 1; 2; 5])Expected output:
b = [0 0 0; 1 0 0; 0 1 0; 1 0 1]Fixed-width left-MSB binary expansion
b = de2bi(5, 4, 'left-msb')Expected output:
b = [0 1 0 1]Base-3 expansion
b = de2bi(21, 4, 3, 'left-msb')Expected output:
b = [0 2 1 0]Using de2bi with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how de2bi changes the result.
Run a small de2bi example, explain the result, then change one input and compare the output.
FAQ
How does de2bi choose the number of columns?⌄
When N is omitted, RunMat chooses the minimum width required by the largest input value in the selected base, with zero represented using one digit.
Can I pass a base without a width?⌄
Yes. Use an empty width placeholder, for example de2bi(D, [], 3).
Related Comms functions
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how de2bi is executed, line by line, in Rust.
- View the source for de2bi 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.