Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

When to use + and | operator?

 
sarada bokka
Ranch Hand
Posts: 80
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20532
54
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Firefox Browser Java Spring
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sarada bokka wrote:Hi All,

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?
 
sarada bokka
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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
Pie
Posts: 10417
63
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: 12127
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10417
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic