Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Bitwise operator " | "  RSS feed

 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone please explain what is a Bitwise operator which looks like "|"? I've read the online Java Tutorial and a few books but I still don't quite understand it.

For e.g. how do we know the bitwise value of 5 or 7? Do we hv to memorize it?

I've also seen SCJP question that looks like this:



Will the (++j) and/or the (i++) inside the "if" condition execute before (i += j)?

Thank you in advance.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi LS chin,
maybe you don't know the difference with "|" and "||" .

"i==++j" will execute at first and "i++==j" will execute later.

"i==++j" will execute at first , and
if that is true , "i++==j" will execute ,
if thst is false , "i++==j" won't execute .
chenchen.
 
Ls chin
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by chen owen:
"i==++j" will execute at first and "i++==j" will execute later.

Hi owen,

Thanks for your reply. Yea, I'm a bit confused by "|" and "||". With the "|" operator, it means that (i++ == j) will continue to execute, even if (i == ++j) value is false? So, it doesn't matter whether the right-side or the left-side value is true or false, both sides will execute?
 
Ranch Hand
Posts: 693
Eclipse IDE Firefox Browser Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is a URL to wikipedia.org on the binary number system and binary operations:
Binary numeral system (http://www.wikipedia.org

The following URL to simple.wikipedia.org is under construction and anybody can improve it:
Binary numeral system (simple.wikipedia.org)
[ August 06, 2008: Message edited by: Kaydell Leavitt ]
 
chen owen
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi LS chin:
yeah , you are right ! You can take a test to prove it .
 
Ls chin
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Owen,
I tested the code by replacing the value of (i = 3) and the output shows that the right-side of "|" will execute even if the left-side is true. But for the "||" operator, it will short-circuit and will skip the right-side equation, therefore (i++ == j) will be ignored. Thanks!

Hi Kaydell,
Thanks for the links.

[ August 06, 2008: Message edited by: LS chin ]

[ August 06, 2008: Message edited by: LS chin ]
[ August 06, 2008: Message edited by: LS chin ]
 
Sheriff
Posts: 21466
97
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please note that in your example, | is not a bitwise operator but a logical OR operator. Whether | is one or the other depends on the argument type - if they are boolean it's logical, if they're numerical (not float or double) it's bitwise, any other case is a compile error.
 
Ls chin
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rob,

Thanks! In the example, "|" is a logical operator which does not short-circuit.

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!