This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.

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

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

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 ]

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.