Communications Toolbox    
de2bi

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,...,flg) uses the string 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.

The output is below.

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.

See Also
bi2de


  ddemodce decode