BigInteger doesn't support >>>

Posted on 2006-04-20
Last Modified: 2006-11-18
I have a large number. I need to grab the number represented by the first 21 bits. I do this by doing:
    (number & 0xFFFFF8000L) >>> 15;
That works in java because the number is of type long which is 64 bits. However, in javascript I have found that the bitwise operators truncate values down to 32 bits. So, to get around this I found a javascript implementation of the Java class BigInteger. It is at:
I can do the mask using the and() method. But, I don't know how to do the shift. The javascript implementation, like the Java implementation of BigInteger, does not support unsigned bitwise shifts.

I can do this:

var origEncodedVal = 26766802693;
var encodedVal = new BigInteger(""+origEncodedVal, 10);
var mask = new BigInteger("FFFFF8000",16);
var result = encodedVal.and(mask).shiftRight(15);

but won't the final result be wrong if the initial bit is set in the original encoded value? I'm not worried about other masks on that number like 0x000007F00 because the high bit will be 0 regardless, so a signed shift and unsigned shift do the same thing in that case..

How do I deal with this? Why does BigInteger not implement unsigned shift right? The javadoc of BigInteger says:
    "The unsigned right shift operator (>>>) is omitted, as this operation makes little sense in combination with the "infinite word size" abstraction provided by this class."
but that statement doesn't make any sense to me.

I suppose one solution would be to shift right one bit and then AND it with a mask that zeroes out the high bit. Is that my best bet or is there a better way to do it? Another solution I suppose would be to prefix the original number with "0" and adjust the masks accordingly. It just seems silly that I would need to do that though.
Question by:HappyEngineer
    LVL 14

    Expert Comment

    by:Tommy Braas
    If you need to right shift, just divide by 2 the number of times you want ot shift.
    LVL 92

    Accepted Solution

    BigInteger has no defined word size, so its not a problem as the virtual sign bit won't be affected.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Suggested Solutions

    INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
    Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
    Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
    This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now