bi2de

Convert binary vectors to decimal numbers

Syntax

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

Description

dbi2de(b) converts a binary row vector b to a nonnegative decimal integer. If b is a matrix, each row is interpreted separately as a binary number. In this case, the output d is a column vector, each element of which is the decimal representation of the corresponding row of b.

    Note:   By default, bi2de interprets the first column of b as the lowest-order digit.

d = bi2de(b,flg) is the same as the syntax above, except that flg is a string that determines whether the first column of b contains the lowest-order or highest-order digits. Possible values for flg are 'right-msb' and 'left-msb'. The value 'right-msb' produces the default behavior.

d = bi2de(b,p) converts a base-p row vector b to a nonnegative decimal integer , where p is an integer greater than or equal to 2. The first column of b is the lowest base-p digit. If b is a matrix, the output d is a nonnegative decimal vector, each row of which is the decimal form of the corresponding row of b.

d = bi2de(b,p,flg) is the same as the syntax above, except that flg is a string that determines whether the first column of b contains the lowest-order or highest-order digits. Possible values for flg are 'right-msb' and 'left-msb'. The value 'right-msb' produces the default behavior.

Examples

Generate a matrix that contains binary representations of five random numbers between 0 and 15, and then convert all five numbers to decimal integers.

b = randi([0 1],5,4);   % Generate a 5-by-4 random binary matrix.
de = bi2de(b);
disp('    Dec          Binary')
disp('   -----   -------------------')
disp([de, b])

Sample output is below. Your results might vary because the numbers are random.

    Dec          Binary
   -----   -------------------
    13     1     0     1     1
     7     1     1     1     0
    15     1     1     1     1
     4     0     0     1     0
     9     1     0     0     1

Convert a base-five number into its decimal counterpart, using the leftmost base-five digit (4 in this case) as the most significant digit. This example reflects the fact that 4(53) + 2(52) +50 = 551.

d = bi2de([4 2 0 1],5,'left-msb')

The output is

d =

   551

See Also

Was this topic helpful?