Main Content

bi2de

Convert binary vectors to decimal numbers

Description

example

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.

example

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

Generate a matrix that contains binary representations of five random numbers between 0 and 15. Convert the binary numbers to decimal integers.

b = randi([0 1],5,4);
d = bi2de(b)
d = 5×1

     1
     5
    14
    11
    15

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(24)+1(23)+0(22)+1(21)+1(20)=11, and b2 corresponds to 1(23)+1(22)+1(21)+0(20)=14.

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(20)+1(21)+0(22)+1(23)+1(24)=26, and b2 corresponds to 1(20)+1(21)+1(22)+0(23)=7.

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

Convert an octal (base-8) number to its decimal equivalent.

Assign the most significant digit to the leftmost position. The output corresponds to 4(83)+2(82)+7(81)+1(80)=2233.

d = bi2de([4 2 7 1],8,'left-msb')
d = 2233

Assign the most significant digit to the rightmost position. The output corresponds to 4(80)+2(81)+7(82)+1(83)=980.

d = bi2de([4 2 7 1],8,'right-msb')
d = 980

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™.

See Also

Introduced before R2006a