Communications Toolbox | ![]() ![]() |
Convert decimal numbers to binary vectors
Syntax
Description
b = de2bi(d)
converts a nonnegative decimal integer d
to a binary row vector. If d
is a vector, then the output b
is a matrix, each row of which is the binary form of the corresponding element in d
. If d
is a matrix, then de2bi
treats it like the vector d(:)
.
b = de2bi(d,n)
is the same as b = de2bi(d)
, except that its output has n
columns, where n
is a positive integer. An error occurs if the binary representations would require more than n
digits. If necessary, the binary representation of d
is padded with extra zeros.
b = de2bi(d,n,p)
converts a nonnegative decimal integer d
to a base-p
row vector, where p
is an integer greater than or equal to 2. The first column of b
is the lowest base-p
digit. b
is padded with extra zeros if necessary, so that it has n
columns, where n
is a positive integer. An error occurs if the base-p
representations would require more than n
digits. If d
is a nonnegative decimal vector, then the output b
is a matrix, each row of which is the (possibly zero-padded) base-p form of the corresponding element in d
. If d
is a matrix, then de2bi
treats it like the vector d(:)
.
b = de2bi(d,[],p)
specifies the base p
but not the number of columns.
b = de2bi(d,...,
uses the string flg
)
flg
to determine whether the first column of b
contains the lowest-order or highest-order digits. Values for flg
are 'right-msb
' and 'left-msb
'. The value 'right-msb
' produces the default behavior.
Examples
The code below counts to ten in decimal and binary.
Dec Binary ----- ------------------- 1 1 0 0 0 2 0 1 0 0 3 1 1 0 0 4 0 0 1 0 5 1 0 1 0 6 0 1 1 0 7 1 1 1 0 8 0 0 0 1 9 1 0 0 1 10 0 1 0 1
The command below shows how de2bi
pads its output with zeros.
The commands below show how to convert a decimal integer to base three without specifying the number of columns in the output matrix. They also show how to place the most significant digit on the left instead of on the right.
t = de2bi(12,[],3) % Convert 12 to base 3. t = 0 1 1 tleft = de2bi(12,[],3,'left-msb') % Significant digit on left tleft = 1 1 0
See Also
bi2de
![]() | ddemodce | decode | ![]() |