Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7070929
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:37:33+00:00 2026-05-28T05:37:33+00:00

I’m trying to implement some functionality of BigInteger s as a personal programming exercise.

  • 0

I’m trying to implement some functionality of BigIntegers as a personal programming exercise.

Like many implementations I use an int[] as an unsigned integer.

I want to implement basic functionality like addition, subtraction, multiplication, division, but I’m hitting the problem that I need to get a “human readable” toString out of my data structure for debug purposes, so that I can better inspect and understand what I’m doing.

I feel like I’m stuck. I’m not confident that my algorithms are right, but I have no way to check it.

I have looked at some implementations like Apache Harmony or OpenJDK, but the algorithms they use to create the String look more complex than the actual implementation of plus, minus, … and so on.

Of course I could just use one of those complicated ones, but I would at least want to be able to understand the implementation of it, if I already fail at implementing it myself.

Can someone suggest a simple implementation which converts an int[] to a String?

Example: new int[]{Integer.MAX_VALUE, 1} should be treated as one large, unsigned number and print: 8589934590 (So basically 2³³).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T05:37:34+00:00Added an answer on May 28, 2026 at 5:37 am

    I’d use this approach:

    • You don’t mention how you store zero; it may need special handling, in which case you can special-case it before proceeding with the rest of the algorithm.
    • Make a copy of the int[]; call it iArr. The rest of this algorithm will operate on iArr.
    • Create a char[] — call it s — that is big enough. Since an int can have up to ten digits, you can use s = new char[iArr.length * 10].
    • Start at position int i = s.length - 1. Go through iArr, dividing by ten, and store the remainder plus '0' in s[i]. Then decrement i. Repeat this process until iArr is zero. (The logic for dividing by ten is roughly: divide each element by ten, noting the remainder. Add that remainder, times Integer.MAX_INT + 1, to the next element, before dividing that element by ten. Needless to say, you’ll need to do your math using long.)
    • Your result is new String(s, i, s.length - i).

    For efficiency’s sake:

    • You can potentially divide by a power of ten, such as 1000000 or whatnot, to get a bunch of digits at once (obviously this makes the char-handling trickier, though).
    • As you zero-out leading elements of iArr, you can keep track of where the first non-zero element is, and ignore any elements before that.

    but neither of those is actually necessary.


    Edited to add actual code. This program:

    public class Main
    {
        private static final long RADIX = -2L * (long)Integer.MIN_VALUE;
    
        public static void main(final String... args)
        {
            System.out.println(stringify(new int[]{0})); // 0
            System.out.println(stringify(new int[]{1})); // 1
            System.out.println(stringify(new int[]{Integer.MAX_VALUE})); // 2^31-1
            System.out.println(stringify(new int[]{Integer.MIN_VALUE})); // 2^31
            System.out.println(stringify(new int[]{-1})); // 2^32-1
            System.out.println(stringify(new int[]{1, 0})); // 2^32
            System.out.println(stringify(new int[]{1, -1})); // 2^33-1
            System.out.println(stringify(new int[]{-1, -1})); // 2^64-1
            System.out.println(stringify(new int[]{1, 0, 0})); // 2^64
        }
    
        private static String stringify(final int[] _iArr)
        {
            final int[] iArr = new int[_iArr.length];
            System.arraycopy(_iArr, 0, iArr, 0, _iArr.length);
            final char[] ret = new char[10 * iArr.length];
            int retIndex = ret.length;
            while(true)
            {
                boolean isZero = true;
                int carry = 0;
                for(int i = 0; i < iArr.length; ++i)
                {
                    long val = unsignedInt2Long(iArr[i]);
                    if(val != 0L)
                        isZero = false;
                    val += carry * RADIX;
                    carry = (int) (val % 10L);
                    val /= 10L;
                    iArr[i] = long2UnsignedInt(val);
                }
                if(isZero)
                {
                    if(retIndex == ret.length)
                        return "0";
                    else
                        return new String(ret, retIndex, ret.length - retIndex);
                }
                assert(retIndex > 0);
                ret[--retIndex] = (char) (carry + (int)'0');
            }
        }
    
        private static long unsignedInt2Long(final int unsignedInt)
        {
            if(unsignedInt >= 0)
                return unsignedInt;
            else
                return unsignedInt + RADIX;
        }
    
        private static int long2UnsignedInt(final long _long)
        {
            assert(_long >= 0L);
            assert(_long < RADIX);
            if(_long <= (long) Integer.MAX_VALUE)
                return (int) _long;
            else
                return (int) (_long - RADIX);
        }
    }
    

    prints this:

    0
    1
    2147483647
    2147483648
    4294967295
    4294967296
    8589934591
    18446744073709551615
    18446744073709551616
    

    (But you’ll have to examine the main method, with its comments, to confirm that I’ve correctly understood how you intend to store these integers.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have some data like this: 1 2 3 4 5 9 2 6
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.