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

  • Home
  • SEARCH
  • 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 6570965
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:49:22+00:00 2026-05-25T14:49:22+00:00

Given a binary string coded on 1 byte, is it possible to map several

  • 0

Given a binary string coded on 1 byte, is it possible to map several of that byte’s bits into an enum value ?
e.g. : suppose I want to cut my field into this :

  • bit1 and bit2 = field1
  • bit3 = field2
  • bit4 = field3
  • bit5 = field4
  • other bits are unused

How could I map that to, say :

public enum MyEnum {
    FIELD1, FIELD2, FIELD3, FIELD4;

    private static final EnumSet<MyEnum> ALLFIELDS = EnumSet.allOf(MyEnum.class);

}

What I’m trying to do is to filter out these enum values using a bitmask. For that, I did the following method :

public static <E extends Enum<E>> EnumSet<E> filterWithBitMask(EnumSet<E> set, int bitmask) {
    List<E> kept = new ArrayList<E>();
    for (E property : set) {
        if ((bitmask & (1 << ((Enum<E>) property).ordinal())) != 0) {
            kept.add(property);
        }
    }
    EnumSet<E> selectedProperties = set.clone();
    selectedProperties.retainAll(kept);
    return selectedProperties;
}

This works fine as long as my enum fields represent a single bit, but I can’t figure out how to combine them into a single field. Sorry if that looks obvious, but this is the first time I’m manipulating data at the bit level…

EDIT COMMENTS :
I edited the field structure to represent what I really have. So the only field that spans more than 1 bit is field1. Based on the presence of bit1 and bit2, I assign an weight variable to field1.

public enum MyByte {    
    BIT_1, BIT_2, BIT_3, BIT_4, BIT_5;

    private static final EnumSet<MyByte> ALLPROPERTIES = EnumSet.allOf(MyByte.class);

    public static EnumSet<Fields> getValues(int bitmask) {
        EnumSet<MyByte> selectedBits = filterWithBitMask(ALLPROPERTIES, bitmask);
        int weight = 0;
        EnumSet<Fields> fields = null;
        if (selectedBits.contains(BIT_1)) 
            weight += 1;
        if (selectedBits.contains(BIT_2))
            weight += 2;
        if (selectedBits.contains(BIT_1) || selectedBits.contains(BIT_2)) 
            fields = EnumSet.of(Fields.FIELD1.setValue(weight));
        if (selectedBits.contains(BIT_3)) {
            if (fields != null)
                fields.add(Fields.FIELD2);
            else fields = EnumSet.of(Fields.FIELD2);
        }
        if (selectedBits.contains(BIT_4)) {
            if (fields != null)
                fields.add(Fields.FIELD3);
            else fields = EnumSet.of(Fields.FIELD3);
        }

        if (selectedBits.contains(BIT_5)) {
            if (fields != null)
                fields.add(Fields.FIELD4);
            else fields = EnumSet.of(Fields.FIELD4);
        }

        return fields;
    }

    public enum Fields {

        // BIT_1 and BIT_2
        FIELD1,
        // BIT_3
        FIELD2,
        // BIT_4
        FIELD3,
        // BIT_5
        FIELD4;

        private int value;

        public Fields setValue(int i) {
            this.value = i;
            return this;
        }

    }


}

Here is the dirty solution I came up with for now, but I don’t really like it with all those if statements. If someone has a better solution, I’d be glad to change this 😉

Thanks !

  • 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-25T14:49:22+00:00Added an answer on May 25, 2026 at 2:49 pm

    not quite sure what you want. this might be of interest:

    import java.util.*;
    enum Field {
        field1((byte) 0xc0) {
            int filter_(byte mask) { // you may want this one to be different
                return mask & this.mask;
            }
        },
        field2((byte) 0x20), field3((byte) 0x10), field4((byte) 0x08);
        Field(byte mask) {
            this.mask = mask;
        }
        int filter_(byte mask) {
            return mask & this.mask;
        }
        static EnumSet<Field> filter(byte mask) {
            final EnumSet<Field> fields = EnumSet.noneOf(Field.class);
            for (Field field : values())
                if (field.filter_(mask) != 0) fields.add(field);
            return fields;
        }
        final byte mask;
    }
    public class Main {
        public static void main(String[] args) {
            for(Field field:Field.values()) {
                System.out.println(Field.filter(field.mask));
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can anybody give me some clue that how to convert binary string into a
I have a function that returns an 8 digit long binary string for given
Given either the binary or string representation of an IPv6 address and its prefix
I've got a chunk of code that reads binary data off a string buffer
I'm given three binary random variables: X, Y, and Z. I'm also given the
Given the following Java code for generating a binary file: DataOutputStream out = new
Given the path of a file (e.g. C:\MyPicture.jpg), how do I convert a binary
I need to generate a series of N random binary variables with a given
Given a specific DateTime value, how do I display relative time, like: 2 hours
I have a list of integer ASCII values that I need to transform into

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.