Forums Register Login

Effective Java 2nd Edition - item 15

+Pie Number of slices to send: Send
Hi All,

can anyone explain me this stanza mentioned in Effective Java 2nd Edition

Not only can you share immutable objects, but you can share their internals.
For example, the BigInteger class uses a sign-magnitude representation
internally. The sign is represented by an int, and the magnitude is represented by
an int array. The negate method produces a new BigInteger of like magnitude
and opposite sign. It does not need to copy the array; the newly created BigInteger
points to the same internal array as the original.



Kindly help.

Thanks
+Pie Number of slices to send: Send
It is about the design of objects; if you have one immutable object, all its fields will be immutable and those fields can be re-used. I presume in the case of an int[] array that means re-used within instances of the same class.
+Pie Number of slices to send: Send
The BigInteger is stored in a twos complement form.
And since its signed ie both + and - numbers can be represented. The magnitude and sign are kept at two separate places. The sign of the number is stored in a separate integer and the magnitude of the number is stored in the int array.
So a bigInteger -10 would have - stored in the int for sign and 10 stored in an array on int.
+Pie Number of slices to send: Send
Good explanation.

Thanks
+Pie Number of slices to send: Send
 

Originally posted by ashu Suri:
The BigInteger is stored in a twos complement form.
. . .
So a bigInteger -10 would have - stored in the int for sign and 10 stored in an array on int.



I am afraid you are mistaken. That's not two's complement. It's sign and magnitude.
+Pie Number of slices to send: Send
In the API it is written

All operations behave as if
* BigIntegers were represented in two's-complement notation

 
+Pie Number of slices to send: Send
That's what causes the confusion: "as if".
Well behaved women rarely make history - Eleanor Roosevelt. tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1016 times.
Similar Threads
Adding 2 int arrays together
Forbidden Conversions
Converting long hexadecimal strin to decimal
The Shift Operators
java.math.BigDecimal vs double or float datatypes
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 06:41:35.