File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
A friendly place for programming greenhorns!
Big Moose Saloon
Register / Login
Win a copy of
Head First Android
this week in the
Joined: Jul 12, 2005
Jul 13, 2005 05:07:00
What is the output of following code and could some one explain me how it is.
int x = 1;
int y = ~x + 1;
How does ~x works?
Joined: Aug 03, 2002
Jul 13, 2005 05:21:00
In General (Beginner)...
Ask a Meaningful Question
Getting someone to think and try something out is much more useful than just telling them the answer.
Joined: Mar 07, 2004
Jul 13, 2005 05:29:00
this means:-- x=00000000 00000000 00000000 00000001 ( representing as 32 bit).
int y=~ x +1;
here ~x inverts all bits in x. so now x :-
~x =11111111 11111111 11111111 11111110 ;
y=~x+1 =11111111 11111111 11111111 11111111;
now this number has left most bit as one so the "y is negative" using 2-complement rule here..
according to it:
invert all digit and 1 to it
y = (-) 00000000 00000000 00000000 00000000 + 1;
y = (-) 00000000 00000000 00000000 00000001;
y= (-) 1;
thus System.out.println(y); prints -1
hope u get the logic of unary operator;
Life is Beautiful Learn to See it.........
I agree. Here's the link:
subject: Unary opearator...
Rounding up function
All times are in JavaRanch time: GMT-6 in summer, GMT-7 in winter
| Powered by
Copyright © 1998-2015