de2bi
(Not recommended) Convert Decimal to Base-P
de2bi is not recommended. Instead, use the int2bit
function.
For more information, see Version History.
Description
Examples
Convert Decimal to Base-2
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:
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:
Input Arguments
d
— Decimal input
nonnegative integer | vector | matrix
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
n
— Number of output columns
positive integer scalar
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
p
— Base
2
(default) | positive integer scalar
flg
— MSB flag
'right-msb'
(default) | 'left-msb'
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
b
— Binary output
vector | matrix
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 R2006aR2021b: 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 Feature | Recommended 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) |
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)