Engineering function

DEC2BIN function

Converts a decimal number to binary text, such as 10 to "1010". Works from -512 to 511; negatives come back as 10-bit two's complement.

Available in Excel 2007 and later (Excel 2003 needed the Analysis ToolPak add-in), Microsoft 365, Excel for the web, Excel for Mac

Excel 2007 and laterWorks in Google Sheets

Syntax

DEC2BIN(number, [places])
ArgumentWhat it means
numberRequiredThe decimal whole number to convert, from -512 to 511. Decimals are truncated.
placesOptionalThe minimum number of characters to return, padded with leading zeros. Ignored for negative numbers. If the result needs more characters than places, you get #NUM!.

Returns: Text containing the binary digits (0 and 1).

How DEC2BIN works

DEC2BIN turns an ordinary decimal number into its binary (base-2) form, the string of 0s and 1s a computer actually stores. Network engineers use it to look at subnet masks and IP octets bit by bit, programmers use it to check flag values, and teachers use it to show how counting in base 2 works. The result is text, so it keeps any leading zeros you ask for.

The function is limited to 10 bits. Positive numbers from 0 to 511 come back with no leading zeros unless you give places; places pads the result to a fixed width, which is what you want for an 8-bit byte. Negative numbers from -1 to -512 always come back as exactly 10 characters in two's-complement form, where the first bit is the sign: -1 is 1111111111. Anything outside -512 to 511 gives #NUM!.

If you need more than 10 bits, use BASE (Excel 2013 and later), which converts any whole number from 0 up to 2^53 into base 2 and also accepts a minimum length. To go back the other way, use BIN2DEC.

Examples

1Convert a number to binary

B2fx=DEC2BIN(A2)
AB
1Decimal
2101010

Result: 1010

10 is 8 + 2, so bits 3 and 1 are on: 1010. The result is text, which is why it sits on the left of the cell.

2Show an IP octet as 8 bits

B2fx=DEC2BIN(A2, 8)
AB
1Octet
219211000000

Result: 11000000

The places argument pads the result to 8 characters, the size of one octet. 192 = 128 + 64, so the first two bits are on: 11000000. Without places the result would be the same here, but for 5 it would be 101 instead of 00000101.

3A negative number in two's complement

B2fx=DEC2BIN(A2)
AB
1Decimal
2-1001110011100

Result: 1110011100

Negatives always return 10 characters. Excel adds 1024 (2^10) to -100 and converts 924, giving 1110011100. Feeding that back into BIN2DEC returns -100 again.

⬇Practice workbookExamples with live formulas + practice sheets · .xlsx · 15 KB

Common errors and how to fix them

ErrorWhy it happensFix
#NUM!The number is below -512 or above 511, or the result has more digits than places allows, or places is negative.Keep within the 10-bit range or switch to =BASE(number, 2, places) for larger positive numbers. Increase places if it is too small.
#VALUE!number or places is text that is not a number.Convert imported text with VALUE first.

Tips

  • The result is text. If you need to do arithmetic on it later, convert it back with BIN2DEC rather than treating the string as a number.
  • =BASE(1000, 2) returns 1111101000 for numbers that are too large for DEC2BIN; =BASE(5, 2, 8) pads like places does.
  • To test a single bit, you do not need the binary text at all: =BITAND(A2, 4)>0 tells you whether bit 2 (value 4) is set.
  • places is truncated, so 8.9 behaves as 8.

Questions

Why does DEC2BIN(-1) return ten 1s?

Excel stores negative binary numbers in 10-bit two's complement. -1 is 1024 − 1 = 1023, which is ten 1s in binary.

How do I convert numbers bigger than 511 to binary?

Use =BASE(number, 2). It accepts whole numbers from 0 to 2^53 but does not handle negatives.

Google Sheets

DEC2BIN also exists in Google Sheets and works the same way for everyday use, so the examples above work there too.

DEC2BIN in other languages

In a non-English version of Excel the function has a local name. Excel translates formulas automatically when a file is opened in another language.

LanguageFunction name
DanishDEC.TIL.BIN
DutchDEC.N.BIN
FinnishDESBIN
FrenchDECBIN
GermanDEZINBIN
HungarianDEC.BIN
ItalianDECIMALE.BINARIO
Norwegian (Bokmål)DESTILBIN
PolishDZIES.NA.DWÓJK
Portuguese (Brazil)DECABIN
Portuguese (Portugal)DECABIN
RussianДЕС.В.ДВ
SpanishDEC.A.BIN
SwedishDEC.TILL.BIN