dec2bin

Converts a nonnegative integer to binary text.

Category: Language Fundamentals > Data Types

Syntax

b = dec2bin(d)

Binary digits of d.

b = dec2bin(d, len)

Pads with zeros to at least len digits.

Description

With a vector input every row has the width needed by the largest value, so dec2bin([1 5 10]) is a 3-by-4 char matrix. The result is text; to do arithmetic on bits use bitand, bitor and bitshift on the numbers, or convert back with bin2dec.

Input arguments

d
Nonnegative integers up to flintmax.
len
Minimum number of digits.

Output arguments

b
Char array of 0 and 1, one row per element, padded with leading zeros to the same length.

Example

Widths and padding

dec2bin(5)
dec2bin(5, 8)
dec2bin([1 5 10])
Output
ans = '101'
ans = '00000101'
ans = 3×4 char array

    '0001'
    '0101'
    '1010'

See also

bin2dec dec2hex dec2base bitand