de2bi

Convert decimal numbers to binary vectors

Description

example

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.

example

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 .

example

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

Examples

collapse all

Convert decimals 1 through 10 into their equivalent binary representations.

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

     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 to binary numbers. Each value is represented by a four-element row.

b = de2bi([3 9])
b = 2×4

     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 = 2×5

     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 = 6×3

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

This example shows how to convert decimals to binary numbers in their base-2 equivalents.

d_array = [1 2 3 4];

Convert the decimal array to binary by using the de2bi function. Specify that the most significant digit is the leftmost element and set the number of desired columns to 5. The output becomes a 4-by-5 matrix where each row corresponds to a decimal value from the input. Because the largest decimal value in d_array can be expressed in 3 columns, the de2bi pads the matrix with two extra zero columns at the specified most-significant bit side. If you specify too few columns, the conversion will fail.

b_array = de2bi(d_array,5,'left-msb')
b_array = 4×5

     0     0     0     0     1
     0     0     0     1     0
     0     0     0     1     1
     0     0     1     0     0

b_array = de2bi(d_array,5,'right-msb')
b_array = 4×5

     1     0     0     0     0
     0     1     0     0     0
     1     1     0     0     0
     0     0     1     0     0

If you do not specify a number of columns, the number of columns is exactly what is needed to express the largest decimal of the input.

b_array = de2bi(d_array,'left-msb')
b_array = 4×3

     0     0     1
     0     1     0
     0     1     1
     1     0     0

The output rows for specifying a leftmost-significant bit correspond to:

1=0(22)+0(21)+1(20)

2=0(22)+1(21)+0(20)

3=0(22)+1(21)+1(20)

4=1(22)+0(21)+0(20)

b_array = de2bi(d_array,'right-msb')
b_array = 4×3

     1     0     0
     0     1     0
     1     1     0
     0     0     1

The output rows for specifying a rightmost-significant bit correspond to:

1=1(20)+0(21)+0(22)

2=0(20)+1(21)+0(22)

3=1(20)+1(21)+0(22)

4=0(20)+0(21)+1(22)

Input Arguments

collapse all

Decimal input, specified as a nonnegative integer, vector, or matrix. If d is a matrix, it is treated like the column vector d(:).

Note

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

Data Types: double | single | integer | fi

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

Data Types: double | single

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

  • If d is a 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(:).

Data Types: double | single

MSB flag, specified as 'right-msb' or 'left-msb'.

  • 'right-msb' –– Indicates the right (or last) column of the binary output, b, as the most significant bit (or highest-order digit).

  • 'left-msb' –– Indicates the left (or first) column of the binary output, b, as the most significant bit (or highest-order digit).

Data Types: char | string

Output Arguments

collapse all

Binary representation of d, returned as a row vector or matrix. The output is of the same data type as the input.

Extended Capabilities

C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.

See Also

Introduced before R2006a