Win a copy of Functional Reactive Programming this week in the Other Languages forum!

# When to use + and | operator?

Ranch Hand
Posts: 80
• 1
Hi All,

In my recent company, when adding two varaiables for all the integer type data they use bit wise operator

ex: long i = 10 | 20;

what makes the difference long i= 10+ 20;?

I am assuming that bitwise operator is used because of performance and memory?

Please correct me if my understanding is wrong?

Rob Spoor
Sheriff
Posts: 20669
65
• 1
10 is in binary 0000 0000 0000 0000 0000 0000 0000 1010.
20 is in binary 0000 0000 0000 0000 0000 0000 0001 0100.

10 | 20 will combine the above to on a bit-level, returning a 1 bit if either number has a 1 bit. So it will be 0000 0000 0000 0000 0000 0000 0001 1110. This happens to be 30 as well. If you take as examples 12 and 20 you get the following:
12 = 0000 0000 0000 0000 0000 0000 0000 1100
20 = 0000 0000 0000 0000 0000 0000 0001 0100
28 = 0000 0000 0000 0000 0000 0000 0001 1100

Saifuddin Merchant
Ranch Hand
Posts: 607
• 3

In my recent company, when adding two varaiables for all the integer type data they use bit wise operator

Sounds horrible. Why would anyone do that for addition? As Rob Spoor that's wrong. Even if it wasn't it would have been an horrible optimization.

Are you sure the code was trying to perform addition? Maybe it was for some other purpose that you that you mistook for addition?

Ranch Hand
Posts: 80
@Sam Mercs

What you said is right it is horrible optimization.

I just clarified with my team and they said to exclude such type of coding in the upcoming releases :-)

Winston Gutkowski
Bartender
Posts: 10527
64
• 1
sarada bokka wrote:What you said is right it is horrible optimization.
I just clarified with my team and they said to exclude such type of coding in the upcoming releases :-)

Well, I suppose it's better to see the light late than never; but in answer to your original question:
you use '+' when you want to add two values together and you use '|' when you want to do a bitwise OR - and the two are NOT the same.

You might want to read the quote at the bottom of my post too.

Winston

Saifuddin Merchant
Ranch Hand
Posts: 607
Honestly it seems strange to me that code is working, surely it would have raised a few issues till now if its in production. There would be very few values for which + and | would yield same results.
If the actual code is int x = 10 | 20 - I don't see an intent was optimization either. Surely writing x = 30 would be more optimized.

So either the code was written as a practical joke or could be a Boolean Flags and Standard Boolean Bitsets pattern that are sometimes used in java.
http://onjava.com/onjava/2005/02/02/bitsets.html

Difficult to say what was the intent without looking at the actual code ...

fred rosenberger
lowercase baba
Bartender
Posts: 12202
35
At my last job, we had a legacy system that used bits as various status flags for library books. a book could be checked out, damaged, have a reservation on it, etc. So if a book had a status of 3, it was both checked out and damaged. When someone placed a reservation on it, we would do a bitwise-or of its current status with 'reservation' to set the new bit.

Winston Gutkowski
Bartender
Posts: 10527
64
fred rosenberger wrote:At my last job, we had a legacy system that used bits as various status flags for library books.
Not just legacy my son. EnumSet does something very similar, it just doesn't tell you. And that's the real lesson.

Winston