Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use Bitwise Operators during webdevelopment?

Although I grasp the concept of Bitwise Operators, I can't say that I have come across many use cases during the webdevelopment process at which I had to resort to using Bitwise Operators.

  • Do you use Bitwise Operators?
  • Why do you use them?
  • What are some example use cases?

Please remember that this question is specifically intended for use of Bitwise Operators in web languages.

like image 654
Aron Rotteveel Avatar asked Nov 04 '08 06:11

Aron Rotteveel


People also ask

When would you use bitwise operators?

Examples of uses of bitwise operations include encryption, compression, graphics, communications over ports/sockets, embedded systems programming and finite state machines. A bitwise operator works with the binary representation of a number rather than that number's value.

Why do we need bitwise operators in Java?

Bitwise operators are used to performing the manipulation of individual bits of a number. They can be used with any integral type (char, short, int, etc.). They are used when performing update and query operations of the Binary indexed trees.

Is it necessary to learn bitwise operators?

They are really only needed for things like hardware programming in drivers and low level network programming. They are usually used to pack a lot of data into a small number of bits, a proceeding made unnecessary for most user space programming on modern machines.

Why are bitwise operations useful?

Bitwise operators are a great way to make very efficient use of space when representing data. Imagine the following situation: You're a game developer for a computer game and you want to store players' mouse positions periodically while they play the game.


2 Answers

I'm going to be more explicit here because I think bitwise masks are a great tool that should be in any devs belt. I'm going to try to expand on the answers above. First, an example of using an integer to maintain state flags (common usage):

// These are my masks private static final int MASK_DID_HOMEWORK  = 0x0001; private static final int MASK_ATE_DINNER    = 0x0002; private static final int MASK_SLEPT_WELL    = 0x0004;   // This is my current state private int m_nCurState; 

To set my state, I use the bitwise OR operator:

// Set state for'ate dinner' and 'slept well' to 'on' m_nCurState = m_nCurState | (MASK_ATE_DINNER | MASK_SLEPT_WELL); 

Notice how I 'or' my current state in with the states that I want to turn 'on'. Who knows what my current state is and I don't want to blow it away.

To unset my state, I use the bitwise AND operator with the complement operator:

// Turn off the 'ate dinner' flag m_nCurState = (m_nCurState & ~MASK_ATE_DINNER); 

To check my current state, I use the AND operator:

// Check if I did my homework if (0 != (m_nCurState & MASK_DID_HOMEWORK)) {     // yep } else {      // nope... } 

Why do I think this is interesting? Say I'm designing an interface that sets my state. I could write a method that accepts three booleans:

void setState( boolean bDidHomework, boolean bAteDinner, boolean bSleptWell); 

Or, I could use a single number to represent all three states and pass a single value:

void setState( int nStateBits); 

If you choose the second pattern you'll be very happy when decide to add another state - you won't have to break existing impls of your interface.

My two cents. Thanks.

like image 155
tyler Avatar answered Sep 20 '22 04:09

tyler


My main use for bitwise operators could be relevant anywhere - representing a set of flags. For instance, you might have an integer in the database representing a set of security permissions for a user, and in your web app you would have to check those before continuing.

Those tend to only require & and | - e.g.

if ((permissions & Permission.CreateUser) != 0) {     ... } 

or

Permission requiredPermission = Permission.CreateUser                                 | Permission.ChangePassword; 

Bit shifting operators are less useful in "business" applications in my experience.

like image 33
Jon Skeet Avatar answered Sep 23 '22 04:09

Jon Skeet