Communications Toolbox | ![]() ![]() |
Convert decimal numbers to binary vectors
Syntax
b = de2bi(d);
b = de2bi(d,n);
b = de2bi(d,n,p);
b = de2bi(d,[],p);
b = de2bi(d,...,flag
)
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 two. 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 flag
)
flag
to determine whether the first column of b
contains the lowest-order or highest-order digits. Values for flag
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.
d = (1:10)'; b = de2bi(d); disp(' Dec Binary ') disp(' ----- -------------------') disp([d, b])
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.
bb = de2bi([3 9],5) % Zero-padding the output bb = 1 1 0 0 0 1 0 0 1 0
The command below shows how to convert a decimal integer to base three without specifying the number of columns in the output matrix.
t = de2bi(12,[],3) % Convert 12 to base 3. t = 0 1 1
See Also
bi2de
![]() | ddemodce | decode | ![]() |