Solved

BitSet to unsinged Int Java

Posted on 2009-04-06
7
1,485 Views
Last Modified: 2012-05-06
Hi i have a BitSet of 8 bits which are set with 8 boolean values. now how to convert this 8 bits to unsinged int or char in java.
public static BitSet bitSets(boolean[] vals)

		{

			

			BitSet bits1 = new BitSet(8); 

			for(int i=0; i<vals.length; i++) { 

			   bits1.set(i,vals[i]); 

			 }

			System.out.println("Bitset length--->"+bits1.length());

			return bits1;

	    }

Open in new window

0
Comment
Question by:karpra
  • 3
  • 3
7 Comments
 

Author Comment

by:karpra
ID: 24081991
the array can be a boolean value of 0 or 1 or integer value or 0 or 1 and the BitSet looks like 10010111 now this value of BitSets should be represented as unsinged int or char
thanks in advance
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 24082181
Try
        public static int bitset2int(BitSet bs) {

                int result = 0;

                for(int i = 0;i < bs.length();i++) {

                    if (bs.get(i)) {

                        result |= (1 << i); 

                    }

                }

                return result & Integer.MAX_VALUE;

        }

Open in new window

0
 

Author Comment

by:karpra
ID: 24082872
Thanks man
Can i use this below if i want 8 bit char actuall i need both. just fine tuned your method above.
thanks in advance,
Please correct if there is any needed.
public static char bitset2char(BitSet bs) {
              char result = 0;
              for(int i = 0;i < bs.length();i++) {
                  if (bs.get(i)) {
                      result |= (1 << i);
                  }
              }
              return (char) (result & Character.MAX_VALUE);
          }
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 500 total points
ID: 24085177
Since char is unsigned anyway, you only need to do make one change:
return (char) result;

Open in new window

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24096143
Any further questions?
0
 

Author Comment

by:karpra
ID: 24099482
oh just wating for testing results from the test team..:) will confirm.
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
matchUp  challenge 9 71
scoreUp challenge 14 48
base64 decode encode 12 96
compre toata in where clue oracle 4 41
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This video teaches viewers about errors in exception handling.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

705 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

22 Experts available now in Live!

Get 1:1 Help Now