3

I would like to create to create an x-bit binary number and specify the number of leftmost bits set.

I.e To create a 8-bit number with 6 most left bits all set as in 1111 1100

Similarly to create 16 bit - number with the 8 left bits all set resulting in 1111 1111 0000 0000

Need to be able to do this for large numbers (128 bits). Is there an existing way to do this using the core libs?

Thanks

1
  • How do you want your 128-bit numbers represented? Is a BitSet sufficient, or do you want a BigInteger? Do you need to support any width of number (e.g., 127-bit?), or just powers of two (like 8,16,32,64,128)? Commented Jan 11, 2016 at 19:36

2 Answers 2

1

Consider using a BitSet, like this:

import java.util.BitSet;

/**
 * Creates a new BitSet of the specified length
 * with the {@code len} leftmost bits set to {@code true}.
 *
 * @param totalBits The length of the resulting {@link BitSet}.
 * @param len       The amount of leftmost bits to set.
 * @throws IllegalArgumentException If {@code len > totalBits} or if any of the arguments is negative
 */
public static BitSet leftmostBits(int totalBits, int len)
{
    if (len > totalBits)
        throw new IllegalArgumentException("len must be smaller or equal to totalBits");
    if (len < 0 || totalBits < 0)
        throw new IllegalArgumentException("len and totalBits must both be positive");
    BitSet bitSet = new BitSet(totalBits);
    bitSet.set(0, len);
    return bitSet;
}

Here are some unit tests

Then, you can use that BitSet using its public API (here Java 8 is shown):

BitSet public API as of Java 8

BitSet has been designed for this (precise bit manipulation), and it provides you with an arbitrary length as well (does not limit you to 64 bits, like long for example would).

Sign up to request clarification or add additional context in comments.

1 Comment

I need to support 128 but numbers but i need to be able to specify the number of left bits arbitrarily. i.e. create a 128 bit number with the 122 left most bits set to 1. Plus im using java 7
0

You can use two loops. One for all the 1's and another for all the 0's.

Or using Java 8 you can do

InStream.range(0, ones).forEach(i -> System.out.print(1));
InStream.range(ones, bits).forEach(i -> System.out.print(0));

3 Comments

This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. - From Review
@skypjack while it doesn't provide a complete answer, it does say what the OP should do. This is just two lines of code. How much more explanation can you give without doing their homework for them?
I commented 3 hours ago, you edited the question 50 minutes ago, then you commented asking what's wrong. Are you serious? It wasn't a complete answer, now it has a few more details and maybe can be fine. thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.