BITAND


Finds the bitwise ‘AND’ value of two numbers and returns it.

Syntax:

BITAND(numberOne, numberTwo)


numberOne and numberTwo must be a decimal and should be 0 or higher.


Example:

If numberOne contains 1 and numberTwo contains 2:

BITAND(1, 2)

returns 0


numberOne:


numberTwo:


Result:

0

Application:

The BITAND function is often used in computer science and data analysis for tasks involving bitwise operations, particularly when dealing with flags or permissions. A great application of using the BITAND function is when someone is managing a system where users have different levels of access.


Let's imagine a small company with a simple file sharing system. Each user has a set of permissions, which we can represent as a single number using bit flags. Each bit in this number corresponds to a specific permission.


Here's how we can set it up:


  • Permission 1 (Bit 0): Read Access = 1
  • Permission 2 (Bit 1): Write Access = 2
  • Permission 3 (Bit 2): Delete Access = 4
  • Permission 4 (Bit 3): Administrator Access = 8


A user's total permissions are the sum of the permissions they have. For example:


  • A user with Read and Write access would have a total permission value of 1 + 2 = 3.
  • A user with Read and Delete access would have a total permission value of 1 + 4 = 5.
  • An administrator (who has all permissions) would have a value of 1 + 2 + 4 + 8 = 15.

The Problem

Now, let's say we want to check if a user has a specific permission, for instance, "Write Access." We can use the BITAND function for this. BITAND compares the bits of two numbers and returns a new number with bits set to 1 only if both corresponding bits in the original numbers are 1.


The logic is: If BITAND(User's_Permission_Value, Permission_to_Check_Value) is equal to Permission_to_Check_Value, then the user has that permission.

Example Table

Let's set up a simple table to demonstrate this.

User ID

User Name

Permission Value

A
B
C
1
101
Alice
3
2
102
Bob
5
3
103
Charlie
15
4
104
David
2

Now, let's add a column to check for "Write Access" (which has a value of 2).


The formula we would use in cell D1 (for Alice) is:


IF(BITAND(C1, 2) = 2, "Yes", "No")


Let's break down what's happening for each user:


  • Alice (Permission Value = 3):
    • In binary: 3 is 0011 and 2 is 0010.
    • BITAND(3, 2) compares the bits:
      0011 (Alice's permissions)
      & 0010 (Write access flag)
      -----
      0010 (Result is 2)
    • Since the result 2 is equal to the permission value 2, the formula returns "Yes".


  • Bob (Permission Value = 5):
    • In binary: 5 is 0101 and 2 is 0010.
    • BITAND(5, 2) compares the bits:
      0101 (Bob's permissions)
      & 0010 (Write access flag)
      -----
      0000 (Result is 0)
    • The result 0 is not equal to 2, so the formula returns "No".


  • Charlie (Permission Value = 15):
    • BITAND(15, 2) results in 2, so the formula returns "Yes".


  • David (Permission Value = 2):
    • BITAND(2, 2) results in 2, so the formula returns "Yes".


Here's what the final table would look like:

User ID

User Name

Permission Value

Has Write Access?

A
B
C
D
1
101
Alice
3
Yes
2
102
Bob
5
No
3
103
Charlie
15
Yes
4
104
David
2
Yes

This example shows how BITAND can be a powerful tool for efficiently checking for the presence of specific attributes or flags stored in a single numerical value, avoiding the need for multiple columns for each permission.





This page is protected by Google reCAPTCHA. Privacy - Terms.
 
Built using Zapof