The Google Sheets BITLSHIFT function shifts the bits of a number to the left by a specified amount, effectively multiplying the number by 2 raised to the shift amount. For example, =BITLSHIFT(4, 2) returns 16 because 4 in binary is 100, shifting left by 2 gives 10000 (16).
What Is BITLSHIFT and Where Does It Fit?
BITLSHIFT belongs to Google Sheets’ Engineering category, a set of functions designed for technical and mathematical work. It performs a bitwise left shift on a non-negative integer. Bitwise operations work directly on the binary representation of numbers, making them useful in fields like computer science, digital electronics, and low-level programming. In spreadsheets, BITLSHIFT is rarely used for everyday tasks, but it appears in data analysis involving flags, permissions, or compact integer encodings.
Syntax and Arguments Explained
The syntax is straightforward: BITLSHIFT(value, shift_amount). Both arguments are required. value must be a non-negative integer (whole number) that you want to shift. shift_amount is the number of places to shift left; it must be an integer and can be negative, which effectively shifts right (equivalent to dividing by powers of 2). For example, =BITLSHIFT(8, -1) returns 4. If either argument is not an integer, Google Sheets truncates it to the nearest integer. If value is negative, the function returns a #NUM! error. The maximum shift amount is 48; shifting beyond that returns a #NUM! error because the result exceeds the 48-bit limit that Google Sheets uses for bitwise operations.
How BITLSHIFT Works: A Step-by-Step Example
Let’s walk through a concrete example. Assume cell A1 contains the value 5 (binary 101) and cell B1 contains the shift amount 3. The formula =BITLSHIFT(A1, B1) returns 40. Here’s why: shifting 101 left by 3 places adds three zeros to the right, yielding 101000, which is 40 in decimal (32 + 8). In general, BITLSHIFT(value, shift) is equivalent to value * 2^shift, as long as the result stays within the 48-bit limit. This multiplication property is often the easiest way to understand the function’s effect without visualizing binary.
Common Use Cases and Practical Applications
While BITLSHIFT is niche, it has several real-world uses:
- Encoding multiple flags in one integer: If you have 8 boolean flags, you can pack them into a single number by assigning each flag a bit position (1, 2, 4, 8, …) and using BITLSHIFT to set them. For example, to set flags at positions 0 and 3, you’d use
=BITLSHIFT(1,0) + BITLSHIFT(1,3)which returns 9. - Fast multiplication by powers of two: In performance-sensitive calculations, shifting is faster than multiplication, though in a spreadsheet the difference is negligible. Still, you might see
=BITLSHIFT(A1, 4)used to multiply by 16. - Converting between binary and decimal: Combined with
BITANDorBITOR, you can extract individual bits. For instance, to check if bit 2 is set in a number in A1, use=BITAND(A1, BITLSHIFT(1,2))– if the result is 4, the bit is set.
Common Errors and How to Fix Them
You might encounter these errors when using BITLSHIFT:
#NUM!– Occurs ifvalueis negative, ifshift_amountis greater than 48, or if the result exceeds the 48-bit limit. For instance,=BITLSHIFT(1, 49)returns an error.#VALUE!– Occurs if either argument is non-numeric (e.g., text). Ensure both arguments are numbers.#N/A– Unlikely, but can appear if you reference a cell that contains an error.
To avoid errors, always use non-negative integers and keep shift amounts within 0–48. If you need to shift right, use a negative shift amount, but be aware that this is equivalent to floor division by powers of two, not rounding down in the usual sense. For example, =BITLSHIFT(7, -1) returns 3 (since 7 in binary is 111, shifting right gives 11 = 3), discarding the remainder.
BITLSHIFT vs. Other Bit Functions in Google Sheets
Google Sheets offers a suite of bitwise functions, each with a distinct role:
BITAND– Performs a bitwise AND, returning 1 for each bit position where both numbers have a 1.BITOR– Performs a bitwise OR, returning 1 where at least one number has a 1.BITXOR– Performs a bitwise XOR (exclusive OR), returning 1 where the bits differ.BITRSHIFT– The right-shift counterpart to BITLSHIFT, shifting bits to the right.
BITLSHIFT is unique in that it only moves bits left, which is equivalent to multiplication by powers of two. BITRSHIFT does the opposite. In contrast, BITAND and BITOR are used for masking and combining bits. A common pattern is to use BITLSHIFT to create a bitmask (e.g., =BITLSHIFT(1, 5) gives 32, which has only bit 5 set) and then use BITAND to test if that bit is set in another number.
BITLSHIFT in Excel vs. Google Sheets
Microsoft Excel also has a BITLSHIFT function with the same syntax and behavior. The main difference is availability: Excel’s BITLSHIFT is present in Excel 2013 and later, while Google Sheets includes it in its standard function list. In both, the 48-bit limit applies. If you’re working in Excel for the web, the function works the same as in Google Sheets. One subtle difference: in Excel, if you enter a negative shift amount, it performs a right shift, just like Google Sheets. However, Excel’s documentation notes that the result is always a signed 64-bit number, but the practical behavior is identical for non-negative values within the 48-bit range.
Frequently Asked Questions
Can BITLSHIFT be used to multiply by 2?
Yes, =BITLSHIFT(5, 1) returns 10, which is 5 times 2. Each shift to the left doubles the number, as long as the result doesn’t exceed the 48-bit limit. This is a quick way to multiply by powers of two without using the * operator.
What happens if I use a negative shift amount?
A negative shift amount shifts the bits to the right, effectively dividing by powers of two and discarding any remainder. For example, =BITLSHIFT(10, -1) returns 5, and =BITLSHIFT(9, -1) returns 4. This is equivalent to floor division by 2.
Why does BITLSHIFT return #NUM! for large shift amounts?
Google Sheets limits bitwise operations to 48 bits. If the shift amount is greater than 48, or if the result would require more than 48 bits, the function cannot represent the number and returns #NUM!. For example, =BITLSHIFT(1, 50) exceeds this limit.
How do I check if a specific bit is set using BITLSHIFT?
Use BITLSHIFT to create a mask for the bit position, then use BITAND. For instance, to check if bit 3 (value 8) is set in cell A1, use =BITAND(A1, BITLSHIFT(1, 3)). If the result is 8, the bit is set; if 0, it’s not.
Is BITLSHIFT available in older versions of Excel?
BITLSHIFT was introduced in Excel 2013. It is not available in Excel 2010 or earlier. In Google Sheets, it has been available since the function was added to the Engineering category, and it works in all current versions.
Can BITLSHIFT handle decimal numbers?
No, BITLSHIFT expects integer values. If you provide a decimal like 5.7, Google Sheets truncates it to 5 before shifting. The function does not perform floating-point bitwise operations; it works only on the integer part.
Related
- Google Sheets URL from Script? Get a Link in Apps Script
- Balance Charger: What It Is & How It Works
- Data Entry Jobs with No Experience: Real Answers
Try it: the Date Difference Calculator: days, months, years and weekdays between two dates, with the DATEDIF formulas.
More in How-to basics
- How to Add a Drop-Down List to an Excel Column Step by StepTo add a drop-down list to an Excel column, select the cells that should contain it, then choose…
- Balance Charger LiPo: What It Is & How to Use OneA balance charger for LiPo batteries is a charger that monitors and equalizes the voltage of each individual…
- How to Split an Excel Row in HalfTo split a row’s contents between two rows in Excel, insert a blank row beneath it, then cut…
- How to Find Duplicate Lines in Excel Without Deleting ThemTo find duplicate lines (rows) in Excel without deleting anything, select the data rows and create a formula-based…
- Day Count in Excel: Calculate Days Between DatesTo get the number of elapsed calendar days between two dates in Excel, subtract the earlier date from…
- Employee Break Schedule Template in Excel: Setup GuideAn employee break schedule template in Excel is a roster that records each worker’s shift and planned break…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.