Main Content

dec2bin

Convert decimal integer to its binary representation

描述

example

binStr = dec2bin(D)returns the binary, or base-2, representation of the decimal integerD. The output argumentbinStr是一个字符acter vector that represents binary digits using the characters0and1.

IfDis a numeric vector, matrix, or multidimensional array, thenbinStris a two-dimensional character array. Each row ofbinStrrepresents an element ofD.

example

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

Examples

collapse all

Convert a decimal number to a character vector that represents its binary value.

D = 23; binStr = dec2bin(D)
binStr = '10111'

Specify the minimum number of binary digits thatdec2binreturns. If you specify more digits are required, thendec2binpads the output.

D = 23; binStr = dec2bin(D,8)
binStr = '00010111'

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

binStr = dec2bin(D,1)
binStr = '10111'

Create a numeric array.

D = [1023 122 14];

To represent the elements ofDas binary values, use thedec2binfunction. Each row ofbinStrcorresponds to an element ofD.

binStr = dec2bin(D)
binStr =3x10 char array'1111111111' '0001111010' '0000001110'

Since all rows of a character array must have the same number of characters,dec2binpads some rows ofbinStr. For example, the number14can be represented by the binary digits'1110'. But to match the length of the first row ofbinStr,dec2binfunction pads the third row to'0000001110'.

Starting in R2020a, thedec2binfunction converts negative numbers using their two's complement binary values.

For example, these calls todec2binconvert negative numbers.

dec2bin(-1)
ans = '11111111'
dec2bin(-16)
ans = '11110000'

Input Arguments

collapse all

Input array, specified as a numeric array,chararray, or logical array.

  • IfDis an array of floating-point numbers, and any element ofDhas a fractional part, thendec2bintruncates it before conversion. For example,dec2binconverts both12and12.5to'1100'. The truncation is always to the nearest integer less than or equal to that element.

  • IfD是一个字符acter or logical array, thendec2bintreats the elements ofDas integers. However,dec2bintreats characters as their Unicode®values, so specifyingDas a character array is not recommended.

Since R2020a

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

Data Types:single|double|int8|int16|int32|int64|uint8|uint16|uint32|uint64|logical|char

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

  • IfDcan be represented with fewer thanminDigitsbinary digits, thendec2binpads the output.

    D >= 0

    Pads with leading zeros

    D < 0

    Pads with leading ones(since R2020b)

  • IfDis so large that it must be represented with more thanminDigitsdigits, thendec2binreturns the output with as many digits as required.

Tips

  • The output ofdec2binis the same whether your computer stores values in memory using big-endian or little-endian format. For more information on these formats, seeEndianness.

Extended Capabilities

Version History

Introduced before R2006a

expand all

Behavior changed in R2022a

Behavior changed in R2022a