Documentation Center

  • Trial Software
  • Product Updates

de2bi

Convert decimal numbers to binary vectors

Syntax

b = de2bi(d)
b = de2bi(d,n)
b = de2bi(d,n,p)
b = de2bi(d,[],p)
b = de2bi(d,...,flg)

Description

b = de2bi(d) converts a nonnegative decimal integer d to a binary row vector. If d is a vector, the output b is a matrix, each row of which is the binary form of the corresponding element in d. If d is a matrix, de2bi treats it like the vector d(:).

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

b = de2bi(d,n) is the same as b = de2bi(d), except that its output has n columns, where n is a positive integer. An error occurs if the binary representations would require more than n digits. If necessary, the binary representation of d is padded with extra zeros.

b = de2bi(d,n,p) converts a nonnegative decimal integer d to a base-p row vector, where p is an integer greater than or equal to 2. The first column of b is the lowest base-p digit. b is padded with extra zeros if necessary, so that it has n columns, where n is a positive integer. An error occurs if the base-p representations would require more than n digits. If d is a nonnegative decimal vector, the output b is a matrix, each row of which is the (possibly zero-padded) base-p form of the corresponding element in d. If d is a matrix, de2bi treats it like the vector d(:).

b = de2bi(d,[],p) specifies the base p but not the number of columns.

b = de2bi(d,...,flg) uses the string flg to determine whether the first column of b contains the lowest-order or highest-order digits. Values for flg are 'right-msb' and 'left-msb'. The value 'right-msb' produces the default behavior.

Examples

The code below counts to 10 in decimal and binary.

d = (1:10)';
b = de2bi(d);
disp('    Dec          Binary       ')
disp('   -----   -------------------')
disp([d, b])

The output is below.

    Dec          Binary       
   -----   -------------------
     1     1     0     0     0
     2     0     1     0     0
     3     1     1     0     0
     4     0     0     1     0
     5     1     0     1     0
     6     0     1     1     0
     7     1     1     1     0
     8     0     0     0     1
     9     1     0     0     1
    10     0     1     0     1

The command below shows how de2bi pads its output with zeros.

bb = de2bi([3 9],5) % Zero-padding the output

bb =

     1     1     0     0     0
     1     0     0     1     0

The commands below show how to convert a decimal integer to base three without specifying the number of columns in the output matrix. They also show how to place the most significant digit on the left instead of on the right.

t = de2bi(12,[],3) % Convert 12 to base 3.

tleft = de2bi(12,[],3,'left-msb') % Significant digit on left

The output is

t =

     0     1     1


tleft =

     1     1     0

See Also

Was this topic helpful?