Win a copy of Kotlin in Action this week in the Kotlin forum!
programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering Languages Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Simple question

Steven Wong
Ranch Hand
Posts: 295
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
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
Yup, I got it.
Thanks.
Clement

Nazmul Huda Sarkar
Ranch Hand
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..
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
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
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
According to digital electronics 2's Complement is the standard method to get a negetive number.

Shura Balaganov
Ranch Hand
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
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.