Engineering function

BITAND function

Returns a bitwise AND of two numbers: only the bits that are 1 in both numbers stay on. Great for testing flags and masks.

Available in Excel 2013 and later, Microsoft 365, Excel for the web, Excel for Mac 2016 and later

Excel 2013 and laterWorks in Google Sheets

Syntax

BITAND(number1, number2)
ArgumentWhat it means
number1RequiredA whole number from 0 to 2^48 − 1.
number2RequiredA second whole number from 0 to 2^48 − 1.

Returns: A decimal number whose binary bits are 1 only where both inputs have a 1.

How BITAND works

BITAND compares two numbers bit by bit and keeps a bit only when it is switched on in both. Its main real-world use is checking flags: many systems pack several yes/no settings into one number, where 1 means "email allowed", 2 means "SMS allowed", 4 means "newsletter" and so on. BITAND(settings, 4) returns 4 if the newsletter bit is on and 0 if it is off.

Both inputs are read as binary behind the scenes; you give and get ordinary decimal numbers. They must be whole numbers from 0 up to 2^48 − 1; a negative number, a decimal or a larger value returns #NUM!. If you want to see the bits, wrap the inputs and the result in DEC2BIN (for values up to 511) or BASE.

Related functions: BITOR switches bits on, BITXOR flips them, and BITLSHIFT / BITRSHIFT move them left or right.

Examples

1AND of two numbers

C2fx=BITAND(A2, B2)
ABC
1Number 1Number 2
213259

Result: 9

13 is 01101 and 25 is 11001. Only the bits worth 8 and 1 are on in both, so the result is 8 + 1 = 9.

2Does a customer allow the newsletter?

C2fx=BITAND(B2, 4)>0
ABC
1CustomerPermission flags
2Ana13TRUE
3Ben3

Result: TRUE

Ana's flags are 13 = 8 + 4 + 1, so the 4 bit is on and BITAND returns 4, which is greater than 0: TRUE. Copied down for Ben (3 = 2 + 1) it returns FALSE.

3Keep the lowest byte

B2fx=BITAND(A2, 255)
AB
1Value
21000232

Result: 232

255 is eight 1s, so ANDing with it keeps only the last 8 bits: 1000 − 3×256 = 232. This is the standard way to extract one byte from a packed number.

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

Common errors and how to fix them

ErrorWhy it happensFix
#NUM!A number is negative, has decimals, or is 2^48 (281,474,976,710,656) or more.Use whole numbers from 0 to 281,474,976,710,655. Wrap calculated inputs in INT or ROUND.
#VALUE!An argument is text that is not a number.Convert text such as imported codes with VALUE or HEX2DEC.

Tips

  • Test a single bit with =BITAND(value, 2^n)>0, where n is the bit position counting from 0.
  • BITAND works on numbers only. If your flags are stored as binary text, convert them with BIN2DEC first.
  • Before Excel 2013, people used =MOD(INT(value/2^n),2) to test bit n; that still works and also handles numbers above 2^48.

Google Sheets

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

BITAND 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
DanishBITOG
DutchBIT.EN
FinnishBITTI.JA
FrenchBITET
GermanBITUND
HungarianBIT.ÉS
Norwegian (Bokmål)BITOG
Portuguese (Portugal)BIT.E
RussianБИТ.И
SpanishBIT.Y
SwedishBITOCH
TurkishBİTVE