# de2bi

(Not recommended) Convert Decimal to Base-P

de2bi is not recommended. Instead, use the int2bit function. For more information, see Compatibility Considerations.

## Description

b = de2bi(d) converts a nonnegative decimal integer d to a binary row vector. If d is a vector, the output b is a matrix in which each row is the binary form of the corresponding element in d.

b = de2bi(d,n) has an output with n columns.

b = de2bi(d,n,p) converts a nonnegative decimal integer d to a base-p row vector.

b = de2bi(d,[],p) specifies the base, p .

example

b = de2bi(d,___,flg) uses flg to determine whether the first column of b contains the lowest-order or highest-order digits.

## Examples

collapse all

This example shows how to convert decimals to binary numbers in their base-2 equivalents.

d_array = [1 2 3 4];

Convert the decimal array to binary by using the de2bi function. Specify that the most significant digit is the leftmost element and set the number of desired columns to 5. The output becomes a 4-by-5 matrix where each row corresponds to a decimal value from the input. Because the largest decimal value in d_array can be expressed in 3 columns, the de2bi pads the matrix with two extra zero columns at the specified most-significant bit side. If you specify too few columns, the conversion will fail.

b_array = de2bi(d_array,5,'left-msb')
b_array = 4×5

0     0     0     0     1
0     0     0     1     0
0     0     0     1     1
0     0     1     0     0

b_array = de2bi(d_array,5,'right-msb')
b_array = 4×5

1     0     0     0     0
0     1     0     0     0
1     1     0     0     0
0     0     1     0     0

If you do not specify a number of columns, the number of columns is exactly what is needed to express the largest decimal of the input.

b_array = de2bi(d_array,'left-msb')
b_array = 4×3

0     0     1
0     1     0
0     1     1
1     0     0

The output rows for specifying a leftmost-significant bit correspond to:

$1=0\left({2}^{2}\right)+0\left({2}^{1}\right)+1\left({2}^{0}\right)$

$2=0\left({2}^{2}\right)+1\left({2}^{1}\right)+0\left({2}^{0}\right)$

$3=0\left({2}^{2}\right)+1\left({2}^{1}\right)+1\left({2}^{0}\right)$

$4=1\left({2}^{2}\right)+0\left({2}^{1}\right)+0\left({2}^{0}\right)$

b_array = de2bi(d_array,'right-msb')
b_array = 4×3

1     0     0
0     1     0
1     1     0
0     0     1

The output rows for specifying a rightmost-significant bit correspond to:

$1=1\left({2}^{0}\right)+0\left({2}^{1}\right)+0\left({2}^{2}\right)$

$2=0\left({2}^{0}\right)+1\left({2}^{1}\right)+0\left({2}^{2}\right)$

$3=1\left({2}^{0}\right)+1\left({2}^{1}\right)+0\left({2}^{2}\right)$

$4=0\left({2}^{0}\right)+0\left({2}^{1}\right)+1\left({2}^{2}\right)$

## Input Arguments

collapse all

Decimal input, specified as a nonnegative integer, vector, or matrix. If d is a matrix, it is treated like the column vector d(:).

Note

To ensure an accurate conversion, d must be less than or equal to 252.

Data Types: double | single | integer | fi

The number of output columns specified as a positive scalar. If necessary, the binary representation of d is padded with extra zeros.

Data Types: double | single

Base of the output b, specified as an integer greater than or equal to 2.

• If d is a vector, the output b is a matrix in which each row is the base-p form of the corresponding element in d.

• If d is a matrix, de2bi treats it like the vector d(:).

Data Types: double | single

MSB flag, specified as 'right-msb' or 'left-msb'.

• 'right-msb' –– Indicates the right (or last) column of the binary output, b, as the most significant bit (or highest-order digit).

• 'left-msb' –– Indicates the left (or first) column of the binary output, b, as the most significant bit (or highest-order digit).

Data Types: char | string

## Output Arguments

collapse all

Binary representation of d, returned as a row vector or matrix. The output is of the same data type as the input.

## Compatibility Considerations

expand all

Not recommended starting in R2021b