File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes When to use + and | operator? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "When to use + and | operator?" Watch "When to use + and | operator?" New topic
Author

When to use + and | operator?

sarada bokka
Ranch Hand

Joined: Jan 13, 2010
Posts: 80
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

Joined: Oct 27, 2005
Posts: 19785
    
  20

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


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Saifuddin Merchant
Ranch Hand

Joined: Feb 08, 2009
Posts: 606

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?


Cheers - Sam.
Twisters - The new age Java Quiz || My Blog
sarada bokka
Ranch Hand

Joined: Jan 13, 2010
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

Joined: Mar 17, 2011
Posts: 8427
    
  23

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


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Saifuddin Merchant
Ranch Hand

Joined: Feb 08, 2009
Posts: 606

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

Joined: Oct 02, 2003
Posts: 11499
    
  16

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8427
    
  23

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: When to use + and | operator?