Main Content

bi2de

(Not recommended) Convert Binary to Base-P

bi2de is not recommended. Instead, use the bit2int function. For more information, see Version History.

Description

d = bi2de(b) converts a binary row vector b to a decimal integer.

example

d = bi2de(b,flg) converts a binary row vector to a decimal integer, where flg determines the position of the most significant digit.

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

d = bi2de(b,p,flg) converts a base-p row vector to a decimal integer, where flg determines the position of the most significant digit.

Examples

collapse all

This example shows how to convert binary numbers to decimal integers. It highlights the difference between right- and left- most significant digit positioning.

b1 = [0 1 0 1 1];
b2 = [1 1 1 0];

Convert the two binary arrays to decimal by using the bi2de function. Assign the most significant digit is the leftmost element. The output of converting b1 corresponds to 0 ( 2 4 ) + 1 ( 2 3 ) + 0 ( 2 2 ) + 1 ( 2 1 ) + 1 ( 2 0 ) = 1 1 , and b2 corresponds to 1 ( 2 3 ) + 1 ( 2 2 ) + 1 ( 2 1 ) + 0 ( 2 0 ) = 1 4 .

d1 = bi2de(b1,'left-msb')
d1 = 11
d2 = bi2de(b2,'left-msb')
d2 = 14

Assign the most significant digit is the rightmost element. The output of converting b1 corresponds to 0 ( 2 0 ) + 1 ( 2 1 ) + 0 ( 2 2 ) + 1 ( 2 3 ) + 1 ( 2 4 ) = 2 6 , and b2 corresponds to 1 ( 2 0 ) + 1 ( 2 1 ) + 1 ( 2 2 ) + 0 ( 2 3 ) = 7 .

d1 = bi2de(b1,'right-msb')
d1 = 26
d2 = bi2de(b2,'right-msb')
d2 = 7

Input Arguments

collapse all

Binary input, specified as a row vector or matrix of positive integer or logical values.

Note

b must represent an integer less than or equal to 252.

Data Types: double | single | logical | integer | fi

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

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

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

Data Types: char | string

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

Data Types: double | single

Output Arguments

collapse all

Decimal output, returned as an nonnegative integer or row vector. If b is a matrix, each row represents a base-p number. In this case, the output d is a column vector in which each element is the decimal representation of the corresponding row of b.

If the input data type is

  • An integer data type and the value of d can be contained in the same integer data type as the input, the output data type uses the same data type as the input. Otherwise, the output data type is chosen to be big enough to contain the decimal output.

  • double or logical data type, the output data type is double.

  • single data type, the output data type is single.

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 bit2int instead of bi2de. If converting numbers from a nonbase-2 representation to decimal, use base2dec.

The code in this table shows binary-to-decimal 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,1],n*bpi,1);
y = bi2de(reshape(x,bpi,[])','left-msb')
% Default (left MSB)
n = randi([1 100]); % Number of integers
bpi = 3;            % Bits per integer
x = randi([0,1],n*bpi,1);
y = bit2int(x,bpi)
% Default row vector (or matrix) input
x = [0 1 1];
bi2de(x)
% Default row vector (or matrix) input
x = [0 1 1];
bit2int(x',length(x),0)'
% Right MSB, logical input
n = randi([1 100]); % Number of integers
bpi = 5;            % Bits per integer
x = logical(randi([0,1],n*bpi,1));
y = bi2de(reshape(x,bpi,[])','right-msb')
% Right MSB, logical input
n = randi([1 100]); % Number of integers
bpi = 5;            % Bits per integer
x = logical(randi([0,1],n*bpi,1));
y = bit2int(x,bpi,false)
% Right MSB, signed input, single input
n = randi([1 100]); % Number of integers
bpi = 8;            % Bits per integer
x = randi([0,1],n*bpi,1,'single');
y = bi2de(reshape(x,bpi,[])','right-msb');
N = 2^bpi;
y = y - (y>=N/2)*N
% Right MSB, signed input, single input
n = randi([1 100]); % Number of integers
bpi = 8;            % Bits per integer
x = randi([0,1],n*bpi,1,'single');
y = bit2int(x,bpi,false);
N = 2^bpi;
y = y - (y>=N/2)*N

See Also

|