Documentation

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 in which each row is the binary form of the corresponding element in d.

b = de2bi(d,n) has an output with n columns.

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

b = de2bi(d,[],p) specifies the base, p .

b = de2bi(d,...,flg) uses the string flg to determine whether the first column of b contains the lowest-order or highest-order digits.

Input Arguments

collapse all

d — Decimal inputscalar | vector | matrix

Decimal input which can be a scalar, vector, or matrix. Specify elements as nonnegative integers. If d is a matrix, it is treated like the column vector d(:).

Example: 4

Example: [10; 5]

    Note:   To ensure an accurate conversion, d must be less than or equal to 252.

n — Number of output columnspositive integer scalar

The number of output columns specified as a positive scalar. If necessary, the binary representation of d is padded with extra zeros.

Example: 3

p — Basepositive integer scalar

An integer that specifies the base of the output b. Specify as an integer greater than or equal to 2. The first column of b is the lowest base-p digit. The output is padded with extra zeros if necessary so that it has n columns. If d is a nonnegative decimal vector, the output b is a matrix in which each row is the base-p form of the corresponding element in d. If d is a matrix, de2bi treats it like the vector d(:).

Example: 8

flg — MSB flag'right-msb' | 'left-msb'

String that determines whether the first column of b contains the lowest-order or highest-order digits. If omitted, de2bi assumes 'right-msb'.

Output Arguments

collapse all

b — Binary outputvector | matrix

Binary representation of d in the form of a row vector or matrix.

Convert Decimals to Binary Numbers

Convert decimals 1 through 10 into their equivalent binary representations.

d = (1:10)';
b = de2bi(d);
[d b]
ans =

     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

Convert 3 and 9 into binary numbers. Each value is represented by a four-element row.

b = de2bi([3 9])
b =

     1     1     0     0
     1     0     0     1

Repeat the conversion with the number of columns set to 5. The output is now padded with zeros in the fifth column.

bb = de2bi([3 9],5)
bb =

     1     1     0     0     0
     1     0     0     1     0

Convert the decimals 1 through 6 to their base 3 equivalents. Set the leftmost bit as the most significant digit.

d = (1:6)';
t = de2bi(d,[],3,'left-msb');
[d t]
ans =

     1     0     1
     2     0     2
     3     1     0
     4     1     1
     5     1     2
     6     2     0

See Also

Introduced before R2006a

Was this topic helpful?