Main Content

de2bi

(Not recommended) Convert Decimal to Base-P

de2bi is not recommended. Instead, use the int2bit function. For more information, see Version History.

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 ( 2 2 ) + 0 ( 2 1 ) + 1 ( 2 0 )

2 = 0 ( 2 2 ) + 1 ( 2 1 ) + 0 ( 2 0 )

3 = 0 ( 2 2 ) + 1 ( 2 1 ) + 1 ( 2 0 )

4 = 1 ( 2 2 ) + 0 ( 2 1 ) + 0 ( 2 0 )

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 ( 2 0 ) + 0 ( 2 1 ) + 0 ( 2 2 )

2 = 0 ( 2 0 ) + 1 ( 2 1 ) + 0 ( 2 2 )

3 = 1 ( 2 0 ) + 1 ( 2 1 ) + 0 ( 2 2 )

4 = 0 ( 2 0 ) + 0 ( 2 1 ) + 1 ( 2 2 )

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.

Extended Capabilities

C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.

Version History

Introduced before R2006a

collapse all

R2021b: Not recommended

Use int2bit instead of de2bi. If converting the representation of numbers from decimal to a base other than 2, use dec2base.

The code in this table shows decimal-to-binary conversion for various inputs using the recommended function.

Discouraged FeatureRecommended Replacement
% Default (left MSB)
n = randi([1 100]); % Number of integers
bpi = 3;            % Bits per integer
x = randi([0,2^bpi-1],n,1);
y = reshape(de2bi(x,bpi,'left-msb')',[],1)
% Default (left MSB)
n = randi([1 100]); % Number of integers
bpi = 3;            % Bits per integer
x = randi([0,2^bpi-1],n,1);
y = int2bit(x,bpi)
% Default vector (or scalar) input
x = [4 5 9];
y = de2bi(x)
% Default vector (or scalar) input
x = [4 5 9];
y = int2bit(x,ceil(log2(max(x) + 1)), 0)'
% Right MSB
n = randi([1 100]); % Number of integers
bpi = 5;            % Bits per integer
x = randi([0,2^bpi-1],n,1);
y = reshape(de2bi(x,bpi,'right-msb')',[],1)
% Right MSB
n = randi([1 100]); % Number of integers
bpi = 5;            % Bits per integer
x = randi([0,2^bpi-1],n,1);
y = int2bit(x,bpi,false)
% Right MSB, signed input
n = randi([1 100]); % Number of integers
bpi = 8;            % Bits per integer
N = 2^bpi;
x = randi([-N/2,N/2-1],n,1);
y = reshape(de2bi(x+(x<0)*N,bpi,'right-msb')',[],1)
% Right MSB, signed input
n = randi([1 100]); % Number of integers
bpi = 8;            % Bits per integer
N = 2^bpi;
x = randi([-N/2,N/2-1],n,1);
y = int2bit(x+(x<0)*N,bpi,false)

See Also

|