dec2bin

Convert decimal number to character vector representing binary number

Description

example

str = dec2bin(d) returns the binary representation of symbolic number d as a character vector. d must be a nonnegative integer.

If d is a matrix or multidimensional array of symbolic numbers with N elements, dec2bin returns a character array with N rows. Each row of the output str corresponds to an element of d accessed with linear indexing.

example

str = dec2bin(d,n) returns a binary representation with at least n bits.

Examples

collapse all

Define a large integer ${2}^{60}$ as a symbolic number.

d = sym(2)^60
d = $1152921504606846976$

Convert the decimal number to binary representation.

str = dec2bin(d)
str =
'1000000000000000000000000000000000000000000000000000000000000'

Create a 2-by-2 symbolic matrix that contains integers in decimal representation.

d = [sym(2)^6 123; 54 11]
d =

$\left(\begin{array}{cc}64& 123\\ 54& 11\end{array}\right)$

Convert the integers to binary representation using dec2bin. dec2bin returns 4 rows of character vectors. Each row contains a 7-digit binary number.

str = dec2bin(d)
str = 4x7 char array
'1000000'
'0110110'
'1111011'
'0001011'

Return a binary representation with at least 8 digits by specifying the number of digits.

str = dec2bin(d,8)
str = 4x8 char array
'01000000'
'00110110'
'01111011'
'00001011'

Input Arguments

collapse all

Decimal number, specified as a symbolic number, vector, matrix, or array. d must be a nonnegative integer.

Example: sym([2 4])

Number of bits, specified as a scalar positive integer.

Example: 8