dec2bin — Convert decimal integers to binary character rows in MATLAB and RunMat.
dec2bin(D) converts each element of D to one binary character row. dec2bin(D, minDigits) pads each row to at least minDigits characters. Numeric arrays are processed in column-major element order and produce one output row per element.
Syntax
dec2bin(D)How dec2bin works
Dmay be numeric, integer, logical, character, or gpuArray numeric data.- Double inputs are floored before conversion, matching MATLAB
dec2binbehavior. - Nonnegative values are padded with
0characters. - Negative values are represented using the smallest signed two's-complement storage width that can contain the value, with additional padding using
1characters. - Signed integer inputs preserve their source integer storage width for negative two's-complement output.
minDigitsmust be a finite nonnegative integer scalar.- Outputs larger than RunMat's character-output safety limit are rejected before formatting.
- Complex, non-finite, and unsupported input values raise an error.
Examples
Convert a scalar decimal value
s = dec2bin(23)Expected output:
s = '10111'Pad to a minimum width
s = dec2bin(23, 8)Expected output:
s = '00010111'Convert multiple values to padded rows
s = dec2bin([1023 122 14])Expected output:
s = ['1111111111'; '0001111010'; '0000001110']Format a negative value as two's complement
s = dec2bin(-16)Expected output:
s = '11110000'Using dec2bin with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how dec2bin changes the result.
Run a small dec2bin example, explain the result, then change one input and compare the output.
FAQ
Does dec2bin round fractional inputs?⌄
No. RunMat floors fractional double inputs before conversion, following MATLAB dec2bin behavior.
How are negative numbers represented?⌄
Negative values are emitted as two's-complement binary rows using 8, 16, 32, or 64 bits as needed.
Related Comms functions
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how dec2bin is executed, line by line, in Rust.
- View the source for dec2bin 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.