dec2bin - Convert decimal integer to its binary representation - MATLAB (original) (raw)
Convert decimal integer to its binary representation
Syntax
Description
binStr = dec2bin([D](#mw%5F5d4ea126-4d83-4079-b235-06df2797ee10))
returns the binary, or base-2, representation of the decimal integer D
. The output argumentbinStr
is a character vector that represents binary digits using the characters 0
and 1
.
If D
is a numeric vector, matrix, or multidimensional array, thenbinStr
is a two-dimensional character array. Each row ofbinStr
represents an element of D
.
binStr = dec2bin([D](#mw%5F5d4ea126-4d83-4079-b235-06df2797ee10),[minDigits](#mw%5F375baeb1-7051-43d8-8976-d33ce60c4b57))
returns a binary representation with no fewer than minDigits
digits.
Examples
Convert a decimal number to a character vector that represents its binary value.
D = 23; binStr = dec2bin(D)
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)
If you specify fewer digits, then dec2bin
still returns as many binary digits as required to represent the input number.
Create a numeric array.
To represent the elements of D
as binary values, use the dec2bin
function. Each row of binStr
corresponds to an element of D
.
binStr = 3×10 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 binStr
, the dec2bin
function pads the third row to '0000001110'
.
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.
Input Arguments
Input array, specified as a numeric array, char
array, or logical array.
- If
D
is an array of floating-point numbers, and any element ofD
has a fractional part, thendec2bin
truncates it before conversion. For example,dec2bin
converts both12
and12.5
to'1100'
. The truncation is always to the nearest integer less than or equal to that element. - If
D
is a character or logical array, thendec2bin
treats the elements ofD
as integers. However,dec2bin
treats characters as their Unicode® values, so specifyingD
as a character array is not recommended.
Since R2020a
D
can include negative numbers. The function converts negative numbers using their two's complement binary values.
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
| logical
| char
Minimum number of digits in the output, specified as a nonnegative integer.
- If
D
can be represented with fewer thanminDigits
binary digits, thendec2bin
pads the output.D >= 0 Pads with leading zeros D < 0 Pads with leading ones (since R2020b) - If
D
is so large that it must be represented with more thanminDigits
digits, thendec2bin
returns the output with as many digits as required.
Tips
- The output of
dec2bin
is the same whether your computer stores values in memory using big-endian or little-endian format. For more information on these formats, see Endianness.
Extended Capabilities
Usage notes and limitations:
- If
minDigits
is specified, the output will have that number of columns even ifD
is empty. IfminDigits
is not specified, the output will have at least one column. - If input
D
isdouble
orsingle
, then it must be greater than or equal tointmin('int64')
and less than2^64
. - This function usually produces a variable-size output. To make the output fixed-size, supply
minDigits
as a constant large enough that the output has a fixed number of columns regardless of input values. For fixed-size output,minDigits
must be at least64
fordouble
,64
forsingle
,32
forhalf
,1
forlogical
,8
forchar
,64
forint64
,64
foruint64
,32
forint32
,32
foruint32
,16
forint16
,16
foruint16
,8
forint8
, and8
foruint8
.
Version History
Introduced before R2006a
User-defined datatypes are restricted to primitive numeric types and classes that inherit from a primitive numeric type.
dec2bin(0,0)
returns '0'
rather than a 1x0 character vector.