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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T00:30:07+00:00 2026-05-14T00:30:07+00:00

I have the need to perform algorithms on various primitive types; the algorithm is

  • 0

I have the need to perform algorithms on various primitive types; the algorithm is essentially the same with the exception of which type the variables are. So for instance,

/**
* Determine if <code>value</code> is the bitwise OR of elements of <code>validValues</code> array. 
* For instance, our valid choices are 0001, 0010, and 1000.
* We are given a value of 1001.  This is valid because it can be made from
* ORing together 0001 and 1000.
* On the other hand, if we are given a value of 1111, this is invalid because
* you cannot turn on the second bit from left by ORing together those 3
* valid values.
*/
public static boolean isValid(long value, long[] validValues) {
    for (long validOption : validValues) {
        value &= ~validOption;
    }
    return value == 0;
}

public static boolean isValid(int value, int[] validValues) {
    for (int validOption : validValues) {
        value &= ~validOption;
    }
    return value == 0;
}

How can I avoid this repetition? I know there’s no way to genericize primitive arrays, so my hands seem tied. I have instances of primitive arrays and not boxed arrays of say Number objects, so I do not want to go that route either.

I know there are a lot of questions about primitives with respect to arrays, autoboxing, etc., but I haven’t seen it formulated in quite this way, and I haven’t seen a decisive answer on how to interact with these arrays.

I suppose I could do something like:

public static<E extends Number> boolean isValid(E value, List<E> numbers) {
    long theValue = value.longValue();
    for (Number validOption : numbers) {
        theValue &= ~validOption.longValue();
    }
    return theValue == 0;
}

and then

public static boolean isValid(long value, long[] validValues) {
    return isValid(value, Arrays.asList(ArrayUtils.toObject(validValues)));
}

public static boolean isValid(int value, int[] validValues) {
    return isValid(value, Arrays.asList(ArrayUtils.toObject(validValues)));
}

Is that really much better though? That way will create a lot more objects than the original implementation, though it DRYs up the source code. Any thoughts in this matter would be appreciated.

  • 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-14T00:30:08+00:00Added an answer on May 14, 2026 at 12:30 am

    I asked a similar question before (Managing highly repetitive code and documentation in Java), and noted that the source code for java.util.Arrays is highly repetitive in its algorithms to deal with primitive array types.

    In fact, the source code contains this comment:

    The code for each of the seven primitive types is largely identical. C’est la vie.

    The answer that I accepted suggests the use of a code generator that lets you work with code templates instead. There’s also a comment that Sun/Oracle uses a templating system internally as well.

    You can also use reflection to reduce repetition, but this is likely to be slow, and perhaps not worth the effort. If you want to test out its performance, this snippet demonstrates the technique:

    import java.lang.reflect.Array;
    
    static long maskFor(Class<?> c) {
        return (
            c.equals(int.class) ? 1L << Integer.SIZE :
            c.equals(short.class) ? 1L << Short.SIZE :
            c.equals(byte.class) ? 1L << Byte.SIZE :
            0
        ) - 1;
    }   
    public static void reflectPrimitiveNumericArray(Object arr) throws Exception {
        int length = Array.getLength(arr);
        Class<?> componentType = arr.getClass().getComponentType();
        long mask = maskFor(componentType);
        System.out.format("%s[%d] = { ", componentType, length);
        for (int i = 0; i < length; i++) {
            long el = Array.getLong(arr, i) & mask;
            System.out.print(Long.toBinaryString(el) + " ");
        }
        System.out.println("}");
    }
    

    You can pass an int[] for arr, as well as other primitive array types. Everything is cast into long, with bit-masking to address sign extension.

    reflectPrimitiveNumericArray(new byte[] { (byte) 0xF0 });
    // byte[1] = { 11110000 }
    reflectPrimitiveNumericArray(new int[] { 0xF0F0F0F0 });
    // int[1] = { 11110000111100001111000011110000 }
    reflectPrimitiveNumericArray(new long[] { 0xF0F0F0F0F0F0F0F0L });
    // long[1] = { 1111000011110000111100001111000011110000111100001111000011110000 }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a form button in the form,which i need to perform basic action
I have a set of data on which I need to perform a topological
I have a need to perform search and replace operations on large blocks of
I have a task I need to perform, do_stuff(opts) , that will take ~1s
I need to perform an action after a session times out. However I have
I have a class that I need to perform some actions on but I
I have a pretty simple SQL I need to perform. I have a ProcessUser
I need to perform action, when my activity is fully loaded? Does Android have
I have a long-running cleanup operation that I need to perform in onDestroy() of
I have a lotus view that stores a number. I need to perform some

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.