# bi2de

Convert binary vectors to decimal numbers

## Syntax

``d = bi2de(b)``
``d = bi2de(b,flg)``
``d = bi2de(b,p)``
``d = bi2de(b,p,flg)``

## 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\left({2}^{4}\right)+1\left({2}^{3}\right)+0\left({2}^{2}\right)+1\left({2}^{1}\right)+1\left({2}^{0}\right)=11$, and `b2` corresponds to $1\left({2}^{3}\right)+1\left({2}^{2}\right)+1\left({2}^{1}\right)+0\left({2}^{0}\right)=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\left({2}^{0}\right)+1\left({2}^{1}\right)+0\left({2}^{2}\right)+1\left({2}^{3}\right)+1\left({2}^{4}\right)=26$, and `b2` corresponds to $1\left({2}^{0}\right)+1\left({2}^{1}\right)+1\left({2}^{2}\right)+0\left({2}^{3}\right)=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\left({8}^{3}\right)+2\left({8}^{2}\right)+7\left({8}^{1}\right)+1\left({8}^{0}\right)=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\left({8}^{0}\right)+2\left({8}^{1}\right)+7\left({8}^{2}\right)+1\left({8}^{3}\right)=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.

### Note

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

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

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

## 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 value of `d` can be contained in the same data type as the input, it inherits the input data type. Otherwise, the output data type is chosen to be big enough to contain the decimal output.