Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When should I use a bitwise operator?

Tags:

I read the following Stack Overflow questions, and I understand the differences between bitwise and logical.

  • Difference between & and && in PHP

  • Reference - What does this symbol mean in PHP?

However, none of them explains when I should use bitwise or logical.

When should I use bitwise operators rather than logical ones and vice versa?

In which situation do I need to compare bit by bit?

I am not asking about the differences, but I am asking the situation when you need to use bitwise operators.

like image 745
shin Avatar asked Jan 16 '11 14:01

shin


People also ask

Are bitwise operations necessary?

Bitwise operations are worth studying because they have many applications. It is not their main use to substitute arithmetic operations. Cryptography, computer graphics, hash functions, compression algorithms, and network protocols are just some examples where bitwise operations are extremely useful.

Why we use bitwise operators in C?

Binary AND Operator copies a bit to the result if it exists in both operands. Binary OR Operator copies a bit if it exists in either operand. Binary XOR Operator copies the bit if it is set in one operand but not both.

What is use of Bitwise Operators Explain with examples?

Bitwise operations A bitwise operation operates on two-bit patterns of equal lengths by positionally matching their individual bits. For example, a logical AND (&) of each bit pair results in a 1 if both the first AND second bits are 1. If only one bit is a 1, the result is 0.

What is use of & bitwise operator?

The bitwise AND operator ( & ) compares each bit of the first operand to the corresponding bit of the second operand. If both bits are 1, the corresponding result bit is set to 1. Otherwise, the corresponding result bit is set to 0. Both operands to the bitwise AND operator must have integral types.


2 Answers

Bitwise is useful for things in PHP just like anything else.

How about a value that can have multiple states turned on at the same time?

<?php  // since we're setting constant values in base10 we must progressively double // them since bitwise operations work in base2. you'll see why when we output // these as binary values below. const STATE_FOO = 1; const STATE_BAR = 2; const STATE_FEZ = 4; const STATE_BAZ = 8;  // show base2 values of the above constants echo sprintf("STATE_FOO's base2 value is %08d\n", decbin(STATE_FOO)); echo sprintf("STATE_BAR's base2 value is %08d\n", decbin(STATE_BAR)); echo sprintf("STATE_FEZ's base2 value is %08d\n", decbin(STATE_FEZ)); echo sprintf("STATE_BAZ's base2 value is %08d\n\n", decbin(STATE_BAZ));  // set state to FOO and FEZ $state = STATE_FOO | STATE_FEZ;  echo sprintf("base10 value of \$state is %s\n", $state); echo sprintf("base2 value of \$state is %08d\n", decbin($state)); echo sprintf("Does \$state include FOO state? %s\n", (bool)($state & STATE_FOO)); echo sprintf("Does \$state include BAR state? %s\n", (bool)($state & STATE_BAR)); echo sprintf("Does \$state include FEZ state? %s\n", (bool)($state & STATE_FEZ)); echo sprintf("Does \$state include BAZ state? %s\n", (bool)($state & STATE_BAZ)); echo sprintf("Is state equivalent to FOO and FEZ states? %s\n", ($state == (STATE_FOO | STATE_FEZ))); 

Output:

STATE_FOO's base2 value is 00000001 STATE_BAR's base2 value is 00000010 STATE_FEZ's base2 value is 00000100 STATE_BAZ's base2 value is 00001000  base10 value of $state is 5 base2 value of $state is 00000101 Does $state include FOO state? 1 Does $state include BAR state? Does $state include FEZ state? 1 Does $state include BAZ state? Is state equivalent to FOO and FEZ states? 1 
like image 132
John Pancoast Avatar answered Oct 03 '22 06:10

John Pancoast


Forget what is already in your head.

OK, now say you have some different roles: admin, user, and guest.

and some different permissions: read, write and delete

Let's create some bitmasks for permissions and roles. A bitmask is a sequence of bits that can be used to manipulate or read some kind of flags. As shown below:

// flags                                bitmasks $read = 1;                              // 0001 $write = 2;                             // 0010 $delete = 4;                            // 0100  $admin = $read | $write | $delete;      // 0001 | 0010 | 0100 => 0111 $user = $read | $write;                 // 0001 | 0010 => 0011  $guest = $read;                         // 0001 => 0001 

Notice 1, 2, 4. This must be raised as double. Otherwise, it might give you some awkward results.

Forget about the things commented. Those are just sequence of bits (or bitmasks) for individual permissions and roles.

Now let's create a handy function which may be used to check a specific permission for a specific role.

function isAllowed($role, $permissison) {     return $role & $permissison ? true : false; } 

We are done. Let's check the $delete permission for all 3 roles:

var_dump(isAllowed($admin, $delete));  // bool(true) var_dump(isAllowed($user, $delete));   // bool(false) var_dump(isAllowed($guest, $delete));  // bool(false) 

So why is bitwise operation? In a word, bitwise operation is more faster, concise and maintainable. Otherwise, using bitwise operation is always efficient for complex applications.

like image 32
unclexo Avatar answered Oct 03 '22 06:10

unclexo