The BITXOR function in Google Sheets performs a bitwise exclusive OR (XOR) operation on two numbers, returning a decimal result. Its syntax is BITXOR(value1, value2), and it is part of the Engineering category of functions. In plain terms, it compares the binary representations of two numbers and returns a number where each bit is 1 if exactly one of the corresponding bits is 1, and 0 otherwise.
What Is Bitwise XOR and Why Does It Matter?
Bitwise operations work on the binary (base-2) form of numbers. Every decimal number can be written as a sequence of 0s and 1s. For example, the decimal number 5 is 101 in binary, and 3 is 011. XOR (exclusive OR) compares each bit position: the result bit is 1 only when the two input bits differ. So 5 XOR 3 becomes:
- 5 = 101
- 3 = 011
- Result = 110 (binary) = 6 (decimal)
In Google Sheets, =BITXOR(5, 3) returns 6. This function is rarely used in everyday spreadsheets, but it appears in engineering, computer science, cryptography, and data validation contexts. For instance, XOR is used in checksum algorithms, toggling flags, and simple encryption schemes.
How the BITXOR Function Works
The function takes exactly two arguments, both of which must be numeric. Google Sheets converts each argument to a 32-bit signed integer internally. The result is also a signed integer, meaning it can be negative if the most significant bit (bit 31) is set. The syntax is straightforward:
=BITXOR(value1, value2)
Here are the key rules:
- Both arguments must be numbers or references to cells containing numbers.
- If an argument is not numeric, the function returns a
#VALUE!error. - If an argument is negative, the function returns a
#NUM!error because negative numbers are not allowed. - If either argument is greater than 2^32 – 1 (4,294,967,295), the function returns a
#NUM!error.
For example, with cell A1 containing 12 and B1 containing 10, =BITXOR(A1, B1) returns 6 because 12 (1100) XOR 10 (1010) = 0110 (6).
Practical Examples of BITXOR in Action
Let’s walk through a few real scenarios. Suppose you are tracking permission flags where each bit represents a feature (bit 0 = read, bit 1 = write, bit 2 = execute). You have two users with permissions 5 (101 = read + execute) and 3 (011 = write + execute). To find which permissions are held by exactly one user, use:
=BITXOR(5, 3) → returns 6 (110), meaning write and read are exclusive, while execute is shared.
Another example: you want to toggle a bit in a number. If you have a value 8 (1000) and you XOR it with 1, you get 9 (1001). Doing it again returns 8. This property makes XOR useful for toggling settings.
For a more spreadsheet-like use, consider a list of product codes in column A and a mask in B1. You could compute =BITXOR(A2, $B$1) for each row to apply a simple obfuscation.
Choosing Between BITXOR, BITAND, and BITOR
Google Sheets offers three bitwise functions: BITAND, BITOR, and BITXOR. They differ in the logic applied to each bit:
- BITAND returns 1 only when both bits are 1. Example:
=BITAND(5,3)→ 1 (001). - BITOR returns 1 when at least one bit is 1. Example:
=BITOR(5,3)→ 7 (111). - BITXOR returns 1 when exactly one bit is 1. Example:
=BITXOR(5,3)→ 6 (110).
Choose BITXOR when you need to detect differences or toggle bits. Use BITAND to test if a specific bit is set (like checking a flag), and BITOR to combine flags. If you need to shift bits, look at BITLSHIFT and BITRSHIFT.
Common Errors and How to Fix Them
Users often encounter a few predictable issues with BITXOR:
- #VALUE! error: One of the arguments is text or a blank cell. Ensure both cells contain numbers, not strings. If you have text numbers, use
VALUE()to convert them. - #NUM! error: An argument is negative or exceeds the 32-bit limit. Check for negative values or numbers over 4,294,967,295. Use
ABS()if you need the absolute value. - Wrong result due to decimal inputs: The function truncates decimals to integers. For example,
=BITXOR(5.7, 3.2)treats them as 5 and 3, returning 6. If you need to round first, useROUND().
Also note that BITXOR works only with two values. If you need to XOR more than two numbers, you cannot chain them directly; you must nest: =BITXOR(BITXOR(A1, B1), C1).
Comparing BITXOR in Google Sheets and Excel
Excel also has a BITXOR function with identical syntax and behavior. The main difference is that Excel’s version is available in Excel 2013 and later, while Google Sheets has it built-in from the start. In both, the function is in the Engineering category. The error handling and 32-bit limit are the same. If you are migrating a spreadsheet, the formulas will work without changes. One subtle difference: in Excel, you might need to enable the Analysis ToolPak add-in for older versions, but in modern Excel (2016 and later), it is native.
Frequently Asked Questions
Can I use BITXOR with more than two numbers?
No, the function only accepts two arguments. To XOR multiple numbers, you must nest calls, like =BITXOR(BITXOR(A1, B1), C1). There is no built-in array version.
Why does BITXOR return a negative number?
Because the result is a 32-bit signed integer. If the most significant bit (bit 31) becomes 1, the result is interpreted as negative. For example, =BITXOR(2147483647, 2147483647) returns 0, but XORing numbers that produce a high bit can yield negative values.
Is BITXOR the same as logical XOR?
No. Logical XOR (often written as XOR() in formulas) returns TRUE or FALSE based on boolean values. BITXOR works on numbers and returns a numeric result. For example, =XOR(TRUE, FALSE) returns TRUE, while =BITXOR(1,0) returns 1.
Can I use BITXOR on cell ranges?
No, it only takes single values. If you have a range, you would need to use an array formula or a helper column, but there is no direct range support.
What is the maximum number BITXOR can handle?
Any non-negative integer up to 4,294,967,295 (2^32 – 1). Values above that produce a #NUM! error.
Does BITXOR work with decimal numbers?
It truncates decimals to integers first. So =BITXOR(5.9, 3.1) is equivalent to =BITXOR(5,3). If you need to round, do it explicitly.
Alternatives and Related Functions
If BITXOR doesn’t fit your need, consider these alternatives:
BITANDandBITORfor other bitwise logic.BITLSHIFTandBITRSHIFTto shift bits left or right.DEC2BINandBIN2DECto convert between decimal and binary for manual inspection.- For boolean XOR, use
XOR()which works with TRUE/FALSE values.
In some cases, you might use a custom formula with LAMBDA to XOR a list of numbers, but that requires Google Sheets’ newer functions and is more complex. For most users, the built-in BITXOR is sufficient.
Related
- Google Sheets TIME Function: Syntax & Examples
- Balance Sheet Template QuickBooks: How to Use & Customize
- Calculator 10 Digit Casio: Models, Features, and Buying Guide
Try it: the Date Difference Calculator: days, months, years and weekdays between two dates, with the DATEDIF formulas.
More in How-to basics
- Budget Spreadsheet South Africa: Build a Rand-Based PlanA useful South African budget spreadsheet is a monthly Excel workbook that records income and spending in rand…
- Excel CSV vs ODS: Which Export Format to ChooseChoose CSV when you need a simple, widely compatible table of data; choose ODS when you want to…
- Freeze 2 Panes in Excel: What Works and What Doesn’tTo freeze two parts of a worksheet at once in Excel, select the cell just below the rows…
- Balance Charger for RC: What It Is and How to Use OneA balance charger is a battery charger that monitors and equalizes the voltage of each cell in a…
- How to Import Excel Data Into Google Earth on DesktopTo import Excel data into Google Earth, save the worksheet as a CSV file, then open it in…
- Use Power Query to Merge Excel Files: A Practical GuideIn Excel for Microsoft 365 on Windows, go to Data > Get Data > From File > From…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.