aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Simple question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Simple question" Watch "Simple question" New topic
Author

Simple question

Steven Wong
Ranch Hand

Joined: Mar 07, 2002
Posts: 295
Hi,
int x = 0xFFFFFFF1;
System.out.println(x); //prints -15
How do I calculate the above out to be -15?

Clement


best regards,<br />Steven<br />SCJP, SCEA
Steven Sun
Greenhorn

Joined: Apr 29, 2002
Posts: 26
Originally posted by Clement Ng:
Hi,
int x = 0xFFFFFFF1;
System.out.println(x); //prints -15
How do I calculate the above out to be -15?

Clement

Maybe this wiil do:
0xFFFFFFF1 == 1111 1111 1111 1111 1111 1111 1111 0001
15 == 0000 0000 0000 0000 0000 0000 0000 1111
plus them ,u get 1 0000...0000,which overload 4 bytes,so the highest "1" will be dropped and u get int 0.
so 0xFFFFFFF1 ==0-15 ==-15


newly SCJP2 <img src="tongue.gif" border="0">
Steven Wong
Ranch Hand

Joined: Mar 07, 2002
Posts: 295
Yup, I got it.
Thanks.
Clement
Nazmul Huda Sarkar
Ranch Hand

Joined: Feb 01, 2002
Posts: 317
Originally posted by Steven Sun:

Maybe this wiil do:
0xFFFFFFF1 == 1111 1111 1111 1111 1111 1111 1111 0001
15 == 0000 0000 0000 0000 0000 0000 0000 1111
plus them ,u get 1 0000...0000,which overload 4 bytes,so the highest "1" will be dropped and u get int 0.
so 0xFFFFFFF1 ==0-15 ==-15

It seems you have to know thw result first..
What about that......?
0xFFFFFFF1 == 1111 1111 1111 1111 1111 1111 1111 0001
To get the decimal number...u have to 2's complement the bits. That is :
First Invert the bits, which results :
0000 0000 0000 0000 0000 0000 0000 1110
Then add 1 :
0000 0000 0000 0000 0000 0000 0000 1111
Which is 15 , as it is a negetive number so the actual result is -15


Nazmul<br />SCJP,SCWCD,IBM OOAD with UML
Shura Balaganov
Ranch Hand

Joined: Apr 22, 2002
Posts: 664
How about this:
1. 0xFFFFFFF1 is negative
2. 0xFFFFFFFF = -1
3. 0xFFFFFFFF - 0xFFFFFFF1 = 0x0000000E = 14
4. add #2 and -#3 (-1 -"offset") = -1-14=-15
All you need to remember to do this calculations is that 0xFFFFFFFF = -1, and the rest is decrementing, i.e. 0xFFFFFFFE = -2, 0xFFFFFFFD = -3, etc.
[ May 07, 2002: Message edited by: Shura Balaganov ]

Any posted remarks that may or may not seem offensive, intrusive or politically incorrect are not truly so.
RusUSA.com - Russian America today - Guide To Russia
Nazmul Huda Sarkar
Ranch Hand

Joined: Feb 01, 2002
Posts: 317
According to digital electronics 2's Complement is the standard method to get a negetive number.
Shura Balaganov
Ranch Hand

Joined: Apr 22, 2002
Posts: 664
Originally posted by Md. Nazmul Huda Sarkar:
According to digital electronics 2's Complement is the standard method to get a negetive number.

Yeah, but my method is a whole lot easier.
Wei Du
Greenhorn

Joined: May 03, 2002
Posts: 16
Shura Balaganov's method only works if the number in question is close to the boundaries. 2's complement is a standard way, and in my opion, very efficient way to solve this kind of questions.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Simple question