Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Simple question

 
Steven Wong
Ranch Hand
Posts: 295
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
int x = 0xFFFFFFF1;
System.out.println(x); //prints -15
How do I calculate the above out to be -15?

Clement
 
Steven Sun
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Steven Wong
Ranch Hand
Posts: 295
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yup, I got it.
Thanks.
Clement
 
Nazmul Huda Sarkar
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Shura Balaganov
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Nazmul Huda Sarkar
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
According to digital electronics 2's Complement is the standard method to get a negetive number.
 
Shura Balaganov
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic