Convert decimal integer to its binary representation
binStr = dec2bin( returns the binary, or
base-2, representation of the decimal integer
D. The output argument
binStr is a character vector that represents binary digits using the
D is a numeric vector, matrix, or multidimensional array, then
binStr is a two-dimensional character array. Each row of
binStr represents an element of
Convert Decimal Number
Convert a decimal number to a character vector that represents its binary value.
D = 23; binStr = dec2bin(D)
binStr = '10111'
Specify Minimum Number of Digits
Specify the minimum number of binary digits that
dec2bin returns. If you specify more digits are required, then
dec2bin pads the output.
D = 23; binStr = dec2bin(D,8)
binStr = '00010111'
If you specify fewer digits, then
dec2bin still returns as many binary digits as required to represent the input number.
binStr = dec2bin(D,1)
binStr = '10111'
Convert Numeric Array
Create a numeric array.
D = [1023 122 14];
To represent the elements of
D as binary values, use the
dec2bin function. Each row of
binStr corresponds to an element of
binStr = dec2bin(D)
binStr = 3x10 char array '1111111111' '0001111010' '0000001110'
Since all rows of a character array must have the same number of characters,
dec2bin pads some rows of
binStr. For example, the number
14 can be represented by the binary digits
'1110'. But to match the length of the first row of
dec2bin function pads the third row to
Represent Negative Numbers
Starting in R2020a, the
dec2bin function converts negative numbers using their two's complement binary values.
For example, these calls to
dec2bin convert negative numbers.
ans = '11111111'
ans = '11110000'
D — Input array
numeric array |
char array | logical array
Input array, specified as a numeric array,
char array, or logical
Dis an array of floating-point numbers, and any element of
Dhas a fractional part, then
dec2bintruncates it before conversion. For example,
'1100'. The truncation is always to the nearest integer less than or equal to that element.
Dis a character or logical array, then
dec2bintreats the elements of
Das integers. However,
dec2bintreats characters as their Unicode® values, so specifying
Das a character array is not recommended.
D can include negative numbers. The function converts negative
numbers using their two's complement binary values.
minDigits — Minimum number of digits in output
Minimum number of digits in the output, specified as a nonnegative integer.
Dcan be represented with fewer than
minDigitsbinary digits, then
dec2binpads the output.
D >= 0
Pads with leading zeros
D < 0
Pads with leading ones (since R2020b)
Dis so large that it must be represented with more than
dec2binreturns the output with as many digits as required.
The output of
dec2binis the same whether your computer stores values in memory using big-endian or little-endian format. For more information on these formats, see Endianness.
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Usage notes and limitations:
minDigitsis specified, the output will have that number of columns even if
Dis empty. If
minDigitsis not specified, the output will have at least one column.
single, then it must be greater than or equal to
intmin('int64')and less than
This function usually produces a variable-size output. To make the output fixed-size, supply
minDigitsas a constant large enough that the output has a fixed number of columns regardless of input values. For fixed-size output,
minDigitsmust be at least
Run code in the background using MATLAB®
backgroundPool or accelerate code with Parallel Computing Toolbox™
This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.
Version HistoryIntroduced before R2006a
R2022a: Restrict input datatype to primitive numeric types
User-defined datatypes are restricted to primitive numeric types and classes that inherit from a primitive numeric type.
'0' rather than a 1x0