HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Is it better to store the magnitude of an arbitrary-precision number in BigEndian or LittleEndian order in an integer array?

Submitted by: @import:stackexchange-cs··
0
Viewed 0 times
littleendianthenumberorderarrayarbitraryprecisionbetterstorebigendian

Problem

I'm implementing a class which provides arbitrary-precision arithmetic (also called "bignum", "BigInteger", etc.).

My questions is about a practical implementation detail:

I'm wondering if there is a significant difference in implementation and computational complexity between an implementation which stores the magnitude in an integer array in BigEndian order vs. LittleEndian order.

My data structure is basically:

class BigInt
  val signum: Int
  val magnitude: Array[Int] // two-complement (unsigned)


Supported operations are for instance:

+, -, * (Long multiplication, Karatsuba, Cook3, Schönhage-Strassen), /, squaring
Conversion to other number types
Comparison, equality, representation as a String

The implementation is immutable, so every operation will return a new value and will not change the any existing.

Feel free to ask for clarifications!

Solution

You are replacing magnitude[i] by magnitude[SZ-i]. That won't have any effect on complexity nor ease of implementation. Depending on the way simplifications occur, one or the other could have slightly less index adjustments, but I won't dare a guess which. Little endian seems slightly more in line with the usual presentations of the algorithms (for instance in TAOCP), big endian had a more intuitive representation of the number in memory, but that's an opinion more than a fact.

If you look at memory access patterns, there was a time when cache prefiller handled better increasing consecutive accesses (which would have favored little endian), but nowadays, I don't think it still make a difference at least on workstation and server processors.

So I don't think there is a compelling argument to choose BE over LE or ortherwise. Take the one which make you more at ease.

Context

StackExchange Computer Science Q#1498, answer score: 5

Revisions (0)

No revisions yet.