dpy learn

Greenhorn

Posts: 4

Anatol Bollinger

Greenhorn

Posts: 11

posted 11 years ago

I asked myself this question a couple of days ago. The method used is the two's complement: Wikipedia: Two's complement

Cheers, Anatol

Cheers, Anatol

posted 11 years ago

An int is 32 bits. The first (left most) bit indicates the sign: 1 for negative, 0 for positive.

If the left most bit is 1, you can determine the number by changing each 1 to 0 and 0 to 1, then adding 1.

So:

1111 1000 0000 0000 0000 0000 0000 0000 = -134217728

Flip bits:

0000 0111 1111 1111 1111 1111 1111 1111 = 134217727

Add 1: 134217728.

And then add the - sign.

If the left most bit is 1, you can determine the number by changing each 1 to 0 and 0 to 1, then adding 1.

So:

1111 1000 0000 0000 0000 0000 0000 0000 = -134217728

Flip bits:

0000 0111 1111 1111 1111 1111 1111 1111 = 134217727

Add 1: 134217728.

And then add the - sign.

SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6

How To Ask Questions How To Answer Questions