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 7714743
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T02:12:27+00:00 2026-06-01T02:12:27+00:00

[EDIT] I am NOT accepting any answer which involves BigInteger, or other similarly inefficient

  • 0

[EDIT] I am NOT accepting any answer which involves BigInteger, or other similarly inefficient method. Please actually read the question before answering!

Java, annoyingly enough, does not support unsigned number types. You can convert a byte, short or int to unsigned, by using the next bigger type, for example:

short s = -10;
int unsigned_short = s & 0xFFFF;

But you cannot do this with long, since there is no bigger type.

So, how do you convert a signed long into an “unsigned” base-X, in my case base-36, and back? The Long class has those methods, but treat longs as signed, simply because they are.

I could probably do that using some manipulation and BigInteger, but BigInteger is incredibly slow, and creates garbage through temporary BigInteger creation. And I’m going to be doing a lot of those conversions (I think). I need an algorithm that is as efficient as the default implementation of Long.toString(long i, int radix).

Trying to adapt the code of Long.toString() I come to:

final int RADIX = 36;
final char[] DIGITS = { '0', ... , 'Z' };
long value = 100;
if (value == 0) {
    return "0";
} else {
    char[] buf = new char[13];
    int charPos = 12;
    long i = value;
    while (i != 0) {
        buf[charPos--] = DIGITS[Math.abs((int) (i % RADIX))];
        i /= RADIX;
    }
    return new String(buf, charPos + 1, (12 - charPos));
}

But it does not handle negative values correctly, despite the Math.abs().

Once this works, I need the reverse conversion, but I’m hoping it will be easier. Your welcome to put it in your answer too.

[EDIT] Actually, I just looked at the code for Long.parseLong(String s, int radix), and it looks more complicated than Long.toString(long i, int radix).

  • 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-06-01T02:12:29+00:00Added an answer on June 1, 2026 at 2:12 am
        long l = 0xffffffffffffffffL; // any long, e.g. -1
    
        // to string
        BigInteger bi = new BigInteger(Long.toString(l & ~(1L << 63)));
        if (l < 0) bi = bi.setBit(64);
        final String b36 = bi.toString(36);
        System.out.println("original long:" + l);
        System.out.println("result 36: " + b36);
    
        // parse
        final BigInteger parsedBi = new BigInteger(b36, 36);
    
        l = parsedBi.longValue();
        if (parsedBi.testBit(64)) l = l | (1L << 63);
        System.out.println("parsed long = " + l);
    

    Benchmarking (one million operations):

        // toString
        long l = 0x0ffffffffffffeffL;
        {
            final long start = System.currentTimeMillis();
            for (int i = 0; i < 1000000; i++) toStringBi(l);
            System.out.println("BigInteger time = " + 
                (System.currentTimeMillis() - start) + " ms.");
        }
        {
            final long start = System.currentTimeMillis();
            for (int i = 0; i < 1000000; i++) Long.toString(l, 36);
            System.out.println("Long.toString time = " + 
               (System.currentTimeMillis() - start) + "ms.");
        }
        // Parsing
        final String b36 = toStringBi(l);
        final String long36 = Long.toString(l, 36);
        {
            final long start = System.currentTimeMillis();
            for (int i = 0; i < 1000000; i++) {
                final BigInteger parsedBi = new BigInteger(b36, 36);
                l = parsedBi.longValue();
                if (parsedBi.testBit(64)) l = l | (1L << 63);
            }
            System.out.println("BigInteger.parse time = " 
                + (System.currentTimeMillis() - start) + " ms.");
        }
        {
            final long start = System.currentTimeMillis();
            for (int i = 0; i < 1000000; i++) Long.parseLong(long36, 36);
            System.out.println("Long.parseLong time = " 
                + (System.currentTimeMillis() - start) + "ms.");
        }
    
    • BigInteger time = 1027 ms.
    • Long.toString time = 244ms.
    • BigInteger.parse time = 297 ms.
    • Long.parseLong time = 132ms.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

LATER EDIT: Please mention not only game programming books, but also more scientific/simulation oriented
EDIT I isolated a real minimal example which does not work (it is a
EDIT: Learned that Webmethods actually uses NLST, not LIST, if that matters Our business
EDIT: It is not a listbox. My mistake. It is a list view. I
Edit: This is not a conflict on the theoretical level but a conflict on
Edit: It's not a bug as Martin pointed out. I'm just crossing the daylight
[edit] I am NOT using jquery in this app. Looking for a way to
edit: I'm not looking for you to debug this code. If you are familiar
Edit #2: i disabled my 404 page and the default not found page says
EDIT: Recent happenings of me compiling a program I know could not compile lead

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.