Main Content

dec2bin

Convert decimal integer to its binary representation for fi objects

Since R2021b

Description

binStr = dec2bin(D) returns the binary, or base-2, representation of the decimal integer D. The output argument binStr is a character vector that represents binary digits using the characters 0 and 1.

example

binStr = dec2bin(D,minDigits) returns a binary representation with no fewer than minDigits digits.

Tip

dec2bin returns the binary representation of the real-world value of the fi object D. To obtain the binary representation of the stored integer value, use bin instead.

example

Examples

collapse all

Convert a decimal number stored as a fi object to a character vector that represents its binary value.

D1 = fi(2748);
D2 = fi(251);
binStr1 = dec2bin(D1)
binStr2 = dec2bin(D2)
binStr1 =

    '101010111100'


binStr2 =

    '11111011'

The dec2bin function converts negative numbers using their two's complement binary values.

D3 = fi(-5);
binStr3 = dec2bin(D3)
binStr3 =

    '11111011'

Convert the decimal number stored as a fi object to binary representation. Specify the minimum number of binary digits that dec2bin returns. If you specify more digits than are required, then dec2bin pads the output.

D = fi(2748);
binStr = dec2bin(D,16)
binStr =

    '0000101010111100'

If you specify fewer digits, then dec2bin still returns as many binary digits as required to represent the input number.

binStr = dec2bin(D,8)
binStr =

    '101010111100'

Create a numeric fi array.

D = fi([1023 122 14]);

To represent the elements of D as binary values, use the dec2bin function. Each row of binStr corresponds to an element of D.

binStr = dec2bin(D)
binStr =

  3×10 char array

    '1111111111'
    '0001111010'
    '0000001110'

Convert the upper and lower bound of a signed fi object with 100-bit word length.

binStr = dec2bin([lowerbound(fi([],1,100,0)),...
    upperbound(fi([],1,100,0))])
binStr =

  2×100 char array

    '1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000'
    '0111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111'

Input Arguments

collapse all

Input array, specified as a numeric fi array.

  • D must contain finite integers. If any element of D has a fractional part, then dec2bin truncates it before conversion. For example, dec2bin converts both fi(12) and fi(12.5) to '1100'. The truncation is always to the nearest integer less than or equal to that element.

  • D can include negative numbers. The function converts negative numbers using their two's complement binary values.

Data Types: fi

Minimum number of digits in the output, specified as a positive integer.

  • If D can be represented with fewer than minDigits binary digits, then dec2bin pads the output.

  • If D is so large that it must be represented with more than minDigits digits, then dec2bin returns the output with as many digits as required.

Extended Capabilities

Version History

Introduced in R2021b

See Also

| | | | | | |