File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Integer bitwise operator doubt? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Integer bitwise operator doubt?" Watch "Integer bitwise operator doubt?" New topic

Integer bitwise operator doubt?

Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 10031


I have the below doubt regarding ~ operator.

byte b = 3;
System.out.println(b); // Why it gives 3 instead of -4??

SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2398
My compiler complains when I try to compile that because ~b isn't a statement.
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 10031


When we say ++b, it accepts and why does it complain when we say ~b??
Sandeep Rana

Joined: Dec 01, 2006
Posts: 6
It's correct that it will give error "not a statement", moreover logically you are not assigning this ~b value to any variable. Any way this is b is same... You have not change the value of b.

If you will do something like
int c = ~b;
then c is -4

Hope you got the point

Try it out!!!
Anuragk kushwaha
Ranch Hand

Joined: Aug 02, 2006
Posts: 51
yes, it will give compilation error!!!11 Not a Statement

I agree. Here's the link:
subject: Integer bitwise operator doubt?
It's not a secret anymore!